C Decision Making - Branching

The If else construct

	If (condition) 
	Program statement 1; 
	Else 
	Program statement 2;
The if else is actually just on extension of the general format of if statement. If the result of the condition is true, then program statement 1 is executed, otherwise program statement 2 will be executed.
If any case either program statement 1 is executed or program statement 2 is executed but not both when writing programs this else statement is so frequently required that almost all programming languages provide a special construct to handle this situation.

If else construct

	#include ' stdio.h ' 
    void main ( ) 
    { 
	   int num; 
	   printf (“Enter the number”); 
	   scanf (“%d”, &num); 
	   if (num < 0) // check whether number is less than //zero. 
	   printf (“The number is negative”) //result if true
	    else // else statement. 
		printf (“The number is positive”); //result if false
} 
        


Program description

In the above program the If statement checks whether the given number is less than zero.
If it is less than zero then it is negative therefore the condition becomes true then the statement The number is negative is executed.
If the number is not less than zero the If else construct skips the first statement and prints the second statement declaring that the number is positive.

 

Make Comments..!!


Oops!! No posts from user.

Download Android App