Decision Making and Branching (if statement) by Sachin Mehta SignUp
Boloji.com
Boloji
Home Kabir Poetry Blogs BoloKids Writers Contribute Search Contact Site Map Advertise RSS Login Register
Boloji
Channels

In Focus

Analysis
Cartoons
Education
Environment
Going Inner
Opinion
Photo Essays

Columns

A Bystander's Diary
Business
My Word
PlainSpeak
Random Thoughts

Our Heritage

Architecture
Astrology
Ayurveda
Buddhism
Cinema
Culture
Dances
Festivals
Hinduism
History
People
Places
Sikhism
Spirituality
Vastu
Vithika

Society & Lifestyle

Family Matters
Health
Parenting
Perspective
Recipes
Society
Teens
Women

Creative Writings

Book Reviews
Ghalib's Corner
Humor
Individuality
Literary Shelf
Love Letters
Memoirs
Musings
Quotes
Ramblings
Stories
Travelogues
Workshop

Computing

CC++
Computing Articles
Flash
Internet Security
Java
Linux
Networking
Computing Articles Share This Page
Decision Making and Branching (if statement)
by Sachin Mehta Bookmark and Share
 

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.   

1-Feb-2001
More by :  Sachin Mehta
 
Views: 11000
Article Comment thankyou for this post
pinky
07/20/2014
Article Comment DANGGGGG
a.seenu
10/18/2012
Article Comment in which
a.seenu
10/18/2012
Article Comment Thank u
ramesh
01/13/2012
Article Comment these proved to be very usefull notes for me.these helped me lot for practising for my final exams of sem.
saurabh makkar
11/26/2011
Article Comment we want uses of those statements
priya
10/19/2011
 
Top | Computing Articles







    A Bystander's Diary     Analysis     Architecture     Astrology     Ayurveda     Book Reviews
    Buddhism     Business     Cartoons     CC++     Cinema     Computing Articles
    Culture     Dances     Education     Environment     Family Matters     Festivals
    Flash     Ghalib's Corner     Going Inner     Health     Hinduism     History
    Humor     Individuality     Internet Security     Java     Linux     Literary Shelf
    Love Letters     Memoirs     Musings     My Word     Networking     Opinion
    Parenting     People     Perspective     Photo Essays     Places     PlainSpeak
    Quotes     Ramblings     Random Thoughts     Recipes     Sikhism     Society
    Spirituality     Stories     Teens     Travelogues     Vastu     Vithika
    Women     Workshop
RSS Feed RSS Feed Home | Privacy Policy | Disclaimer | Site Map
No part of this Internet site may be reproduced without prior written permission of the copyright holder.
Developed and Programmed by ekant solutions