Control Statements in C programming (if and else)

Control Statements

Introduction

A control statement is a statement that causes program statements to execute in a manner that
doesn’t relate to the order it appears in the source code. These statements help to jump from one
part of the program to another. The control transfer may be conditional or unconditional. C
supports three types of control statements namely selection, repetition and jump statements.
Selection Statements
These are also known as branching, decision making, or conditional statements. These
statements execute program statements depending upon a given condition. C supports two
selection statements: if and switch. There are different forms of if statements: if only, if-else- if,
and if-else- if ladder.
The if only statement
Allows your program to execute a single statement, or a block of statements enclosed between
braces, if a given condition is true. Its general form (syntax) is:
if (expression)
statement 1
OR
if (expression)
{
statement 1
statement 2

statement n
}
If expression evaluates to true (any non-zero value), statement or the block of statements
enclosed between braces is executed. If expression evaluates to false (0), these are not executed.
In either case, execution then passes to whatever code follows the if statement. Expression can
be any valid C expression and statement can be simple or compound or even another control
statement. For example,
if(amount >= 1000)
discount = amount * 0.05;
The if else statement
When there is a need of executing a statement or a block of statements enclosed between braces
if a condition is true and another statement or a block of statements enclosed between braces to
be executed otherwise, then we use if–else statements. Its syntax is:
if (expression)
statement 1else
statement 2
OR
if (expression)
{
statement 1
statement 2

statement n
}
else
{
statement 1
statement 2

statement n
}
If expression evaluates to true (any non-zero value), statement or the block of statements in the
if body is executed. If expression evaluates to false (0), statement or the block of statements in
the else body is executed. For example,
if(amount >= 1000)
discount = amount * 0.05;
else
discount = amount * 0.03;
Using if-else, you can specify an action to be performed both when the condition is true and
when it is false.
The if else statement resembles with the conditional operator (?:). Conditional operator is a
ternary operator (demands 3 operands), and is used in certain situations, replacing if-else
statement. Its general form is:
exp1 ? exp2 : exp3;
If exp1 is true, exp2 is executed and whole expression is assigned with value of exp2. If the exp1
is false, then exp2 is executed, and whole expression is assigned with exp2 value. For example,
c = a > b ? a+b : a-b;
is equivalent to
if(a>b)
c=a+b;
else
c=a-b;

The if else if ladder statement
If we have the situation where there are different actions that are executed depending upon
different conditions with same type of instance then if-else- if ladder is useful. Its syntax is:
if (expression)
statement 1
else if
statement 2
………….
else
statement n
OR
if (expression)
{
statement 1
statement 2

statement n
}
else if
{
statement 1
statement 2

statement n
}
…………………..
else
{
statement 1
statement 2

statement n
}
The conditions are evaluated from the top downward. As soon as a true condition is found, the
statement associated with it is executed and the rest of the ladder is bypassed. If none of the
conditions are true, the final else is executed. If the final else is not present, no actions take place
if all other conditions are false. For example,
if(amount >= 5000)
discount = amount * 0.1;
else if (amount >= 4000)
discount = amount * 0.07;
else if (amount >= 3000)
discount = amount * 0.05;
else
discount = amount * 0.03;

Other C Programming:

No comments

Powered by Blogger.