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

Control Flow Statements in Programming

Module 3 covers control flow statements in programming, including flowcharts, if statements, ternary operators, switch statements, loop statements, and jumping statements. It explains the syntax and usage of each control flow type, such as if-else, while loops, do-while loops, for loops, break, and continue statements. The module provides examples to illustrate how these statements function in code.

Uploaded by

Heng Dalux
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)
8 views16 pages

Control Flow Statements in Programming

Module 3 covers control flow statements in programming, including flowcharts, if statements, ternary operators, switch statements, loop statements, and jumping statements. It explains the syntax and usage of each control flow type, such as if-else, while loops, do-while loops, for loops, break, and continue statements. The module provides examples to illustrate how these statements function in code.

Uploaded by

Heng Dalux
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

Module 3: Control Flow

Instructor: Khouv Tannhuot


Phone: 087 42 47 36 (Telegram)
Control Statement
1. Flowchart
2. If statements
3. Ternary Operator
4. switch Statements
5. Loop Statements
6. Jumping Statements
Flowchart
if statement
● If statement is one of the control flow statement. If statement is
used make decision based on condition.
● In If statement we test a condition, if the condition is true, the
code inside “If block” will be executed and if the condition is false,
the code inside if the condition is ignored.

Syntax:
if (condition) {
statement(s)
}
if-else
● An if statement can be followed by an optional
else statement, which executes when the
Boolean expression is false.
Syntax:
if (condition) {
statement(s)
} else {
Statement(s)
}
if-elseif True

● Use the else if statement to specify a new False


condition if the first condition is false.
True

Syntax:
if (condition) { False

statement(s) True

} elseif (condition) {
Statement(s)
} else {
Statement(s)
}
Ternary Operator
Ternary operator works on three operands. It returns a value based on a condition that
evaluates either true or false statement. True and false statements are also an
expression that you want to evaluate. If the expression is true it evaluates expression
true otherwise it evaluates the expression false.
Ternary operator has three operands: condition ? Expression 1 : Expression 2
1. The condition is evaluated.
2. If the condition is true then Expression 1 is returned.
3. If the condition is false then Expression 2 will be returned.
Example:
x = 5 < 4 ? true : false;
cout << x; // 0 (false)
switch Statements
A switch statement takes an expression and compares its value to successive patterns
given as one or more case blocks.
Loop Statements
In a programming language, a loop means repeating the execution to the instruction or
instruction block with respect to the condition specified by the programmer.

1. while loop
2. do-while loop
3. for loop
while loop
The while loop is similar to the if statement in many ways.
Where as the if statement runs its body once if the condition is True, the while loop
keeps repeating the body as long as the condition is True.

Syntax:
while (condition) {
statement(s)
}
Example: int i = 0;
while (i < 5) {
cout << i << "\n";
i++;
}
Example:
do-while loop
The do-while loop is a variant of the while loop. This loop will execute the code block
once, before checking if the condition is true, then it will repeat the loop as long as the
condition is true.
Syntax:
do {
statements;
} Example:
while (condition); int i = 0;
do {
cout << i << "\n";
i++;
}
while (i < 5);
for loop
When you know exactly how many times you want to loop through a
block of code, use the for loop instead of a while loop:
Syntax:
for (statement 1; statement 2; statement 3) {
// code block to be executed
}
Statement 1 is executed (one time) before the execution of the code
block.
Statement 2 defines the condition for executing the code block.
Statement 3 is executed (every time) after the code block has been
executed.
Jump Statements
Jump statements are used to manipulate the flow of the program if some conditions are
met. It is used to terminate or continue the loop inside a program or to stop the
execution of a function.

1. break
2. continue
Break statement
● You have already seen the break statement used in an earlier. It was used to "jump
out" of a switch statement.
● The break statement can also be used to jump out of a loop.
Example:
for (int i = 0; i < 10; i++) {
if (i == 4) {
break;
}
cout << i << "\n";
}
Continue statement
The continue statement breaks one iteration (in the loop), if a specified condition occurs,
and continues with the next iteration in the loop.
Example:
for (int i = 0; i < 10; i++) {
if (i == 4) {
continue;
}
cout << i << "\n";
}

You might also like