control structures (1)
control structures (1)
Syntax : -
If(test condition)
Statement A;
Statement x;
Example :-
#include<stdio.h>
#includr<conio.h>
Void main ()
Int n;
Clrscr();
Printf(“enter number”);
Scanf(“%d”,&n);
If(n%2==0)
Getch();
Syntax : -
Statement A;
Else
Statement B;
Statement x;
Example : -
#include<stdio.h>
#includr<conio.h>
Void main ()
Int a,b;
Clrscr();
Scanf(“%d %d”,&a,&b);
if(a>b)
Printf(“a is a greatest”);
else
Printf(“b is greatest”);
Getch();
Syntax : -
Statement A;
Statement B;
}
Statement C;
else(test condition n)
Statement n;
Example : -
#include<stdio.h>
#includr<conio.h>
Void main ()
Float per ;
Clrscr();
Printf(“enter percentage”);
Scanf(“%f”,&per);
if(per>=75)
Printf(“ Distinction”);
{
Printf(“Grade A”);
Printf(“Grade B”);
Printf(“Grade C”);
else
Printf(“Fail”);
Getch();
Nested if else statement : - when there are another if else statement in a if-block, then
it is called nesting of if-else statement
Syntax : -
if(test condition 1)
if(test condition 2)
{
Statement A;
else
Statement B;
Statement C;
Syntax: -
Switch(choice variable )
case 1:
Statement A;
break;
case 2:
Statement B;
Break;
default :
statement n;
Statement x;
Example :-
#include<stdio.h>
#include<conio.h>
Void main()
Int day-of-w;
Clrscr();
Scanf(“%d”,&day-of-w);
Switch(w)
Case1:
Printf(“Monday”);
break;
case2:
Printf(“Tuesday”);
Break;
case3:
printf(“Wednesday”);
break;
case4:
Printf(“Thursday”);
break;
case5:
printf(“Friday”);
break;
case6:
Printf(“Saturday”);
break;
case7:
printf(“Sunday”);
break;
default:
getch();
Syntax:-
go to label-name;
Looping statement :- It is used to control flow statements that repeat a block of code
as long as a specified condition is true.
Type of loops
1.while loop : - Here First condition is checked if, it is true body of the loop is excuted
else, If condition is false control will be come out of loop.
Syntax :-
While(test condition)
Statement A;
Statement B;
Example
#include<stdio.h>
#include<coni.h>
Void main()
Int i=1;
Clrscr ();
While(i<=5)
Printf(“welcome to c”);
i++;
getch();
Do while loop :- Here firstly statement inside body is executed then condition is
checked.
Syntax:-
do
{
Statement A;
while(test condition);
Statement B;
Example: -
#include<stdio.h>
#include<coni.h>
Void main( )
Int i=0;
Clrscr();
do
Printf(“welcome to c”);
while(i<=10);
i++;
getch();
for loop : - for loop is most powerful loop is use to purpose of repetition again and
again of this condition is true in sequence,it runs up to the condition is true.
Syntax:-
Statement A;
Example:-
#include<stdio.h>
#include<conio.h>
Void main()
Int I;
Clrscr();
For(i=0;i<=5;i++)
Printf(“welcome to c”);
getch();
Nesting of loop :- when a loop written inside the body of another loop then ,it is known
as nesting of loop.
Syntax:-
{
Statement A;
Statement B;
Example:-
#include<stdio.h>
#include<conio.h>
Void main()
int i,j,pos;
Clrscr();
Printf(“enter pos”);
Scanf(“%d”,&pos);
for(i=1;i<=pos;i++)
printf(“\n”);
for(“ j=1;j<=I;j++)
Printf(“\t%d”,i);
getch();
}
Break statement:- break statement is used inside loop and switch statement.
It is used to come out of the loop even before loop condition becomes false.
Continue statement :- continue statement is used for continuing next iteration of loop
after skipping some statement of loop.