0% found this document useful (0 votes)
21 views16 pages

Control Statements

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

Control Statements

Programming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

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:

Key Concepts of Conditional Statements

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.

Key Concepts of Conditional Statements:


1. Conditions: A condition is an expression that evaluates to either True or False. This could be a comparison, like x > 5, or a boolean
expression, like is_valid == True.

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:

 Linear Execution: Each instruction is executed in the order it appears.


 No Decision-Making: There are no branches or choices in the sequence structure; the program simply follows the steps in the order
provided.
 Basic Building Block: All programs consist of sequences, even if they also include selection or looping structures.
SEQUENCE STATEMENTS

This flowchart and pseudocode represent a classic example of a sequence


control structure in programming. In a sequence statement, instructions are
executed one after another in the order they are written, without branching or
looping.

Here’s how the flowchart follows the sequence statement concept:

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.

Sequence Structure Explanation:

 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:

1. Start: The process begins.


2. Input A, B: The program takes two numbers, A and B, as inputs.
3. C = A + B: The program performs the addition of A and B,
storing the result in C.
4. Print C: The result, C, is printed or displayed.
5. Stop: The process ends.

Sequence Structure Explanation:

 Every step is executed in a specific order, without any conditional


or looping structures.
 The flow is straightforward: input the numbers, compute the sum,
print the result, and stop.

This flowchart is a perfect example of a sequential control structure,


where tasks are performed in a linear, step-by-step manner.
SELECTION STATEMENTS

name of If Symbol Explanation


statement
One decision This flowchart represents the structure of a
…True statement simple if statement, where a condition is
evaluated. If the condition (test expression) is
true, the flow follows the "True" path, and the
block of code inside the "Body of if" is executed.
If the condition is false, the flow follows the
"False" path, skipping the "Body of if" and
continuing to the next part of the program. This
diagram highlights how decisions are made in an
if control structure based on whether the test
condition is satisfied or not.
name of If Symbol Explanation
statement
Two decision state This flowchart illustrates the structure of an if-
else statement, where the program begins by
1. True statement evaluating a condition (expression). If the
2. False statement condition is true, the program follows the "True"
path and executes the statement in the if branch.
If the condition is false, the program follows the
"False" path and executes the statement in the
else branch. After either branch is executed, the
program flow proceeds to the "Stop" point,
marking the end of the decision process. This
flowchart shows how a program can choose
between two actions based on a condition.
name of If Symbol Explanation
statement
Nested if This flowchart represents a nested if-else
statements statement, where decisions are made in layers.
The process begins with evaluating Boolean
expression-1. If this expression is true, the
program follows the "True" path and executes
the Body of if, then checks Nested expression-
1. Depending on whether Nested expression-1
is true or false, it will either execute the nested
Body of if or the Body of else. If Boolean
expression-1 is false, the program takes the
"False" path, executes the Body of else, and
evaluates Nested expression-2, leading to
further decisions and actions based on whether
Nested expression-2 is true or false. After the
nested decisions, the program continues to the
Statement after if block, concluding the
decision-making process. This structure allows
for more complex conditions to be evaluated
sequentially.

Switch Case
statement
name of If Symbol Explanation
statement

This flowchart represents a switch statement, often


used as an alternative to a long chain of if-else
statements when dealing with multiple conditions
based on a single expression. Here's how it works:

1. Begin switch: The program evaluates the


expression inside the switch statement.
2. Case 1: The program checks if the
expression matches the value in case 1.
o If it matches, it executes statement
1 and skips the remaining cases.
3. Case 2: If there is no match with case 1, the
program moves to case 2 and checks for a
match.
o If it matches, the corresponding
statement is executed.
4. Case n: This continues for each case until
the expression matches one of the cases, and
its respective statement is executed.
5. Default statement: If none of the cases
match, the program executes the default
statement (if present).
6. End switch: After a case or the default
statement is executed, the program exits the
switch block.

This flowchart helps visualize how a switch


statement evaluates an expression and executes one
of many possible blocks of code based on matching
a specific case value.
ITERATION OR LOOPING STATEMENTS

Type of Symbol Explanation


Looping
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

While This flowchart represents the structure of a while loop in


statement programming. Here's how it works:

1. Enter while loop: The loop begins with a test


expression that needs to be evaluated.
2. Test Expression: The condition is checked.
o If the expression evaluates to true, the
program enters the loop and executes the
block of statements inside the loop.
o If the expression evaluates to false, the
program skips the loop and exits the while
loop, moving to the next part of the program.
3. Statements: If the test expression is true, the
statements within the loop are executed. After the
statements are executed, the program returns to
evaluate the test expression again.
4. The process repeats: The loop continues as long as
the test expression remains true. Once the
expression becomes false, the loop terminates, and
the program flow moves beyond the loop.

This flowchart clearly demonstrates how a while loop


allows repetitive execution of a set of statements until a
condition becomes false.
Type of Symbol Explanation
Looping
Statement

