< C Tutorial

C decision making

← Previous Next →


In decision control statements, a block of code are executed when condition is true and if condition is false then else part of code gets executed.

There are 3 types of decision making control statements in C language

  • if statements
  • if else statements
  • nested if statements

If Statement

Syntax:

if(condition) { // condition like x > 10 
  // if condition is true then all the code here will be executed
}

If- Else Statement

Syntax:

if(condition1) {
   // code block 1 
} else if ( condition2 ) { 
   // code block 2 
} else { 
  // code block 3 
}
  • In this format, condition is checked sequentially.
  • If condition1 is true then only code block 1 will be executed.
  • If condition1 is false and condition2 is true then code block 2 will be executed.
  • If all above condition fails then code block 3 will be executed.

Nested if statements

Syntax :

if(condition) {
   // code 1
   if(inner_condition) {
	// code 2	
   } else {
        // code 3		
   }
}

In this format if condition is true then inner if block will be check and further code be executed.

← Previous Next →