Society & Lifestyle
|Computing||Share This Page|
Decision Making and Branching (if statement)
|by Sachin Mehta|
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.
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:
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.
Simple if statement
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.
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
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.
Nesting of if…else statements
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
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.
Average marks Grade
This grading can be done using the else if ladder follows:
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.
|More by : Sachin Mehta|
|Views: 12135 Comments: 6|
Comments on this Article
07/20/2014 12:39 PM
10/18/2012 07:49 AM
10/18/2012 07:47 AM
01/13/2012 03:07 AM
11/26/2011 02:37 AM
10/19/2011 08:43 AM
|Top | Computing|