Do While This flowchart represents a do-while loop, which is a type


Statement of loop in programming that ensures the body of the loop is
executed at least once before the condition is tested. Here’s
how it works:

1. Do while Loop Start: The loop begins, and the


program moves to the next step.
2. Execute Loop Body: The body of the loop is
executed first, regardless of whether the condition is
true or false.
3. Check/Test Condition: After executing the loop
body, the condition is checked.
o If the condition is true, the program returns
to the loop body and executes it again.
o If the condition is false, the program exits
the loop and moves to the Do while Loop
End.
4. Loop Continuation: As long as the condition
remains true, the loop body will continue to execute.
The condition is checked after each iteration of the
loop.

The key difference between a do-while loop and a while


loop is that the do-while loop guarantees at least one
execution of the loop body because the condition is checked
after 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:

1. For each item in sequence: The loop starts by


iterating through each item in a given sequence (like
an array, list, or range).
2. Last item reached?: The loop checks if the last
item in the sequence has been processed.
o If true (the last item is reached), the loop
terminates, and the program proceeds to exit
the loop.
o If false (there are still items remaining), the
program moves on to execute the block of
statements inside the loop.
3. Statements: The code block within the loop is
executed for the current item in the sequence.
4. The process repeats: The loop continues to process
each item in the sequence until all items have been
handled, checking the condition after each iteration.

Once the loop has processed the final item, it exits, and the
program continues beyond the loop.

This flowchart is a good representation of how a for loop


cycles through items in a sequence until it reaches the end.
JUMP STATEMENTS
name of Jump Symbol Explanation
Statement
One decision This diagram represents different types of jump
…True statements used in programming to alter the normal
statement flow of execution in loops, functions, or control
structures. The jump statements include:

1. break: Exits from a loop or switch statement


before the loop or block has completed its
execution.
2. continue: Skips the current iteration of a loop
and moves to the next iteration.
3. return: Exits from a function and optionally
returns a value to the calling function.
4. goto: Transfers control to a labeled statement
within the same function (although generally
discouraged due to making code harder to
follow).

These statements help manage the control flow,


especially in loops and functions, by jumping to different
points in the code based on conditions.
name of Jump Symbol Explanation
Statement
Break This flowchart demonstrates the use of a break statement
Statement inside a loop. Here’s how it works:

1. Enter loop: The loop begins, and the program


proceeds to evaluate the condition.
2. Condition: The condition for the loop is checked.
o If the condition is false, the program exits the
loop.
o If the condition is true, the program
continues to the next step.
3. Break check: Inside the loop, the program checks
whether a break statement is encountered.
o If the break condition is true (i.e., the break
statement is triggered), the program exits the
loop.
o If the break condition is false, the program
executes the body of the loop.
4. Body of loop: The body of the loop executes if there
is no break condition, and the loop continues from
the top to check the condition again.

The break statement is used to terminate the loop


prematurely when a certain condition is met. Once a break is
encountered, the loop is exited, bypassing any remaining
iterations.
name of Jump Symbol Explanation
Statement
Continues This flowchart illustrates the use of the continue
statement statement inside a loop. Here's how it works:

1. Enter Loop: The loop begins, and the program


evaluates the Test Expression of Loop.
2. Test Expression of Loop:
o If the condition is false, the loop is exited,
and the program proceeds to the statement
just below the loop.
o If the condition is true, the program
proceeds further.
3. Continue Check: Inside the loop, the program
checks for a continue statement.
o If the continue condition is met (Yes),
the program skips the remaining body of
the loop and goes back to the top of the
loop to re-evaluate the condition (i.e., it
continues with the next iteration of the
loop).
o If the continue condition is No, the
program executes the remaining body of
the loop and then returns to the top of the
loop for the next iteration.
4. The process repeats: The loop continues until the
condition becomes false, at which point the
program exits the loop and moves on to the
statement just below the loop.

The continue statement is used to skip the current


iteration of the loop and proceed to the next iteration
without executing the remaining statements in the loop
body.
name of Jump Symbol Explanation
Statement
goto Statement his flowchart demonstrates the use of a goto statement
in programming, which causes the program to jump to a
labeled section of the code. Here's how it works:

1. Statement 1: The program starts by executing


statement 1.
2. Statement 2 or Goto:
o Normally, after executing statement 1,
the program would continue with
statement 2.
o However, if a goto Label 3 is
encountered, the program skips statement
2 and jumps directly to Label 3.
3. Label 3: If the goto statement is triggered, the
program jumps to this label, and statement 3 is
executed next.
4. Remaining Code: After executing statement 3,
the program continues with the remaining code
following the labeled section.

In this example, the goto statement causes a direct jump


to Label 3, skipping over statement 2. This can be
useful in certain situations, but it can make code harder
to follow, which is why the use of goto is generally
discouraged in modern structured programming.

You might also like