Flow of Control

Chapter 10 · Tamil Nadu State Board

  • 17 min read
  • 4-question quiz

Every key point below shows the sentence from your textbook it comes from. Book back answers are solved and checked against the book; answers we could not prove are left out. How we check content

In short

This lesson explains how C++ statements run in sequence, make choices, repeat actions, or jump to another part of a program. It introduces selection statements such as if and switch, loops such as for, while, and do-while, and jump statements such as break and continue. Understanding these structures helps students control which instructions a program executes and when. The lesson also explains how loop conditions and updates affect repetition and termination.

Written by AI from this lesson. How we check it

Key points

  • A null statement contains only a semicolon and can serve as a placeholder.
    From your textbook
    “The "null or empty statement" is a statement containing only a semicolon.”
  • A compound statement groups several statements inside braces and is treated as one unit.
    From your textbook
    “The compound statement or block is a treated as a single unit”
  • Selection statements choose which statement or block to execute according to a condition.
    From your textbook
    “The selection statement means the statement (s) executed depend upon a condition.”
  • An iteration statement repeats its body while its condition is true, stopping when the condition becomes false.
    From your textbook
    “As soon as the condition becomes false, the repetition stops.”
  • The for and while loops check their conditions before the body runs, while do-while checks after the body.
    From your textbook
    “The do-while loop is an exit-controlled loop.”
  • A break statement exits a loop, whereas continue skips the remaining body and begins the next iteration.
    From your textbook
    “When the continue statement is executed in the loop, the code inside the loop following the continue statement will be skipped”
Test yourself: 4 questions

Formulas in this chapter

  • Conditional operator syntaxexpression 1? expression 2 : expression 3Explained here
  • General for loop formfor (initialization(s); test-expression; update expression(s)) { Statement 1; Statement 2; } Statement-x;Explained here
  • while loop formwhile ( Test expression ) { Body of the loop; } Statement-x;Explained here
  • do-while syntaxdo { Body of the loop; } while(condition);Explained here

Statements in C++

A C++ program is made of statements that perform actions. These actions include declaring variables, evaluating expressions, assigning values, making decisions and repeating instructions.

Two kinds of statements

KindMeaning
Null statementA statement containing only a semicolon: ;
Compound statementA group of statements enclosed in braces {} and treated as one unit.

A null statement is often used as a placeholder in an iteration statement. A compound statement can contain several statements and may appear anywhere in a program.

Sequential Flow and Control Flow

Definition

Sequential flow
Statements run one after another, once, from top to bottom, without changing the flow of execution.

Definition

Control flow
The flow of execution changes through actions such as branching, iteration, jumping or function calls.

Main ways statements run

  • Sequence: instructions run one after another.
  • Selection: a condition determines which statements run.
  • Iteration: statements run repeatedly while a condition is true.

Control statements alter the sequence in which instructions run. A conditional expression evaluates to true or false. In C++, any non zero value, including a negative number, is true; zero is false.

Selection and Iteration Statements

Definition

Selection statement
A statement that chooses which set of instructions to execute based on a condition.

Definition

Iteration statement
A statement that repeats a set of instructions while its condition is true.

The repeated instructions form the loop body. The condition that controls repetition or stops the loop is called the exit-condition or test-condition. Repetition stops when the condition becomes false.

Selection and iteration

SelectionIteration
Chooses instructions to run based on a condition.Repeats instructions while a condition is true.
Makes a decision about which statements to execute.Stops repeating when the condition becomes false.

if and if-else Statements

The if statement checks an expression. If it is true, the true-block runs; otherwise, that block is skipped. The next statement runs after the if statement in either case. A true-block can be one statement, a compound statement or an empty statement.

if statement form

  • if (expression) true-block;
  • statement-x;

The if-else statement provides two alternatives. If the expression is true, the true-block runs and the false-block is skipped. If it is false, the false-block runs and the true-block is skipped.

if-else statement form

  • if ( expression) { True-block; }
  • else { False-block; }
  • Statement-x

Worked example

Check whether a person aged 23 is eligible to vote using the given if statement.

Try it first, then show the solution
  1. The condition is age>=18.
  2. Since 23 meets the condition, the eligibility message is displayed.
  3. The statement after the if is also executed.

Answer: The person is eligible for voting, and “This statement is always executed.” is displayed.

Worked example

Find whether the number 10 is even or odd using if-else.

Try it first, then show the solution
  1. Calculate rem = num % 2.
  2. For num = 10, rem is 0.
  3. The condition rem==0 is true, so the even-number message is displayed.

