what are the conditional statements in C ?Explain Each

As we know C is procedural language which executes a statements in procedural way the way it is written but we get some situation where we need to execute statements based on some condition . There we can use Conditional statements provided in C .

        Conditional statements are used to execute statement or  group of statements based on some conditon.

C supports following conditional statements.

  1. if statement
  2. if else statement
  3. if else if  ladder
  4. nested if
  5. go to statement

 

a.)    if statement :

Syntax :

If(Conditon) {

C statements;

}

If the condition is true then C statements are executed other wise next statement will be executed.

 

Example :

File1.c

#include<stdio.h>

int main(){

int a=10;

if(a%2==0){

printf(“ a is even no :”);

}

printf(“ statement after if “);

return 0;}

 

Output :

a is even no:

statement after if

 

File2.c

#include<stdio.h>

int main(){

int a=10;

if(a%2==0){

printf(“a is even no :”);

}

Printf(“statement after if”);

Output:

statement after if

 

b.)    if else statement

Syntax :-

if(condition){

Statements to be executed when condition is true;

}

else {

Statements to be executed when condition is false;

}

 

#include<stdio.h>

int main(){

int a=10;

if(a%2==0) {

printf(“ a is even “);

}

else {

printf(“a is odd “);

}

Return 0;

}

 

Output :

                                a is even.

c.)     if else if ladder

Syntax :

If(condition1){

Statements to be executed when  condition1 is true;

}

else if(condition2){

Statements to be executed when  condition2 is true;

}

else if(condition3){

Statements to be executed when  condition3 is true;

}

else if(….){

… .. .

}

else {

Statements to be executed when no condition is true;

}

 

Example :

#include<stdio.h>

int main(){

int a;

printf(“n Enter the no of day :” );

scanf(“%d”,&a);

if(a==1){

printf(“ Monday”);

}

else if(a==2){

printf(“Tuesday”);

}

else if(a==3){

printf(“Wednesdat”);

}

else if(a==4){

printf(“Thursday”);

}

else if(a==5){

printf(“Friday”);

}

else if(a==6){

printf(“Saturday”);

}

else if(a==7){

printf(“Sunday !! ”);

}

else {

printf(“ Enter the valid day between 1-7”);

}

return 0;

}

d.)    Nested if statements

if statement within if statements.

Example:

To find max of three no (a,b,c)

 

 

 

Input:

5 10 20

Output:

                                20 is max

e.)    go to statement

Syntax:

So far we have seen conditional statements which are executed  when certain condition is true or false.

go to statement is used to branch unconditinally from one point to another point. go to requires a label to identify where the control to be transferred.

Example:

 

 

 

%d bloggers like this: