CONTROL STATEMENTS IN C LAN
CONTROL STATEMENTS IN C LAN
(switch)
Q: Explain various selection statements with example.
Conditional statements: conditionally executes set of statements. These are of 2 types: if and switch
If statement it is of 4 types: if, if- else , if elseif, nested if(if else ladder)
if: here ,if the condition is true then respective block code is executed.
Syntax: Example:
If(condition) #include<stdio.h>
{ void main()
Statements; {
} int i;
printf(‘enter number”);
scanf(“%d”,&i);
if(i%2==0)
printf(“even number”);
}
if else : here ,if the condition is true then true block is executed else false block is executed.
Syntax: Example:
If(condition) #include<stdio.h>
{ voidvoid mainmain()
True block Statements; {
} int i;
else printf(‘enter number”);
{ scanf(“%d”,&i);
False block Statements; if(i%2==0)
} printf(“even number”);
else
printf(“odd number”);
}
if else if : when a condition is true if block is executed, if it fails else if block’s condition is checked , if the condition is true the
respective block will be executed. If it fails Else block is executed .
Syntax: Example:
If(condition) #include<stdio.h>
{ Void main()
Statements; {
} int a,b,c;
else if(condition) printf(‘enter 3 numbers”);
{ scanf(“%d%d%d”,&a,&b,&c);
Statements; if( a>b && a>c)
} printf(“A is Big”);
---- else if(b>c)
---- printf(“B is Big”);
Else else
{ printf(“ C is Big”);
Statements; }
}
Switch statement : it is used to select any one of the given choices. it is also called multi way branching statement.
In a switch statement, the “case value” must be of “char” and “int” type.
There can be one or N number of cases.
The values in the case must be unique.
Each statement of the case can have a break statement. It is optional.
The default Statement is also optional.
Syntax: Example:
Switch(variable) Void main()
{ {
case value1: Int a,b,c;
Statements; Char op;
break; Printf(“Enter Operator “);
case value2: Scanf(“%c”,&op);
Statements; Printf(“enter a,b value “);
break; Scanf(“%d%d”,&a,&b);
-- Switch(op)
--- {
--- Case ‘+’ : c=a+b;
case value n: Printf(“\n\t Sum is %d”,c);
Statements; Break;
break; Case ‘-’ : c=a-b;
default: Printf(“\n\t Sub is %d”,c);
Statements ; Break;
} Case ‘*’ : c=a*b;
Printf(“\n\t product is %d”,c);
Break;
Default:
Printf(“\n\t Wrong Operator “);
}
}
Iteration/ loops: set of statements will be executed continuously/repeatedly until the given condition is false. these are of 2 types
1.pre test / entry control loops 2.post test / exit control loops
Ex:while and for Ex:do while
1.pre test / entry control loops condition will be checked before entering in to the loop.
while: it is always repeated in clockwise direction. Condition is checked before execution of statements.
Syntax: Example:
Initialisation; Void main()
While(condition) {
{ int i=1;
statements; While(i<=10)
inc/dec; {
} printf(“%d”,i);
i++;
}
}
for: it contains initialization , condition , inc/ dec in the same place and this is very easy to use.
syntax: example:
for(initialization ; condition ; incre/decre) void main()
{ {
Statements; int i,n,s;
} printf(“enter N value “);
scanf(“%d”,&n);
for(i=1,s=0 ; i<=n ; i++)
{
s=s+i
}
Printf(“\n\t Sum of n numbers %d”,s);
}
2.post test / exit control loops without checking condition the set of statements will be executed at least once . set of statements
will be executed continuously/repeatedly until the given condition is false.
do while :it repeats in clockwise direction.it checks condition after execution of statements.
Syntax: Example:
Initialization; void main()
do {
{ int i=1;
Loop statements; do
Incre/decre ; {
}while(condition); printf(“%d”,i);
i++;
}while(i<=10);
}
break: it is keyword, used to terminate the loop (for, while ,do while, switch) from execution.
The break statement is one of the four jump statements in the C language
The purpose of the break statement in C is for unconditional exit from the loop
It can be used inside loops or switch statements to bring the control out of the block
Syntax: break;
Ex.
#include<stdio.h>
int main()
{
int i;
for(i=0;i<10;i++)
{
If(i==5)
{
printf(“coming out of loop when i=5”);
break;
}
printf(“%d”,i);
}
}
Output: 0 1 2 3 4 Coming out of loop when i=5
#include <stdio.h>
#include <stdlib.h>
int main ()
{
printf("Start of the program....\n");
printf("Exiting the program....\n");
exit(0);
printf("End of the program....\n");
return(0);
}
goto statement / label loop : this is unconditional loop. using goto statement cursor moves to the specified label in the program
The goto statement in C allows the program to jump to some part of the code, giving you more control over its execution.
While it can be useful in certain situations, like error handling or exiting complex loops,
it’s generally not recommended because it can make the code harder to read and maintain.
Syntax: Void main()
Label name: {
Statements Kiet:
Goto label name; Printf(“\n\t welcome to c Program”);
Goto kiet;
}