KEMBAR78
Chapter 2 Control Structures DDJ | PDF | Control Flow | Computers
0% found this document useful (0 votes)
48 views10 pages

Chapter 2 Control Structures DDJ

Yhhzjskslodidnnxffff

Uploaded by

prataph863
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views10 pages

Chapter 2 Control Structures DDJ

Yhhzjskslodidnnxffff

Uploaded by

prataph863
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Chapter 2 Control Structures

2.1 Branching- If statement, If-else Statement, Multiway decision.


1. programming language runs programs sequentially from top to bottom.
2. However, due to certain requirements, we may need to skip some lines of code for
this, Branching Statements in C are useful.

The Branching Statements in C are categorized as follows.

❖ Conditional Branching Statements in C

• if Statement

• else Statement

❖ Unconditional Branching Statements in C

• switch Statement

• goto Statement

• break Statement

• continue Statement

Conditional Branching-

1.if Statement-

syntax

if (condition)
{
//code to be executed if condition specified evaluates is true
}

Program-

#include <stdio.h>
int main()
{
int x = 10;
if (x > 5)
{
printf("x is greater than 5");
}
return 0;
}
Output-
x is greater than 5
2.if else Statement-

syntax

if (condition)
{
// statement
}
Else
{
// code executed if condition is false
}

Program-
#include <stdio.h>
int main()
{
int x = 10;
if (x < 5)
{
printf("x is less than 5\n");
}
else
{
printf("x is greater than or equal to 5\n");
}
return 0;
}

Output-
x is greater than or equal to 5
2.2 Looping- While, do while, for-
The looping can be defined as repeating the same process multiple times until a specific
condition satisfies. Using loops, we do not need to write the same code again and again.
There are three types of loops in C language:
1. while
2. do while
3. for

1.While loop-
The while loop in c is to be used in the scenario where we don't know the number of
iterations in advance.

syntax

while(condition)
{
//code to be executed
}

Program-

#include<stdio.h>
int main()
{
int i=1;
while(i<=10)
{
printf("%d \n",i);
i++;
}
return 0;
}

Output-
1
2
3
4
5
6
7
8
9
10
Program-

#include<stdio.h>
int main()
{
int i=1,number;
printf("Enter a number: ");
scanf("%d",&number);
while(i<=10)
{
printf("%d \n",(number*i));
i++;
}
return 0;
}

Output-
Enter a number: 2
2
4
6
8
10
12
14
16
18
20

2. do while loop-
The do-while loop continues until a given condition satisfies.

syntax

do
{
//code to be executed
}
while(condition);
program-
#include <stdio.h>
int main()
{
int i = 0;

do
{
printf("%d\n", i);
i++;
}
while (i < 5);
return 0;
}

Output-
0
1
2
3
4

3.for loop- When you know exactly how many times you want to loop through a block of
code, use the for loop.

syntax

for (expression 1; expression 2; expression 3)


{
// code block to be executed
}
for (initialization; condition; increment/decrement)
{
//code to be executed
}
program-
#include <stdio.h>
int main()
{
int i;
for (i = 0; i < 5; i++)
{
printf("%d\n", i);
}
return 0;
}

Output-
0
1
2
3
4

2.3 Nested Control Structure-


1.Switch and break statement-
The switch statement in C is used when we have multiple conditions to check. It was used to
"jump out" of a switch statement.

syntax

switch (expression)
{
case value1:
// code to be executed if expression equals value1
break;
case value2:
// code to be executed if expression equals value2
break;
default:
// code to be executed if none of the cases match
break;
}
Program 1-

#include <stdio.h>
int main()
{
int x = 2;

switch (x)
{
case 1:
printf("x is 1");
break;

case 2:
printf("x is 2");
break;

case 3:
printf("x is 3");
break;

default:
printf("x is not 1, 2, or 3");
break;
}
return 0;
}

Output-
x is 2
Program 2-

#include<stdio.h>
int main()
{
int a,b;
int op;
printf(" 1.Addition\n 2.Subtraction\n 3.Multiplication\n 4.Division\n");
printf("Enter the values of a and b: ");
scanf("%d %d",&a,&b);
printf("Enter your Choice : ");
scanf("%d",&op);
switch(op)
{
case 1 :
printf("Sum of %d and %d is : %d",a,b,a+b);
break;
case 2 :
printf("Difference of %d and %d is : %d",a,b,a-b);
break;
case 3 :
printf("Multiplication of %d and %d is : %d",a,b,a*b);
break;
case 4 :
printf("Division of Two Numbers is %d : ",a/b);
break;
default :
printf(" Enter Your Correct Choice.");
break;
}
return 0;
}

Output-
1.Addition
2.Subtraction
3.Multiplication
4.Division
Enter the values of a and b: 2 2
Enter your Choice: 3
Multiplication of 2 and 2 is: 4
2.Continue statement- The continue statement skips some lines of code inside the loop and
continues with the next iteration.

syntax

//loop statements
continue;
//some lines of the code which is to be skipped
Program-
#include <stdio.h>
int main()
{
int i;

for (i = 0; i < 10; i++)


{
if (i == 4)
{
continue;
}
printf("%d\n", i);
}
return 0;
}

Output-
0
1
2
3
5
6
7
8
9
3.goto statement- The goto statement is known as jump statement in C.

syntax

label:
//some part of the code;
goto label;
program-
#include <stdio.h>
int main ()
{
int num;
printf("enter any number= ");
scanf("%d”, &num);
if (num == 0)
goto end;
printf("The number is: %d", num);

end:
printf ("\nEnd of program");

return 0;
}

Output-
1. enter any number= 2
The number is: 2
End of program

2. enter any number= 0


End of program

You might also like