Answer: The given number 10 is Even.

Remember: Use if for an action that runs only when a condition is true. Use if-else when one action must run for true and a different action for false.

Nested if Statements

Definition

Nested if
An if statement that contains another if statement is called a nested if.

Three forms of nested if

  • An if statement nested inside the if part.
  • An if statement nested inside the else part.
  • An if statement nested inside both the if part and the else part.

A nested if lets a program check another condition within an if or else branch. In a nested if inside the if part, the inner condition is checked only when the outer condition is true. In a nested if inside the else part, the inner condition is checked when the outer condition is false.

In the commission example, sales greater than 5000 gives a commission of sales * 0.10. Otherwise, the commission is sales * 0.05. For sales of 6000, the output commission is 600, followed by “Good Job .....”.

Remember: The outer condition determines which branch runs; only then can a nested condition in that branch be checked.

if-else-if Ladder

Definition

if-else-if ladder
An if-else-if ladder is a multi-path decision statement with one or more else if branches and a final else branch.

The conditions are checked in order. When one condition is true, its associated statement runs and the rest of the ladder is bypassed. If every condition is false, the final else statement runs.

Grade conditions in the example

  • marks >= 60 displays “Your grade is 1st class !!”.
  • marks >= 50 && marks < 60 displays “your grade is 2nd class !!”.
  • marks >= 40 && marks < 50 displays “your grade is 3rd class !!”.
  • If none of these conditions is true, “You are fail !!” is displayed.

Worked example

What message does the grade program display when marks are 60?

Try it first, then show the solution
  1. The first condition, marks >= 60, is true.
  2. The program displays “Your grade is 1st class !!”.
  3. The rest of the ladder is bypassed.

Answer: Your grade is 1st class !!

Conditional Operator ?:

Definition

Conditional operator
The conditional operator, also called the Ternary operator, is an alternative to an if else statement and uses the two symbols ?:.
Conditional operator syntax

expression 1? expression 2 : expression 3

expression 1 is the condition. If it is true (Non-zero), expression 2 is selected; otherwise, expression 3 is selected.

The operator takes three arguments. In the greatest-number example, largest = (a>b)? a : b selects a when a>b is true; otherwise, it selects b. For inputs 12 and 98, the largest number is 98.

Remember: For expression 1? expression 2 : expression 3, a true condition selects expression 2 and a false condition selects expression 3.

Switch Statement and if-else Comparison

Definition

Switch statement
A switch statement is a multi-way branch statement that sends execution to code based on the value of an expression.

The switch expression is compared with the constant values in its case statements. A matching case runs; if no case matches, the default statements run. The switch statement can replace multiple if-else choices.

Switch statement rules

  • The switch expression should result in a constant value.
  • Duplicate case values are not allowed.
  • The default statement is optional.
  • break terminates the switch and moves control to the next line after it.
  • break is optional. Without it, execution continues into the next case until a break is reached.
  • Switch statements can be nested.

if-else and switch

if-elseswitch
The expression decides whether to execute the if block or the else block.The expression decides which case to execute.
Uses multiple statements for multiple choices.Uses one expression for multiple choices.
Checks equality and logical expressions.Checks only for equality.
Can evaluate integer, character, pointer, floating-point, or Boolean type.Evaluates only character or integer data type.
If the condition is false, the else block statements run.If no case matches, the default statements run.

In the weekday example, num is matched with cases 1 through 7. When num is 6, the program displays Friday. The if statement is more flexible than the switch statement.

Remember: Use break to stop a switch from falling through into the next case.

Iteration and the parts of a loop

Definition

Iteration
Iteration, or looping, repeatedly runs one or more statements until a condition is satisfied.

Four parts of a loop

  • Initialization expression: sets the control variable before the loop begins. It runs only once at the start.
  • Test expression: decides whether the loop body runs or the loop ends.
  • Update expression: changes the loop variable after the body runs.
  • Body of the loop: the statement or statements that are repeated.

When the test expression is checked

Entry-controlled loopExit-controlled loop
The test expression is checked before the body runs.The body runs first, then the test expression is checked.
If the test is nonzero, the body runs; if it is zero, the loop ends.The body repeats if the test is true; otherwise, the loop ends.

Remember: A loop needs a test to control repetition, a body to do the repeated work, and usually initialization and updating of its control variable.

The for loop and its execution

The for loop is an entry-controlled loop. Its initialization, test and update parts are separated by semicolons. Initialization happens once. If the test is true, the body runs, then the update runs, and the test is checked again.

General for loop form

