Decision Making in C: Conditional Statements and Control Flow

Rumman Ansari   Software Engineer   2024-07-04 05:10:54   13716  Share
Subject Syllabus DetailsSubject Details
☰ TContent
☰Fullscreen

Table of Content:

Programming language uses control statements to cause the flow of execution to advance and branch based on changes to the state of a program.Selection statements (if-else, switch) allow your program to choose different paths of execution based upon the outcome of an expression or the state of a variable.

C supports various selection statements, like if, if-else and switch . These statements allow us to control the flow of our program’s execution based upon conditions known only during run time.

There are various types of if statement in C.

  • if statement
  • if-else statement
  • nested if statement
  • if-else-if ladder

There are another types of selection or decision making statement in C.

  • switch

if statement

if statement executes an action if and only if the condition is true.

  • The if statement is a conditional statement. It is used to test the condition.
  • It tells your program to execute a certain section of code only if a particular test evaluates to true.
  • It checks boolean condition: true or false,  If boolean Expression evaluates to true, the statements in the block following the if statement is executed.
  • If it evaluates to false, the statements in the if the block is not executed.

Syntax of if statement

if (boolean-expression) {
statement(s);
}

Flow Diagram

if statement in C

The above flowchart illustrates how C program executes the syntax of an if statement. If the boolean-expression evaluates to true , the statements in the block are executed. As an example, see the following code:

if (radius >= 0) {
area = radius * radius * PI;
System.out.println("The area for the circle of radius "+radius+ " is "+area);
}

If the value of radius is greater than or equal to 0 , then the area is computed and the result is displayed; otherwise, the two statements in the block will not be executed.

Practical approach of the above code

 #include
    void main(){
	float radius, area, PI;
	PI=3.14f;
	radius = 2.1f ;

	 if (radius >= 0) {
	 area = radius * radius * PI;
	 printf("The area for the circle %f ",area);
     }
  }

Output

The area for the circle 13.847399 

Wrong Syntax

if n > 0 {
printf("i is positive");
}

Example of right statement

if(n > 0) {
 printf("i is positive");
}

Equivalent of the above Syntax

The block braces { } can be omitted if they enclose a single statement.

if(n > 0)
 printf("i is positive");


 Examples of if statement

if else Statement

If-else statement also tests the condition. It executes the if block if condition is true otherwise else block is executed.

  • The if else statement is a conditional branch statement.
  • It tells your program to execute a certain section of code only if a particular test evaluates to true.
  • If boolean Expression evaluates to true, the statements in the block following the if statement is executed.
  • If it evaluates to false, else block is executed.

Syntax if-else Statement

if(condition){
	statement1(s);
	}
else
	{ 
	statement2(s);
	}
 

Flow Diagram

if statement in C

An if-else statement decides which statements to execute based on whether the condition is true or false.The if-else works like this: If the condition is true, then statement1 is executed. Otherwise, statement2 (if it exists) is executed. In no case will both statements be executed.

 if(condition){  
//code if condition is true  
}else{  
//code if condition is false  
}
 

If the boolean expression evaluates to true, the statement(s) for the true case is executed; otherwise, the statement(s) for the false case is executed. For example, consider the following code

 if (radius >= 0) {
area = radius * radius * PI;
printf("The area for the circle is %f ",area);
}
else {
printf("Negative input");
}
 

Practical approach of the above code

 
#include
    void main(){
	double radius, area, PI;
	PI=3.14f;
	radius = -1 ;

	  if (radius >= 0) {
	  area = radius * radius * PI;
      printf("The area for the circle is %f ",area);
     }
     else {
	  printf("Negative input \n");
	}
  }
 
 

Output

 
 Negative input
Press any key to continue . . .

If radius >= 0 is true, the area is computed and displayed; if it is false, the message "Negative input" is displayed. In our program, the value of radius is -1 for that it displayed Negative input.

 Examples of if-else statement

Nested if Statement

A nested if is an if statement which contains another if or else. Nested ifs are very common in programming.

Syntax

The syntax for a nested if...else is as follows ?

if(Boolean_expression 1) {
   // Executes when the Boolean expression 1 is true
   if(Boolean_expression 2) {
      // Executes when the Boolean expression 2 is true
   }
}

Practical Approach of Nested if

#include
    void main(){
	int x =15;
	int y = 20;

	 if( x == 15 ) {
	   if( y == 20 ) {
	      printf("X = 15 and Y = 20\n");
	     }
      }
  }
  }
}
 
X = 15 and Y = 20
Press any key to continue . . .

When you nest ifs, the main thing to remember is that an else statement always refers to the nearest if statement that is within the same block as the else and that is not already associated with an else. Here is an example:

#include
    void main(){
	int a=2, b=3, c=8,d=0;
	int i=15, j=20 , k=300;
	if(i == 15) {
	    if(j< 25) a = b;
	       if(k > 102) c = d; // this if is
	       else a = c; // associated with this else
	 }
    else a = d; // this else refers to if(i == 10)

    printf("%d \n",c);
  }
 
0
Press any key to continue . . .

As the comments indicate, the final else is not associated with if( j < 25) because it is not in the same block (even though it is the nearest if without an else). Rather, the final else is associated with if(i==15). The inner else refers to if(k>102) because it is the closest if within the same block.

 Examples of Nested if-else statement

if else if ladder

A common programming construct that is based upon a sequence of nested ifs is the if-else-if ladder. It looks like this:

The syntax for a if else if ladder as follows ?

if(condition)
 statement;
 else if(condition)
      statement;
      else if(condition)
        statement;
       .
        .
         .
         else
         statement;
if statement in C

The if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the final else statement will be executed. The final else acts as a default condition; that is, if all other conditional tests fail, then the last else statement is performed. If there is no final else and all other conditions are false, then no action will take place.

Practical Approach of if else if Ladder

#include
    void main(){ 
	double score = 55;

	if (score >= 90.0)
	 printf("A \n");
	else if (score >= 80.0)
	 printf("B \n");
	else if (score >= 70.0)
	 printf("C \n");
	else if (score >= 60.0)
	 printf("D \n");
	else
     printf("F \n");
  }
 
Output
 
F
Press any key to continue . . . 
 Examples of if-else-if ladder statement
MCQ Available

There are 6 MCQs available for this topic.

6 MCQ