Conditional statement
The Role of Flowcharts in Algorithm Design
• Flowcharts are graphical representations of algorithms that depict the
logical steps involved in solving a problem or performing a task. They
serve as a visual guide for understanding, planning, and implementing
algorithms.
Key Components of a Flowchart for Algorithm
Design
•Start and End Points:
Every flowchart begins with a start point and concludes with an end point,
denoting the algorithm's initiation and conclusion.
•Processes:
Rectangles represent processes or operations. In algorithm design, these
correspond to specific actions or computations.
•Decisions:
Diamonds symbolize decision points where conditions are evaluated. Based on
the outcome, the algorithm proceeds along different paths.
•Input/Output:
Parallelograms denote input or output operations. In algorithm design, this
includes operations such as reading data or displaying results.
Symbol Description
Start and End Points
Processes or operations
Parallelograms denote input or output
operations including reading data or
displaying results.
Diamonds symbolize decision points
Introduction
• Conditional statements are fundamental programming constructs
that allow the execution of different code blocks based on specified
conditions.
• If Statement
• If-Else Statement
• Nested If-Else Statement
• Switch Statement
if statement if-else if-else-if Nested if-else
if(condition) if(condition) if(condition) if(condition)
{ { { {
//if true //if true // true if(condition 2)
} } } {
else else if(condition 2) if(condition 3)
{ { {
//false // cond 2 true
} } }
else if(condition 3) }
{ }
// cond 3 true else
} {
else }
{
//false
}
if statement
• The if statement allows you to execute a block of code if a specified
condition is true.
• Syntax:
Flowchart of if Statement
Start
false
Condition
true
Statement if
Condition is verified
Exit
If-Else Statement
• The if-else statement provides an alternative block of code to execute
if the condition is false.
• Syntax:
Flowchart of if-else Statement
Start
false
Condition
true
Statement if Statement if ‘Condition’
‘Condition’ is verified is not verified
Exit
Nested If-Else Statement
• Nesting involves placing one conditional statement inside another.
• It allows for multiple levels of decision-making.
• Example:
Nested If-Else Statement
Start
Condition false
1
true Statement if ‘Condition1’
is not verified
Condition false
2
Statement if ‘Condition2’
is not verified
true
Condition false
Statement if ‘Condition n’
n is not verified
Statement if the ‘Conditions’
are verified
Exit
Example
• Write a C program that displays a student's grade based on their
average
Algorithmic notation
Switch Statement
• The switch statement allows to select one of many code blocks to be
executed.
• Useful when there are multiple cases to consider.
• Syntax:
Example of switch Statement
Flowchart of Switch
Switch conditional
Statement
true
Case 1
false Statement;
Break;
true
Case 2
Statement;
false Break;
Statement;
default
Break;
Statement after switch
Exercise
• Write a C program that implements a calculator with basic operations
(+,-,*,/) using if-else, and then using switch-case.