0% found this document useful (0 votes)
106 views

Chapter 4-Decision Making and Branching in C Programs

This document discusses different decision making and branching statements used in C programming. It explains if, if-else, else-if, and switch statements. The if statement executes code if a condition is true, while if-else executes one code block if true and another if false. Else-if is used to check multiple conditions. Switch compares a variable to case values and executes the associated code block. Flowcharts provide examples of how these statements control program flow.

Uploaded by

Falah Alkahteeb
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
106 views

Chapter 4-Decision Making and Branching in C Programs

This document discusses different decision making and branching statements used in C programming. It explains if, if-else, else-if, and switch statements. The if statement executes code if a condition is true, while if-else executes one code block if true and another if false. Else-if is used to check multiple conditions. Switch compares a variable to case values and executes the associated code block. Flowcharts provide examples of how these statements control program flow.

Uploaded by

Falah Alkahteeb
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

Chapter Four

Decision Making and Branching


OBJECTIVES LEARNING OUTCOMES (LO)
7. Explain and apply the control 2. Write a C program, compile it,
structures use in C-programming link it, execute it and debug it.
such as if statement, if else 6. Use branching (if-then-else) and
statement, switch statement, and case selections (switch) in C.
looping/iteration statements. 12. Recognize a program written
9. Capable of designing and in C.
implementing C programs to solve 13. Solve a simple real world
simple and complex problems problems by using programming
in C.
1
•There are a number of decision making
and branching statements that are used
in C programs in order to transfer in
execution from one part to another
depending on a certain condition.
•So with decision making the normal
sequential flow of program execution can
be controlled.

2
These statements are:
1. if statement.
2. if else statement.
3. else if statement.
4. switch statement.

As these statements are used to control


the flow of execution, so they are also
known as control structures/statements.

3
4.1 if statement:
•Used to execute an instruction (either simple
or compound) depending on the result of a
condition.
Syntax:
if ( condition)
{ simple or compound statement;
}
If the result of (condition) is true, the statement
will execute, otherwise it will not.
No need to use { } in case if it works on only one
simple statement. 4
Flowchart of if statement.

5
6
7
8
4.2 if else statement:
•In if statement, if the condition returns
false then no statement will be executed.
•This problem is handled in if else
statement where two statements are
written for both results of the condition
i.e. for True and False.

9
Syntax:
if (condition)
{ simple or compound statement1;
}
else
{simple or compound statement2;
}
If the result of (condition) is true, statement1
will execute, otherwise statement2 will
execute.
No need to use { } in case if or else works on
only one simple statement. 10
Flowchart of if else statement.

11
12
13
14
15
16
17
4.3 else if statement:
-Used to check multiple conditions.

-Based on the result of each condition


one statement (either simple or
compound) will be executed and all
others will be neglected.

18
Syntax:
if (condition1)
{ simple or compound statement1;
}
else if (condition2)
{simple or compound statement2;
}
........
else
{ default simple or compound statement;
}
19
•Starting from the top, the program will
check the conditions.
•If any condition is True the statement
after it will be executed, and all other
statements will be neglected.

•If all conditions are False, the default


statement after last else will be executed.

20
Flowchart of nested else if statement.

21
22
23
24
25
26
27
4.4 switch statement:
• It is also used to check multiple conditions
in order to select one case only out of a
number of cases.
• It is used in programs as a replacement of else
if. But, it has two limitations:

1. Only Equality can be checked in switch statement


and it is automatically done, no need to write a
relational expression using Equal to == operator.

2. It can only be used with int and char data types.


28
29
• Switch compares the value of
(variable/expression) with the values
written after each case.
• It selects and executes the block of
statements that is mentioned after the
case value that is exactly equal to
(variable/expression).
• break statement is used to go out of
switch statement.
30
• If no break is used, switch will
automatically continue to execute the
next block of statements till it finds a
break.
• If no case value is equal to
(variable/expression), the block of
statements that is mentioned after
default will be executed if default is
used.
31
Flowchart of switch statement.

32
33
34
35
36
37
38

You might also like