Two-way branching
It takes the following form If (test-expression)
This point of program has two paths to follow, one for the true condition and the other for the false condition as shown |
Different forms of if statements
The if statement may be implemented in different forms depending on the complexity of conditions to be tested.
1. Simple if statement 2. if ….. else statement 3. Nested if ….else statement 4. else if ladder |
Before discussing the actual structure of the if statement, let us examine the meaning of TRUE and FALSE in computer terminology. A true statement is one that evaluates to a nonzero number. A false statement evaluates to zero. When you perform comparison with the relational operators, the operator will return 1 if the comparison is true, or 0 if the comparison is false.
One of the important functions of the if statement is that it allows the program to select an action based upon the user's input. For example, by using an if statement to check a user-entered password, your program can decide whether a user is allowed access to the program.
|
Properties of if Statement
|
Components of simple-if statement
|
General form of if statement with single Statement
if (test-expression)
statement; NextStatementFollowingIfStatement; Explanation
Here the test-expression can be any valid expression. The test-expression must be placed in the parenthesis. If the expression is true then the Statement is executed. If the expression is not true then the Statement is not executed; instead the program skips past it (the control goes to the NextStatementFollowingIfStatement). The statement can be either simple or compound. Scope of if statement :The default scope of if statement is the immediately next statement after it.
|
Review Questions
1. Write the general form of simple-if statement
with single statement. 2. Write the general form of simple-if st. with multiple statements. 3. Draw the flow of execution of simple-if statement with single statement. 4. Draw the flow of execution of simple-if statement with multiple statements. |
General form of simple-if statement with multiple statements
if (test-expression)
{ Statement-1; Statement-2; ……………… ......................... } NextStatememtFollowingIfStatement; In some programming situation, need to execute more than one statement, if the expression following the keyword if is true. Such multiple statements must be placed within a pair of braces.
If a pair of braces is not used then the C compiler assumes that the programmer wants to execute only the next statement immediately after if, the condition is true. That is, the default scope of if statement is the immediately next statement after it.
|
Various forms of if-else statement
Review Questions
1. Write the general form of if-else statement.
2. Draw the flow of execution of if-else statement. 3. Draw the flow-chart for if-else statement. 4.Define if-block. 5.Define else-block. 6. Write about various forms of if-else statement. |
Properties of if - else Statement
1.The condition is given in parenthesis and must be evaluated as true (non-zero value) or false (zero value) 2.If the condition is true, then the if-block is executed. 3.If the condition is false, the else-block is executed. 4.If a compound structure is provided either in if-block or else-block must be enclosed in opening and closing braces. The if-else statement is an extension of the simple-if statement. The if-else statement is closely related to the simple if statement. The if-else statement is used to carry out a logical test and then take one of two possible actions depending on the outcome of the test. In a program to execute one group of statements if the expression is true and another group of statements if the expression is false. Such situation is handled through the else statement.
General form of if-else statement
if (test expression)
{ if-block statement(s) } else { else-block statement(s) } The test-expression used in the if statement should be specified within parenthesis. If the test-expression is true, then the if-block statements are executed immediately following the if statement. If the test-expression is false, then the else-block statements are executed immediately following the else statement.
|
Nested-ifs are very common in programming. It is possible to nest (i.e., embed) if - else statements, one within another. Write an entire if-else construct within either the if-block of the if statement or an if-else construct occurs within the else-block of the if statement. This is called ‘Nesting’ of ifs.
Note: Any number of if-else statements can be included in a program, but the conditions specified inside each if statement should be different. In a nested if, an else statement always refers to the nearest if statement that is written that is within the same block as the else and that is not already associated with an else.
The test-expression used in the if statement should be specified within parenthesis. If the test-expression is true, then one complete if-else statement will be executed immediately following the if statement. If the test-expression is false, then another complete if-else statement will be executed immediately following the else statement. The statement can be either simple or compound. Note: When nesting, care should be taken to match every if with an else. When a series of decisions are involved, use more than one if-else statement called as nested ifs. Any number of if-else statements can be included in a program, but the conditions specified inside each if statement should be different. Flow-Chart
Various forms of Nested-if Statement
|
Flow of execution
Disadvantages
a) As the number of conditions go on increasing the level of indentation also goes on increasing. As a result, whole program creeps to the right.
b) Care needs to be exercised to match the corresponding ifs and elses. c) Care needs to be exercised to match the corresponding pair of braces. Program in C to find biggest among three given numbers using nested-if
#include <stdio.h>
#include <conio.h> main() { int a, b, c; clrscr(); printf ("Enter a, b and c: "); scanf ("%d %d %d", &a, &b, &c); if (a >b) { if (a>c) printf ("a is biggest"); else printf ("c is biggest"); } else { if (b>c) printf ("b is biggest"); else printf ("c is biggest"); } } Output: Enter a, b and c: 10 5 1 a is biggest Enter a, b and c: 5 10 1 b is biggest Enter a, b and c: 5 1 10 c is biggest |
The if statements are executed from the top down. As soon as one of the test-expressions controlling the ifs is true, the statement associated with that if is executed, and the rest of the ladder is bypassed.
If none of the condition is true, then the final else statement will be executed. The final else acts as a default condition; that is, if all other test-expressions fail then the last else statement is performed. |
A common programming construct that is based upon a sequence of nested-ifs is the if-else-if ladder. There is another way of putting ifs together when multipath decisions are involved. A multipath decision is a chain of ifs in which the statement associated with each else is an if. Various combinations of if and if…else statements can occur and if the situation is complicated, the logic can be difficult to understand.
|
/*Demonstration of else-if ladder */
/* Demonstration of else-if ladder */
#include <stdio.h> #include <conio.h> main ( ) { int m1, m2, m3, m4, m5, per; clrscr(); printf("Enter the marks one by one"); scanf("%d %d %d %d %d", &m1, &m2, &m3, &m4, &m5); per= (m1+m2+m3+m4+m5) / 500 * 100; if (per >= 60) printf("First division"); else if (per >= 50) printf("Second division"); else if (per >= 40) printf("Third division"); else printf("Fail"); getch(); } |