for (initialization(s); test-expression; update expression(s)) { Statement 1; Statement 2; } Statement-x;

The initialization sets starting values, the test-expression controls repetition, the update expression changes values, and the statements in braces form the body.

Worked example

Display numbers from 0 to 9 using a for loop.

Try it first, then show the solution
  1. Set i to 0.
  2. Check whether i < 10.
  3. When the test is true, display i and then increment i.
  4. Repeat the test and stop when i becomes 10.

Answer: The program displays the values of i from 0 to 9.

Worked example

Find the sum of the numbers from 1 to 10 using a for loop.

Try it first, then show the solution
  1. Start with sum=0 and set i to 1.
  2. While i<=10, add i to sum.
  3. Increase i after each addition.

Answer: The sum of 1 to 10 is 55.

Remember: In an entry-controlled for loop, the test is checked before the body. If the test is false, the body is skipped.

For loop variations and special cases

Useful variations

  • The initialization part can contain multiple statements separated by commas. They are evaluated in order.
  • The update part can contain multiple statements separated by commas. They are evaluated in order.
  • The initialization, test and update expressions are optional. If initialization or update is absent, control passes to the conditional part.
  • When an increment or decrement operator is used alone in an update expression, prefer the prefix operator over the postfix operator because prefix operators are executed faster.

Worked example

Sum the numbers from 1 to n when the initialization and update expressions are outside the for loop.

Try it first, then show the solution
  1. Read n, set sum=0, and set i=1 before the loop.
  2. Use for ( ; i<=n; ) and add i to sum inside the body.
  3. Increment i inside the body until the test becomes false.

Answer: For the shown input n=5, the sum of 1 to 5 is 15.

Special for loops

Loop typeWhat happens
Infinite loopIf the test-expression is absent, the loop continues infinitely. for( i=0 ; ; ++i) is an example.
Infinite loopWhen all three expressions are skipped, as in for( ; ; ), the statement is displayed infinitely.
Empty loopA loop with a null statement as its body is an empty loop. A semicolon immediately after the for loop makes its body a null statement.

A variable declared inside a for loop can be accessed only within the loop body. A variable declared inside the block of main() can be accessed anywhere inside main().

The while loop

Definition

while loop
A while loop repeats its body as long as its condition is true.
while loop form

while ( Test expression ) { Body of the loop; } Statement-x;

The test expression is checked before the body runs. If it is true, the body runs; otherwise, control moves to Statement-x.

Remember: A while loop is entry-controlled because its test expression is checked before entering the loop body.

while Loop

A while loop checks its condition before running the body. If the condition is true, the body runs and control returns to the condition. When the condition is false, the loop ends and the next statement runs.

How a while loop works

  • Check the condition.
  • If it is true, run the loop body.
  • Update the value used by the condition.
  • Check the condition again. Stop when it is false.

A program uses a while loop to add numbers from 1 to 10. It starts with i=1 and sum=0, then adds i to sum and increases i by 1 each time. The output is The sum of 1 to 10 is 55.

A while loop can also read 5 numbers, add them, and find their average. With the input values 1, 2, 3, 4, and 5, the sum is 15 and the average is 3.

While loop variations

VariationWhat happens
Empty loopThe body contains no useful statement, only a null statement such as a semicolon. It can be used as a time delay loop.
Infinite loopIf the loop body does not update the value that controls the condition, the condition may remain true and the loop may keep running.

Remember: A while loop checks its condition before the body, so its body might not run at all.

do-while Loop

Definition

do-while loop
A do-while loop is an exit-controlled loop because it checks its condition after running the body.

The body runs before the condition is checked. So it runs at least once, even if the condition is false at the first check.

do-while syntax

do { Body of the loop; } while(condition);

The body runs first. Then condition is checked; the loop repeats while it is true.

A program displays numbers from 10 to 1 using a do-while loop. It displays n, decreases n by 1, and checks whether n>0. The loop stops when n becomes 0.

Remember: A do-while loop always runs its body at least once.

Nesting of Loops

Definition

Nested loop
A loop that contains another loop is called a nested loop.

A loop can be placed inside another for loop, while loop, or do-while loop. The inner loop runs as part of the outer loop.

A program uses nested for loops to display a multiplication table. The outer loop selects each row from 1 to 9, and the inner loop displays its products from 1 to 9.

Jump Statements and goto

Definition

Jump statements
Jump statements interrupt the normal flow of a program.

Types of jump statements

  • goto statement
  • break statement
  • continue statement

Definition

goto statement
A goto statement transfers program control from one place to another without a condition.

