Control Statements : If, If-else , nested if-else , while , do while , for , break, continue

Control statements are also called as decision making control structures . These controls are used to take decisions according to condition . 

1. If statement :  

Syntax:

if(condition)

{

    statement1;

    statement2;

}


Example : 

#include<stadio.h>

#include<conio.h>

void main()

{

int a=10;

if(a==10)

{

printf("Value is equal");

getch();

}


2. If-else :

It is used to take decision from one or multiple conditions . 

Syntax : 

if(condition)
{
   statements;
}
else
{
statements;
}

Example : 

#include<stadio.h>
#include<conio.h>
void main()
{
int a,b;
printf("Enter Value for a ");
scanf("%d",&a);
printf("Enter value for b");
scanf("%d",&b);
if(a>b)
{
printf("a is greater");
}
else
{
printf("b is greater");
}
getch();
}


 3. Nested if-else :

One if statement is used inside another  is called as nested if-else . It used to execute multiple statements.

Syntax: 

if(condition)

{

  if(condition)

    {

         statements;

    }

}

else

{

statements;

}


Example : 

#include<stadio.h>

#include<conio.h>

void main()

{

int a=10;

if(a<100)

{

   if(a==10)

    {

       printf("value is equal");

    }

    else

    {

       printf("value is not equal");

    }

else

{

printf("value is greater than 100");

}

getch();

}

 

while loop :

 It is an control structure used to execute or print multiple values at a time .

Syntax :

while(condtion)

{

statement;

increment/decrement;

}

 

Example :

Q. Write c program to print 1 to 10 values using while loop

#include<stdio.h>

#include<conio.h>

void main()

{

int t=1;

while(t<=10)

{

printf("%d\t",t);

t++;

}

printf("\n");

getch();

}

Output :

 1 2 3 4 5 6 7 8 9 10 


do while loop :

do while is bottom tested or exit controlled loop which executes the condition after executing the statements .

Syntax :

do
{
statements;
increment/decrement;
}
while(condition)
{
statements;
}

Example : 

print 1 to 15 numbers using do while loop 


#include<stadio.h>
#include<conio.h>
void main()
{
int i=1;
do
{
printf("\n%d",i);
i++;
}
while(i<=15)
{
}
getch();

} 

 

 for loop :

It is also an control structure used for iteration and can print multiple values at a time .

Syntax :

for(variable=initialization;condition;increment/decrement)

{

statement;

}

Example :

Q. Write a c program to print 1 to 10 values using for loop

#include<stdio.h>

#include<conio.h>

void main()

{

int t;

for(t=1;t<=10;t++)

{

printf("Values are : %d\t",t);

}

getch();

}

Output :

1 2 3 4 5 6 7 8 9 10 

 

break statement :

It is used inside loop and switch statement . It is used to terminate an loop.

Syntax :

break;

Example :

Q. Write a c program to execute break statement .

#include<stdio.h>

#include<conio.h>

void main()

{

int n;

for(n=1;n<=5;n++)

{

if(n==3)

{

printf("It is an break");

break;

}

printf("Numbers are : %d\n",n);

}

printf("Out of for loop");

getch();


Output :

Numbers are : 1

Numbers are : 2

It is an break statement

Out of for loop

 

 continue statement  :

This statement is used to go for next iteration after skipping some part of loop 

Syntax:

continue;

Example :

Q. Write c program to execute continue statement.

#include<stdio.h>

#include<conio.h>

void main()

{

int n;

for(t=1;t<=10;t++)

{

if(t==5)

{

printf("Continue statement ");

continue;

}

printf("Numbers are : %d\n",t);

}

printf("Out of for loop\n");

getch();

}


switch statement :

This is an multi-directional conditional control statement .

In this we cam give cases according to choice .

Example :

Q. Write c program to execute switch statement 

#include<stdio.h>

#include<conio.h>

void main()

{

int topper;

printf("Enter which student no you want to be topper : ");

scanf("%d",&topper);

switch(topper)

{

case1:

printf("Suchita\n");

case2:

printf("Darshan\n");

case3:

printf("Manisha\n");

default:

printf("Wrong choice\n");

}

}

getch();

}