0% found this document useful (0 votes)
2 views

control structures (1)

The document provides an overview of control structures in programming, including if statements, if-else statements, nested if-else statements, switch cases, and looping constructs such as while, do-while, and for loops. It includes syntax examples and sample programs demonstrating how to implement these control structures in C. Additionally, it discusses break and continue statements for managing loop execution flow.

Uploaded by

patilmohit882
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)
2 views

control structures (1)

The document provides an overview of control structures in programming, including if statements, if-else statements, nested if-else statements, switch cases, and looping constructs such as while, do-while, and for loops. It includes syntax examples and sample programs demonstrating how to implement these control structures in C. Additionally, it discusses break and continue statements for managing loop execution flow.

Uploaded by

patilmohit882
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/ 13

Control struCtures

If statement : - The statement is executed only when condition is true. Otherwise, it is


skipped from execution.

Syntax : -

If(test condition)

Statement A;

Statement x;

Example :-

Write a program check whether given number is even or odd.

#include<stdio.h>

#includr<conio.h>

Void main ()

Int n;

Clrscr();

Printf(“enter number”);

Scanf(“%d”,&n);

If(n%2==0)

Printf(“given number is even”);


}

Printf (“thanks for using this program “);

Getch();

If else statement : - It is bidirectional conditional control statement that contains one


condition & two possible action.

Syntax : -

If( test condition)

Statement A;

Else

Statement B;

Statement x;

Example : -

Write a program to check whether greatest among two.

#include<stdio.h>

#includr<conio.h>

Void main ()

Int a,b;
Clrscr();

Printf(“enter a and b”);

Scanf(“%d %d”,&a,&b);

if(a>b)

Printf(“a is a greatest”);

else

Printf(“b is greatest”);

Printf(“thanks for using this program”);

Getch();

if else LADDER : - It is used to check more than two condition.

It is used are multiple cases to be performed for different conditions.

Syntax : -

If( test condition 1)

Statement A;

else if(test condition 2)

Statement B;
}

else if(test condition 3)

Statement C;

else(test condition n)

Statement n;

Example : -

Write a program to print grades of student based on percentage.

#include<stdio.h>

#includr<conio.h>

Void main ()

Float per ;

Clrscr();

Printf(“enter percentage”);

Scanf(“%f”,&per);

if(per>=75)

Printf(“ Distinction”);

else if(per>=60 && per<=75)

{
Printf(“Grade A”);

else if(per>=55 && per<=60)

Printf(“Grade B”);

else if(per>=40 && per<=55)

Printf(“Grade C”);

else

Printf(“Fail”);

Printf(“thanks for using this program”);

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;

Switch case :- an alternate to if-else-if ladder statement which allows us to execute


multiple operations for the different possibles values of a single variable called switch
variable.

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();

Printf(“enter days of week “);

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:

Printf(“ no such day”);

Printf(“thanks for using this program”);

getch();

Go to statement :- go to is statement is use to transfer control of the program


statement from one Statement to perticular location as given per label given.

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

Write a program to print 5 times welcome to c.

#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: -

Write a program to print 5 times welcome to c.

#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:-

for(initial condition; final condition; increment & decrement)

Statement A;

Example:-

Write a program to print 5 times welcome to c.

#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:-

For(initial condition; final condition; increment & decrement)

{
Statement A;

for(initial condition; final condition; increment & decrement)

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.

You might also like