A label is an identifier followed by a colon. When the program reaches goto label;, control jumps to that label and continues with the code below it. The jump can go forward or backward.

A program displays the first five odd numbers using a label called jump. It starts with n=1, displays n while n<10, increases n by 2, and jumps back to jump. The output is 1, 3, 5, 7, 9.

Remember: goto transfers control directly to a named label.

break Statement in C++

Definition

break statement
A jump statement that ends the loop immediately and moves control to the statement after the loop.

A break statement can be used in for, while and do-while loops. When its condition is met, the loop ends at once. Any remaining loop iterations are skipped.

Worked example

What does the do-while program display?

Try it first, then show the solution
  1. count starts at 1, so the program displays Count : 1.
  2. It then displays Count : 2 and Count : 3 as count increases.
  3. When count becomes 4, count > 3 is true, so break ends the loop.

Answer: Count : 1, Count : 2, Count : 3

Remember: break takes control out of the loop.

continue Statement in C++

Definition

continue statement
A jump statement that skips the rest of the current loop iteration and begins the next iteration.

continue does not end the loop. It skips the code that follows it in the current iteration. The loop then moves on to its next iteration.

Worked example

What does the for loop display when it prints numbers from 1 to 10 except 6?

Try it first, then show the solution
  1. The loop checks the values of i from 1 to 10.
  2. When i == 6, continue skips the cout statement for that iteration.
  3. The other values are printed.

Answer: 1 2 3 4 5 7 8 9 10

Remember: continue skips one iteration; it does not exit the loop.

Difference Between break and continue

How the two jump statements work

breakcontinue
Ends the loop.Does not end the loop.
Moves control to the statement immediately after the loop.Moves control to the next loop iteration.
Breaks out of the loop.Skips the current iteration.
Can be used with loops and switch case.Can be used only in loops, not in switch case.

Quick way to remember

  • Use break when the loop must stop.
  • Use continue when only the current iteration must be skipped.

Questions students ask

Short, direct answers to what students search for in this chapter.

What is a null statement in C++?

A null statement in C++ contains only a semicolon. It is written as ;. Null statements are commonly used as placeholders in iteration statements or as statements on which to place labels at the end of compound statements or functions.

What is a compound statement in C++?

A compound statement, also called a block, is a group of statements enclosed by a pair of braces {}. The group is treated as a single unit and may appear anywhere in a program.

What is the difference between selection and iteration statements?

A selection statement uses a condition to choose which set of statements to execute. An iteration statement repeats a set of statements while its condition is true. The repetition stops when the condition becomes false.

How does an if-else statement work in C++?

An if-else statement checks an expression. If it is true, the true-block runs and the false-block is skipped. If it is false, the false-block runs and the true-block is skipped. The statement after the if-else runs after either choice.

How can an if-else statement check whether a number is even or odd?

Find the remainder using rem = num % 2. If rem==0 is true, the number is even. Otherwise, it is odd. For example, when the number is 10, the remainder is 0, so the number is even.

What is a nested if statement?

A nested if statement is an if statement that contains another if statement. The inner if can be placed inside the outer if part, the outer else part, or both parts. The outer condition determines which branch is followed.

Show 18 more questions

How does an if-else-if ladder work?

An if-else-if ladder checks its conditions in order. When a condition is true, its associated statement runs and the remaining branches are bypassed. If none of the conditions is true, the final else statement runs.

How does the conditional operator ?: work?

The conditional operator uses the form expression 1? expression 2 : expression 3. It evaluates expression 1 as a condition. If the condition is true (Non-zero), expression 2 is selected; otherwise, expression 3 is selected.

What does break do in a switch statement?

A break statement terminates the switch and moves control to the next line following it. If break is omitted, execution continues into the next case and falls through to later cases until a break is reached.

What is the difference between if-else and switch?

if-else can check equality and logical expressions, while switch checks only for equality. if-else can evaluate integer, character, pointer, floating-point, or Boolean type. switch evaluates only character or integer data type. The if statement is more flexible.

What is iteration in C++?

Iteration, or looping, repeatedly executes one or more statements until a condition is satisfied. C++ supports for, while and do-while iteration statements. A loop repeats its statements while its loop-control condition is true.

What are the four parts of a loop?

The four parts are the initialization expression, test expression, update expression and body of the loop. Initialization sets the control variable at the beginning. The test controls whether the loop continues, the update changes the variable, and the body contains the repeated statements.

How does a for loop work in C++?

A for loop initializes its control variable once, then checks its test expression. If the test is true, the loop body runs and the update expression changes the variable. The test is checked again. When the test is false, control moves out of the loop.

