Decision Making and Branching
(if statement)
We have seen that a C program is a set of statements, which are normally executed sequentially in the order in which they appear. This happens when no repetitions of certain calculations are necessary . However, in practice, we have a number of situations where we may have to change the order of execution of statements based on certain conditions are met.
C language possesses such decision making capabilities and supports the following statements known as control or decision-making statements.
1. 1. if statement
2. switch statement
3. Conditional operator statement
4. goto statement
This article will focus on the if statement. Rest all will be discussed in the next article.
Decision making if statement
The if statement is a powerful decision making statement and is used to control the flow of execution of statements. It is basically a two-way decision statement and is used in conjunction with an expression. It takes the following form:
if (test expression)
It allows the computer to evaluate the expression first and then , depending on whether the value of the expression (relation or condition) is ' true ' (non-zero) or ' false ' (zero), it transfers the control to a particular statement. This point of program has two paths to follow , one for the true condition and the other for the false condition.
The if statement may be implemented in different forms depending on the complexity of the conditions to be tested.
1. Simple if statement
2. if...else statement
3. Nested if...else statement
4. else if ladder.
Simple if statement
The general form of a simple if statement is
if (test expression)
{
statement-block;
}
statement-x;
The statement-block may be a single statement or a group of statements. If the test expression is true, the statement-block will be executed; otherwise the statement-block will be skipped and the execution will jump to statement-x. remember, when the condition is true both the statement-block and the statement-x are executed in sequence.
Example:
..........
if (x == 1)
{
y = y +10;
}
printf("%d", y);
.........
The program tests the value of x and accordingly calculates y and prints it. If x is 1 then y gets incremented by 1 else it is not incremented if x is not equal to 1. Then the value of y gets printed.
The if…else statement
The if....else statement is an extension of the simple if statement. The general form is
if (test expression)
{
True-block statement(s)
}
else
{
False-block statement(s)
}
statement-x
If the test expression is true , then the true-block statement(s), immediately following the if statement are executed; otherwise the false-block statement(s) are executed. In either case, either true-block or false-block will be executed, not both.
Example:
........
if (c == 'm')
male = male +1;
else
fem = fem +1;
.......
Nesting of if…else statements
When a series of decisions are involved, we may have to use more than one if.....else statement in nested form as follows:
if (test condition1)
{
if (test condition 2)
{
statement-1;
}
else
{
statement-2;
}
}
else
{
statement-3;
}
statement-x;
If the condition-1 is false statement-3 will be executed; otherwise it continues to perform the second test. If the condition-2 is true, the statement-1 will be evaluated; otherwise the statement-2 will be evaluated and then the control is transferred to the statement-x.
The 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. It takes the following general form:
if (condition 1)
statement 1 ;
else if (condition 2)
statement 2;
else if (condition 3)
statement 3;
else if (condition n)
statement n;
else
default statement;
statement x;
This construct is known as the else if ladder. The conditions are evaluated from the top (of the ladder), downwards. As soon as a true condition is found, the statement associated with is executed and the control is transferred to the statement x (skipping the rest of the ladder).
When all the n conditions become false, then the final else containing the default statement will be executed.
Example :
Let us consider an example of grading the students in an academic institution. The grading is done according to the following rules:
Average marks Grade
80-100 Honours
60- 79 First Division
50- 59 Second Division
40- 49 Third Division
0- 39 Fail
This grading can be done using the else if ladder follows:
if (marks > 79)
grade = "Honours";
else
if (marks >59)
grade = "First Division";
else
if (marks > 49)
grade = "Second Division";
else
if (marks > 39)
grade = "Third division";
else
grade = "Fail";
Don't underestimate the power of if statement. It finds place in almost all programming language with similar cause and effect. Learn to use it carefully and properly to give your program that professional look.
– Ravinder Singh
*ks*
No comments:
Post a Comment