C Decision Making - Branching

Compound Relational tests

Syntax 
	a> if (condition1 && condition2 && condition3) 
	b> if (condition1 // condition2 // condition3)
        
The syntax in the statement ‘a’ represents a complex if statement which combines different conditions using the and operator in this case if all the conditions are true only then the whole statement is considered to be true. Even if one condition is false the whole if statement is considered to be false.
The statement ‘b’ uses the logical operator or (//) to group different expression to be checked. In this case if any one of the expression if found to be true the whole expression considered to be true


Nested if Statement



Syntax: if (condition1) if (condition2) statement-1; else statement-2; else statement-3; if statement may be nested as deeply as you need to nest it. One block of code will be executed if two conditions are true. Condition 1 is tested first and then condition 2 is tested. The second if condition is nested in the first. The second if condition is tested only when the first condition is true else the program flow will skip to the corresponding else statement.

Nested if Statement

#include ' stdio.h ' 
main ( ) 
{ 
    int a,b,c,big; 
	printf (“Enter three numbers”); 
	scanf (“%d %d %d”, &a, &b, &c); 
	if (a>b) // check whether a is greater than b if true then 
		if(a>c) // check whether a is greater than c 
				big = a ; // assign a to big 
    	else 
		        big = c ; // assign c to big 
		else if (b>c) // if (a>b) fails check whether b>c 
				big = b ; // assign b to big 
	    else 
		        big = c ;
				
				printf (“Largest of %d,%d&%d = %d”, a,b,c,big); 
}
        


Program description

In the above program the statement if (a>c) is nested within the if (a>b). If the first If condition if (a>b) If (a>b) is true only then the second if statement if (a>b) is executed. If the first if condition is executed to be false then the program control shifts to the statement after corresponding else statement.

 

Make Comments..!!


Oops!! No posts from user.

Download Android App