What is an empty loop in C++?

An empty loop is a loop with no statement in its body. A semicolon immediately after the for loop can act as its null body. In that case, the loop body does not contain work to execute, and a statement after the loop runs once the loop ends.

Why is a while loop called entry-controlled?

A while loop is called entry-controlled because its test expression is evaluated before the loop body runs. If the test is true, the body executes. If the test is false, the loop ends without entering its body.

How does a while loop work?

A while loop checks its condition before running its body. If the condition is true, the body runs and control returns to the condition. The loop ends when the condition becomes false.

What is the difference between a while loop and a do-while loop?

A while loop checks its condition before running the body, so the body may not run. A do-while loop checks its condition after running the body, so the body runs at least once.

What is a nested loop?

A nested loop is a loop inside another loop. For example, nested for loops can display a multiplication table: the outer loop selects a row, while the inner loop displays the products in that row.

What does the goto statement do?

The goto statement transfers control to a named label in a program without checking a condition. The label is an identifier followed by a colon, and the program continues with the code below that label.

What is an infinite while loop?

An infinite while loop keeps running when its condition stays true. This can happen if the loop body does not update the value used by the condition, so the loop never reaches a stopping condition.

What does the break statement do in C++?

The break statement immediately terminates the loop in which it appears. Control then moves to the statement after the loop. It can be used with for, while and do-while loops, and also with switch case.

What does the continue statement do in a loop?

The continue statement skips the remaining code in the current loop iteration. It does not end the loop. Instead, control moves to the next iteration, where the loop continues.

What is the difference between break and continue?

break ends the loop and sends control to the statement after it. continue skips the rest of the current iteration and moves to the next one. break can also be used in switch case, while continue is used only in loops.

How do you print numbers from 1 to 10 except 6 using continue?

Use a loop that checks values from 1 to 10. Inside the loop, write if (i == 6) continue; before the print statement. The output is 1 2 3 4 5 7 8 9 10.

Book back answers

9 textbook exercises, solved and checked against the book. Questions we could not prove are left out.

SECTION – A Choose the correct answer

  1. What is the alternate name of null statement?

    • (A) No statement
    • (B) Empty statement
    • (C) Void statement
    • (D) Zero statement
    Try it, then show the answer

    (B) Empty statement

    The chapter refers to a null statement as a null or empty statement.

    From the book: “The "null or empty statement" is a statement containing only a semicolon.”

  2. In C++, the group of statements should be enclosed within:

    • (A) { }
    • (B) [ ]
    • (C) ( )
    • (D) < >
    Try it, then show the answer

    (A) { }

    A compound statement is a group of statements enclosed by a pair of braces.

    From the book: “C++ allows a group of statements enclosed by pair of braces {}.”

  3. The set of statements that are executed again and again in iteration is called as:

    • (A) condition
    • (B) loop
    • (C) statement
    • (D) body of loop
    Try it, then show the answer

    (D) body of loop

    The chapter calls the set of statements executed repeatedly the body of the loop.

    From the book: “The set of statements that are executed again and again is called the body of the loop.”

  4. The multi way branch statement:

    • (A) if
    • (B) if … else
    • (C) switch
    • (D) for
    Try it, then show the answer

    (C) switch

    The switch statement is described as a multi-way branch statement.

    From the book: “The switch statement is a multi-way branch statement.”

All 9 book back answers →

Practice

Question 1 of 4Score 0

What does a null statement contain?

Exam questions

Practice questions with model answers. These are not from an official paper.

What is the difference between a null statement and a compound statement?2 marks

A null statement contains only a semicolon. A compound statement is a group of statements enclosed by braces and treated as a single unit.

How does an if-else statement choose which block to execute?2 marks

It evaluates its condition. If the condition is true, it executes the true-block and skips the false-block; if false, it executes the false-block and skips the true-block.

Distinguish between break and continue in a loop.2 marks

break terminates the loop and transfers control to the statement after it. continue does not leave the loop; it skips the remaining code in that iteration and begins the next iteration.

Why does a do-while loop execute its body at least once?2 marks

It checks its condition at the bottom of the loop, after the body has run. Therefore, the body runs once even if the condition is false at the first check.

Watch out

  • Do not confuse break with continue: break leaves a loop, while continue moves to its next iteration.
  • Do not assume every loop checks its condition before running the body; do-while checks after the body.

Still stuck on this lesson?

Get an AI answer grounded in your actual textbook — with the exact page reference.

Ask AI about this topic →