If and nested if Statement

If Statements and its types

C provides two styles of flow control:
Branching
Looping

Branching:

  • Branching is deciding what actions to take and looping is deciding how many times to take a certain action.
  • Branching is so called because the program chooses to follow one branch or another.
  • In decision control statements (C if else and nested if), group of statements are executed when condition is true.  If condition is false, then else part statements are executed.
  • There are 3 types of decision making control statements in C language. They are,

  1.   if statements
  2.   if else statements
  3.   nested if statements

If Statement

Let us clarify some concepts of “If”, “else” and “nested if” decision control statements

  • Syntax for each C decision control statements are given in below table with description.
Decision control statementsSyntaxDescription
ifif (condition) 

Statements;
}
In these type of statements, if condition is true, then respective block of code is executed.
if…else

if (condition) 

Statement1; Statement2;

else 
{

Statement3; Statement4; 
}
In these type of statements, group of statements are executed when condition is true.  If condition is false, then else part statements are executed.
nested ifif (condition1)
{
Statement1; 
}
else_if(condition2) 

Statement2; 

else Statement 3;
If condition 1 is false, then condition 2 is checked and statements are executed if it is true. If condition 2 also gets failure, then else part is executed.

1. if statement
  • This is the most simple form of the branching statements.

NOTE: Expression will be assumed to be true if its evaluated values is non-zero.

if statements take the following form:

Example program for if statement in C:

 In “if” control statement, respective block of code is executed when condition is true.
#include<stdio.h>
#include<conio.h>
void main( )
{
int a;
printf("n Enter a number:");
scanf("%d", &a);
if(a>0)
{ 
printf( "n The number %d is positive.",a);
getch();
}

More Example:

#include<stdio.h>
#include<conio.h>
void main()
{
  int m=40,n=40;
  if (m == n)
  {
  printf("m and n are equal");
  }
}

Output:

m and n are equal


  • The single if statement in C language is used to execute the code if condition is true. The syntax of if statement is given below.

Let's see a simple example of c language if statement.
#include<stdio.h>  
#include<conio.h>  
void main(){  
int number=0;  
clrscr(); 
printf("enter a number:");  
scanf("%d",&number);  
if(number%2==0){  
printf("%d is even number",number);  
getch(); 
}  

Output
enter a number:4
4 is even number
enter a number:5

 Nested-if statement

  • In “nested if” control statement, if condition 1 is false, then condition 2 is checked and statements are executed if it is true. 
  • If condition 2 also gets failure, then else part is executed.
Example program for nested if statement in C: 
#include <stdio.h>
int main()
{
  int m=40,n=20;
  if (m>n) {
 printf("m is greater than n");
  }
  else if(m<n) {
  printf("m is less than n");
  }
  else {
  printf("m is equal to n");
  }
}
 Output:
m is greater than n

Post a Comment

Previous Post Next Post