Previous | Home | Next |
Decision control structure
In C programming we want the program to be executed sequentially. we want a set of instruction to be executed at one time and an entirely set of instruction to be executed in another situation. in this type of situation will have to use the decision control structure.
If statement
Like most languages c uses the keyword 'if' to implement the decision control instruction. The general form of the 'if' statement is given below:
if this condition is true) execute the statement;
the keyword if tells the compiler to follow the decision control instruction. Any condition which follow the if keyword, always enclosed within pair of parentheses. We express a condition using C’s relational operators which allow us to compare two values to see whether they are equal, unequal or one is greater than other. We can explain these operators like below:
this expression is true if x == y x is equal to y x != y x is not equal to y x < y x is less than y x > y x is greater than y x <= y x is less than or equal to y x >= y x is greater than or equal to y
here is an example which shows the use of 'if' and relation operator.
/* Demonstration of if statement */ main( ) { int num ; printf ( "Enter a number less than 10 " ) ; scanf ( "%d", &num ) ; if ( num <= 10 ) printf ( "What an obedient servant you are !" ) ; }
Explanation- when we execute the above program then if you type the number less than 10 then you will get a message on screen through printf(). If you type the number which is greater than 10 then program will do nothing. As we generally mention the form of ‘if’ as below:
if(condition) statement; but trually speaking the form of ‘if’ is given bellow if(expression) statement;
If-else condition
The if statement by itself will execute the single statement or a group of statement when the expression follow ‘if’ evaluates true and another expression with in single group of statement follow’if’ evaluates falls then we generally use th ‘if-else’ condition.
To understand the ‘if-else’ condition we can take an example which is given below:
/* Calculation of gross salary */ main( ) { float bs, gs, da, hra ; printf ( "Enter basic salary " ) ; scanf ( "%f", &bs ) ; if ( bs < 1500 ) { hra = bs * 10 / 100 ; da = bs * 90 / 100 ; } else { hra = 500 ; da = bs * 98 / 100 ; } gs = bs + hra + da ; printf ( "gross salary = Rs. %f", gs ) ; }
Nested 'if-else'
It is perfectly all right if we write an entire if-else construct within either the body of the if statement or the body of an else statement. This is called ‘nesting’of ifs. This is shown in the following program.
/* A quick demo of nested if-else */ main( ) { int i ; printf ( "Enter either 1 or 2 " ) ; scanf ( "%d", &i ) ; if ( i == 1 ) printf ( "You would go to heaven !" ) ; else { if ( i == 2 ) printf ( "Hell was created with you in mind" ) ; else printf ( "How about mother earth !" ) ; } }
Multiple statement within 'if'
It may so happen that in a program we want more than one statement to be executed if the expression following if is satisfied. If such multiple statements are to be executed then they must be placed within a pair of braces as illustrated in the following example.
/* Calculation of bonus */ main( ) { int bonus, cy, yoj, yr_of_ser ; printf ( "Enter current year and year of joining " ) ; scanf ( "%d %d", &cy, &yoj ) ; yr_of_ser = cy - yoj ; if ( yr_of_ser > 3 ) { bonus = 2500 ; printf ( "Bonus = Rs. %d", bonus ) ; } }
Observe that here the two statements to be executed on satisfaction of the condition have been enclosed within a pair of braces. If a pair of braces is not used then the C compiler assumes that the programmer wants only the immediately next statement after the if to be executed on satisfaction of the condition. In other words we can say that the default scope of the if statement is the immediately next statement after it.
Form of 'if'
The if statement can take any of the following forms:
(a) if ( condition ) do this ; (b) if ( condition ) { do this ; and this ; } (c) if ( condition ) do this ; else do this ; (d) if ( condition ) { do this ; and this ; } else { do this ; and this ; } (e) if ( condition ) do this ; else { if ( condition ) do this ; else { do this ; and this ; } } (f) if ( condition ) { if ( condition ) do this ; else { do this ; and this ; } } else do this ;
'Else if' condition
else if condition is nothing but this this a way to arrange the else with the if that follows it.
if ( i == 2 ) printf ( "With you…" ) ; else if(j==2) { printf("…All the time"); }
Previous | Home | Next |