Control Statements
Control Statements
The three basic control structures in programming are sequence, selection, and loop. These structures form the foundation of how programs
control the flow of execution and make decisiojtns. Let’s explore each one in detail:
Conditional statements (also known as decision-making statements) are programming constructs that allow a program to execute certain blocks
of code based on whether a specified condition is true or false. These statements introduce decision-making capabilities into a program, allowing
the code to choose different paths of execution.
2. Control Flow: Based on whether the condition evaluates to True or False, the program can branch off and execute different blocks of
code. This controls the flow of the program and allows it to make decisions.
1. Sequence
The sequence structure is the simplest form of control flow in programming. It involves executing statements one after another in a linear order.
Every program begins with a sequence structure, where each line of code runs in the order it is written, from top to bottom, without any
branching or repeating.
Key Features:
1. Start: The process begins, indicating the first step in the sequence.
2. Input (Read n1, n2, n3): The program sequentially reads three
numbers (n1, n2, n3). This is the first operation in the sequence.
3. Process (Calculate Average): After reading the numbers, the program
calculates the average using the formula (n1+n2+n3)/3(n1 + n2 + n3) /
3(n1+n2+n3)/3. This is the second operation in the sequence.
4. Output (Write Average): Once the average is calculated, it is
displayed or written as output. This is the third operation in the
sequence.
5. Stop: The process ends after the output is written, marking the final
step in the sequence.
In a sequence statement, every step happens one after the other with no
deviations (such as conditions or loops). This process is linear.
In this diagram, there are no conditional checks (like if statements) or
loops (like for or while). Each step flows naturally to the next, making
it a perfect example of a sequence control structure.
The steps always execute in the same order: input, process, output, and stop.
This structure is fundamental to most programming tasks, ensuring that a
program completes tasks in a predefined, logical order.
This flowchart represents the sequence of steps for adding two numbers.
Here’s how it follows the sequence control structure:
Switch Case
statement
name of If Symbol Explanation
statement
Types of This diagram categorizes loops into two main types based on
Loop when the condition is checked:
statements
1. Entry Controlled Loops:
o In these loops, the condition is checked before
the loop body is executed. If the condition is
false at the start, the loop body may never
execute.
o For loop and While loop are examples of entry-
controlled loops.
For loop: Repeats a block of code for a
fixed number of iterations, with the
condition checked before each iteration.
While loop: Repeats as long as a
condition is true, checking the condition
before entering the loop.
2. Exit Controlled Loops:
o In these loops, the condition is checked after the
loop body is executed. This ensures that the loop
body is executed at least once.
o The Do-while loop is an example of an exit-
controlled loop, where the loop body is executed
first, and then the condition is tested.This
classification highlights how loops differ based
on when the condition is checked, influencing
how often the loop body is executed.
Type of Symbol Explanation
Looping
Statement
for loop This flowchart represents the structure of a for loop that
statement iterates over a sequence of items. Here's how it works:
Once the loop has processed the final item, it exits, and the
program continues beyond the loop.