0% found this document useful (0 votes)
17 views22 pages

C Control Statements: If and Else

Uploaded by

soumyapanda0078
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views22 pages

C Control Statements: If and Else

Uploaded by

soumyapanda0078
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Programming For Problem Solving (PFPS)

Decision Control/Selection
Statements Using If
Control Statements in C
• In C, programs are executed sequentially in the order of which
they appear.
• This condition does not hold true always. Sometimes a
situation may arise where we need to execute a certain part
of the program.
• Also it may happen that we may want to execute the same part
more than once.
• Control statements enable us to specify the order in which
the various
• instructions in the program are to be executed.
• They define how the control is transferred to other parts of the
program.
2
Classification of Control Statements

3
Selection Statements
• The selection statements are also known as Branching or
Decision Control Statements.
• Sometime we come across situations where we have
to make a decision.
• Decision making structures require the programmer to
specify one or more conditions to be evaluated or tested
by the program, along
• If the condition is determined to be true, a statement or
a set of statements is to be executed
• Otherwise if the condition is found to be false, the other
statement or a set of statements to be executed
4
if Statement
• The if statement allows us to put some decision-making
into our programs.
• The general form of the if statement is:

5
if Statement
• Syntax of if statement:

if (condition )
{
Statement 1;
…………..
Statement n;
}
//Rest of the code

• If the condition is true(nonzero), the statement will be


executed. If the condition is false(0), the statement will not be
executed.
6
Example of if Statement

7
if-else Statement
• The if statement is used to execute only one action. If
there are two statements to be executed
alternatively, then if-else statement is used.
• The if-else statement is a two way branching
statement as another group of statements is
executed if the expression evaluates to false.
• The group of statements after the if is called an ‘if
block’.
• Similarly, the statements after the else form the ‘else
block’.

8
if-else Statement
• The general syntax of simple if - else statement is:

• Where, statement may be a single statement or a


block.
• If we desire that multiple statements be executed on
a branch, we must block them inside of a pair of
opening and closing curly braces to make them a
single compound statement.
9
if-else Statement

10
Example of if-else Statement

11
Example of if-else Statement

12
Nested if-else statement
• An entire if-else construct can be written within either
the body of the if statement or the body of an else
statement.
• This is called ‘nesting’ of ifs. This is shown in the
structure given in the figure for syntax of nested if-else
construct.
• From the syntax it is clear that:
– If test condition – 1 is false, statemet3 is executed
otherwise test condition-2 is tested.
– If test condition – 2 is true, statemet1 is executed
otherwise statemet2 is executed .

13
Syntax of Nested if-else construct

14
Flowchart of Nested if-else construct

15
Example of Nested if-else Statement
Write a program to check whether the given number is even or odd using nested if-else
statement.

16
Example of Nested if-else Statement

17
The if-else-if/else-if Ladder
• When we are faced with a situation in which a program must
select from many processing alternatives based on the value of
a single variable, a multi-way decision statement is required
• In C if-else-if ladder helps user decide from among multiple
options.
• The C if statements are executed from the top down.
• As soon as one of the conditions controlling the if is true, the
statement associated with that if is executed, and the rest of
the C else-if ladder is bypassed.
• If none of the conditions is true, then the final else statement
will be executed.

18
General form of the if-else-if Ladder

19
Flowchart of if-else-if Ladder

20
21
Example of if-else-if Ladder
Write a program to check to check for the relation between two numbers using if-else-if
ladder.

22

You might also like