Conditional Statements : if, else, switch

Conditional statements help you to make a decision based on certain conditions. These conditions are specified by a set of conditional statements having boolean expressions which are evaluated to a boolean value true or false. There are following types of conditional statements in C.

1.      If statement

2.      If-Else statement

3.      Nested If-else statement

4.      If-Else If ladder

5.      Switch statement

If statement

The single if statement in C language is used to execute the code if a condition is true. It is also called one-way selection statement.

Syntax

if(expression)
{
 //code to be executed
}

How “if” statement works..

·         If the expression is evaluated to nonzero (true) then if block statement(s) are executed.

·         If the expression is evaluated to zero (false) then Control passes to the next statement following it.

Note

“Expression must be scalar type” i.e evaluated to a single value.

if Statement Example

#include<stdio.h>
#include<conio.h>
void main()
{
int num=0;
printf("enter the number");
scanf("%d",&num);
if(n%2==0)
{
printf("%d number in even",num);
}
getch();
}

If-else statement

The if-else statement in C language is used to execute the code if condition is true or false. It is also called two-way selection statement.

Syntax

if(expression)
{
 //Statements
}
else
{
 //Statements
}

How “if..else” statement works..

·         If the expression is evaluated to nonzero (true) then if block statement(s) are executed.

·         If the expression is evaluated to zero (false) then else block statement(s) are executed.

if..else Statement Example

#include<stdio.h>
#include<conio.h>
void main()
{
int num=0;
printf("enter the number");
scanf("%d",&num);
if(n%2==0)
{
printf("%d number in even", num);
}
else
{
printf("%d number in odd",num);
}
getch();
}

 

Nested If-else statement

The nested if…else statement is used when a program requires more than one test expression. It is also called a multi-way selection statement. When a series of the decision are involved in a statement, we use if else statement in nested form.

Syntax

if( expression )
{ 
 if( expression1 )
 {
 statement-block1;
 }
 else 
 {
 statement-block 2;
 }
}
else
{
 statement-block 3;
}

Example

#include<stdio.h>
#include<conio.h>
void main( )
{ 
 int a,b,c;
 clrscr();
 printf("Please Enter 3 number");
 scanf("%d%d%d",&a,&b,&c);
 if(a>b)
 {
 if(a>c)
 {
 printf("a is greatest");
 }
 else 
 {
 printf("c is greatest");
 }
 }
 else
 {
 if(b>c)
 {
 printf("b is greatest");
 }
 else
 {
 printf("c is greatest");
 }
 }
getch();
} 

 

If..else If ladder

The if-else-if statement is used to execute one code from multiple conditions. It is also called multipath decision statement. It is a chain of if..else statements in which each if statement is associated with else if statement and last would be an else statement.

Syntax

if(condition1)
{
 //statements
} 
else if(condition2)
{
 //statements
}
else if(condition3)
{
 //statements
}
else
{
 //statements
}

If..else If ladder Example

#include<stdio.h>
#include<conio.h>
void main( )
{
 int a;
 printf("enter a number");
 scanf("%d",&a);
 if( a%5==0 && a%8==0)
 {
 printf("divisible by both 5 and 8");
 } 
 else if( a%8==0 )
 {
 printf("divisible by 8");
 }
 else if(a%5==0)
 {
 printf("divisible by 5");
 }
 else 
 {
 printf("divisible by none");
 }
getch();
}

Switch Statement

switch statement acts as a substitute for a long if-else-if ladder that is used to test a list of cases. A switch statement contains one or more case labels which are tested against the switch expression. When the expression match to a case then the associated statements with that case would be executed.

Syntax

Switch (expression)
{
 case value1:
 //Statements 
 break;
 case value 2:
 //Statements
 break; 
 case value 3:
 //Statements 
 case value n:
 //Statements
 break;
 Default:
 //Statements
}
 

switch statement Example

#include<stdio.h>
#include<conio.h>
void main( )
{
 char grade = 'B';
 
 if (grade == 'A')
 {
 printf("Excellent!");
 }
 else if (grade == 'B')
 {
 printf("Well done");
 }
 else if (grade == 'D')
 {
 printf("You passed");
 }
 else if (grade == 'F')
 {
 printf("Better try again");
 }
 else
 {
 printf("You Failed!");
 }
 }
getch();
}

Read More Articles Related to C Programming Language

Note

·         The switch statement must be an integral type.

·         Case labels must be constants.

·         Case labels must be unique.

·         Case labels must end with a colon.

·         The break statement transfers the control out of switch statement.

·         The break statement is optional.

Related Posts

© 2024 Software Engineering - Theme by WPEnjoy · Powered by WordPress