Python Control Structures

Chapter 6 · Tamil Nadu State Board

  • 11 min read
  • 5-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 Python control structures direct the order in which program statements run. Sequential statements run one after another, branching statements choose actions according to conditions, and loops repeat code. It also teaches how indentation defines code blocks and how break, continue, and pass affect program flow. These structures help programmers make programs choose, repeat, or skip actions as needed.

Written by AI from this lesson. How we check it

Key points

  • A control structure changes the flow of a program, and the lesson groups control flow into sequential, branching, and iterative forms.
    From your textbook
    “Three types of flow of control are o Sequencing o Branching or Alternative o Iteration”
  • Sequential statements run one after another.
    From your textbook
    “A sequential statement is composed of a sequence of statements which are executed one after another.”
  • Branching statements use conditions to choose which code block runs.
    From your textbook
    “the condition determines which BLOCK is to be executed.”
  • Python indentation shows which statements belong to a code block.
    From your textbook
    “in Python, it is required to indicate to which block of code the statement belongs to.”
  • A while loop repeats its block while its condition is true; its optional else block runs when the condition becomes false.
    From your textbook
    “The statements block1 is kept executed till the condition is True. If the else part is written, it is executed when the condition is tested False.”
  • A for loop goes through items in a sequence, while range() can generate a series of values for it.
    From your textbook
    “The for .... in statement is a looping statement used in Python to iterate over a sequence of objects”
  • break ends its loop, continue skips to the next iteration, and pass performs no operation.
    From your textbook
    “break, continue, pass act as jump statements in Python.”
Test yourself: 5 questions

Formulas in this chapter

Python Control Structures

Python normally runs statements one after another. Control structures change this flow when a condition must choose what happens next or when statements must be repeated.

Definition

Control structure
A program statement that moves control from one part of a program to another is called a control structure or control statement.

Three types of control flow

TypeWhat it does
SequentialRuns statements one after another.
Alternative or BranchingChooses a path based on a condition.
Iterative or LoopingRepeats a set of statements.

Sequential Statements

A sequential statement is a group of statements that run in order, one after another. Printing a name and address is one example.

Worked example

Print a name and address using sequential statements.

Try it first, then show the solution
  1. Run print ("Hello! This is Shyam").
  2. Then run print ("43, Second Lane, North Car Street, TN").

Answer: Hello! This is Shyam 43, Second Lane, North Car Street, TN

Alternative and Branching Statements

Branching lets a program choose what to do based on a condition. Python provides simple if, if..else, and if..elif statements.

Branching forms

StatementHow it works
Simple ifRuns its statement block only when the condition is true. If the condition fails, it does nothing in that block.
if..elseRuns one block when the condition is true and another when it is false.
if..elif..elseChecks conditions in order. It runs the matching block, or the else block if no condition matches.

A condition in an if statement should be a relational or logical expression. In Python, indentation shows which statements belong to a block. The statements in a block are indented four spaces in the examples.

Short form of if..else

variable = variable1 if condition else variable 2

If condition is true, variable1 is assigned to variable; otherwise, variable2 is assigned.

Worked example

Check whether an entered number is odd or even.

Try it first, then show the solution
  1. Read the number into a.
  2. Check whether a%2==0.
  3. If true, print that the number is even; otherwise, print that it is odd.

Answer: For 56, the output is “56 is an even number”. For 67, the output is “67 is an odd number”.

Worked example

Give a grade using the average of two subject marks.

Try it first, then show the solution
  1. Read m1 and m2.
  2. Calculate avg= (m1+m2)/2.
  3. If avg>=80, print Grade : A.
  4. Otherwise, check the elif conditions for grades B, C, and D; if none match, print Grade : E.

Answer: For marks 34 and 78, the output is Grade : D. For marks 67 and 73, the output is Grade : B.

Membership checks in if statements

  • Use in to check whether a value is in a group. For example, ch in ('a', 'A', 'e', 'E', 'i', 'I', 'o', 'O', 'u', 'U') checks whether ch is a vowel.
  • Use not in to check whether a value is not in a group. For example, ch not in ('a', 'b', 'c') checks whether ch is not one of those letters.

Remember: In if..elif..else, put the else clause at the end. Indentation is required in Python because it shows which block each statement belongs to.

Python Looping Constructs

A loop repeats a statement or group of statements. It can repeat while a condition is true or go through items in a sequence. Python has two looping constructs: while and for.

while loop and for loop

while loopfor loop
Repeats while its condition is True.Goes through each item in a sequence.
The condition is checked before the loop body runs.Usually used when the number of repetitions is known.

Remember: A while loop may run zero times if its condition is False at the start.

while Loop and Its else Part

The condition in a while loop must be a Boolean expression that returns True or False. The loop body repeats while the condition is True. The optional else block runs when the condition becomes False.

Worked example

Print all numbers from 10 to 15 using a while loop.

Try it first, then show the solution
  1. Set i to 10.
  2. Check whether i <= 15.
  3. Print i and increase it by 1 while the condition is True.
  4. When i becomes 16, the condition is False and the loop stops.

Answer: The output is 10 11 12 13 14 15.

The variable i controls how many times the loop runs. The update i = i + 1 can also be written as i += 1. In print, end can add characters such as '\t' or '\n'. The sep parameter sets the separator between values.

Worked example

Use the else part with a while loop that prints numbers from 10 to 15.

Try it first, then show the solution
  1. Start with i = 10 and repeat while i <= 15.
  2. Print i and increase it by 1 each time.
  3. After the condition becomes False, run the else block.

Answer: The numbers printed are 10 11 12 13 14 15. The else block then prints the value of i when the loop exits: 16.

for Loop, range(), and Indentation

Definition

for loop
A loop that takes each item from a sequence in turn and runs its body.

A sequence can be a string or a collection of values. The control variable gets the next item on each repetition. A for loop can also have an optional else block, which runs after the loop finishes.

range()

range(start,stop,[step])

start is the initial value, stop is the final limit, and step is the optional increment value. The range ends at stop - 1.

Examples of range()

  • range (1,30,1) gives values from 1 to 29.
  • range (2,30,2) gives values from 2 to 28.
  • range (30,3,-3) gives values from 30 to 6.
  • range (20) gives values from 0 to 19.

Worked example

Print the single digit even numbers using range (2,10,2).

Try it first, then show the solution
  1. The range begins at 2 and increases by 2.
  2. It stops before 10.
  3. Print each value in the range.

Answer: The output is 2 4 6 8.

Worked example

Print the single digit even numbers, followed by a message from the for loop's else block.

Try it first, then show the solution
  1. Run the loop with range(2,10,2) and print each value.
  2. After the loop finishes, run the else block.

Answer: The output is 2 4 6 8, followed by End of the loop.

Worked example

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

Try it first, then show the solution
  1. Set n = 100 and sum = 0.
  2. Use range(1,n+1) so the loop includes 100.
  3. Add counter to sum during each repetition.

Answer: Sum of 1 until 100: 5050

A for loop can take items from a string. For example, looping through 'Computer' prints its characters one by one. Indentation is important because it marks the loop body and other code blocks.

Remember: Use n+1 as the stop value when a range must include n.

Nested Loops

Definition

Nested loop structure
A loop placed inside another loop is called a nested loop structure.

A while loop can contain another while loop, and a for loop can contain another for loop. A for loop and a while loop can also be placed inside each other.

Worked example

Use a for loop inside a while loop to print the number pattern.

Try it first, then show the solution
  1. Start with i = 1 and repeat the while loop while i <= 6.
  2. For each value of i, run range (1,i) and print its values.
  3. Move to a new line and increase i by 1.

Answer: The pattern is: 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5

Python jump statements

Jump statements change the normal flow of a program. Python has three jump statements: break, continue and pass. Each one affects the loop or code block in a different way.

The three jump statements

  • break ends the loop containing it.
  • continue skips the rest of the current loop iteration and starts the next one.
  • pass does nothing when it runs.

Remember: break ends a loop, continue skips to its next iteration, and pass does nothing.

break statement in Python

Definition

break statement
A statement that ends the loop containing it and moves program control to the code after the loop.

A loop normally continues until its condition becomes false. break can end it earlier. In a nested loop, break ends only the innermost loop.

If break ends a loop, that loop's else part does not run. Program execution continues with the statement after the loop.

Worked example

What does the break example print when it checks the letters in “Jump Statement” and stops at “e”?

Try it first, then show the solution
  1. The loop checks each letter in the string.
  2. Letters are printed until the loop reaches “e”.
  3. When the condition is met, break ends the loop.

Answer: Jump Stat

Worked example

What is printed by the break example that has an else part?

Try it first, then show the solution
  1. The loop prints letters until it reaches “e”.
  2. break ends the loop, so its else part is skipped.
  3. The statement after the loop prints the program-ending message.

Answer: Jump Stat End of the program

continue statement in Python

Definition

continue statement
A statement that skips the remaining part of the current loop iteration and starts the next iteration.

Unlike break, continue does not end the whole loop. The loop carries on with its next iteration after the condition is met.

Worked example

What does the continue example print while checking “Jump Statement” for the letter “e”?

Try it first, then show the solution
  1. The loop checks each letter in the string.
  2. When it reaches “e”, continue skips the print for that iteration.
  3. The loop prints the other letters and then prints the final message.

Answer: Jump Statmnt End of the program

break and continue

breakcontinue
Ends the loop containing it.Skips the rest of the current iteration.
Moves control to the statement after the loop.Moves to the next iteration.
Skips the loop's else part when it exits the loop.Does not end the loop.

pass statement in Python

Definition

pass statement
A null statement that is ignored when executed and performs no operation.

pass can be used in an if clause or a loop when no action is needed in that block. It can also act as a placeholder for a loop or function that will be written later.

Worked example

What happens in the pass example if the input is 0, and what is shown when the input is 3?

Try it first, then show the solution
  1. If a is 0, the if block runs pass, so no action is performed.
  2. If a is not 0, the else block prints its message.
  3. For the shown input, a is 3.

Answer: For input 0, nothing happens in the if block. For input 3, the output is “non zero value is accepted”.

Worked example

What does the pass example in the for loop print?

Try it first, then show the solution
  1. The loop checks each value in “Computer”.
  2. pass performs no action during the loop.
  3. After the loop, the print statement runs.

Answer: End of the loop, loop structure will be built in future.

Remember: Use pass when a code block must exist but should do nothing for now.

Questions students ask

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

What are the three types of control structures in Python?

The three types are sequential, alternative or branching, and iterative or looping. Sequential statements run one after another. Branching chooses a path based on a condition. Iteration repeats a set of statements.

What is the difference between if and if..else in Python?

A simple if statement runs its block only when its condition is true. If the condition is false, that block does not run. An if..else statement has two blocks, so it runs one for a true condition and the other for a false condition.

How does if..elif..else work in Python?

The program checks conditions in order. When a condition is true, it runs that condition's statement block. If all the conditions fail, it runs the else block. If an else clause is used, it must come at the end.

Why is indentation important in Python?

Indentation shows which block of code a statement belongs to. It is required in Python, not just used to make code look neat. Statements inside an if, elif, or else block must be indented so Python can identify their place in the program.

What is a while loop in Python?

A while loop repeats a block of code as long as its condition is True. It checks the condition before running the body, so the body may not run at all if the condition is False at the beginning.

What is the difference between a while loop and a for loop?

A while loop repeats while its condition is True. A for loop goes through each item in a sequence, such as a string or a collection of values. A for loop is usually used when the number of repetitions is known.

Show 7 more questions

How does range() work in Python?

range(start,stop,[step]) generates values beginning at start and changing by step. The stop value is not included. For example, range (20) gives values from 0 to 19.

What is a nested loop structure?

A nested loop structure has one loop placed inside another loop. For example, a for loop can be placed inside a while loop. The inner loop runs as part of the outer loop.

What does the else part of a loop do in Python?

The optional else part runs when the loop has finished. In a while loop, it runs when the condition is tested as False. In a for loop, it runs after the loop has gone through its sequence.

What is a jump statement in Python?

A jump statement changes the normal flow of a program by transferring control. Python has three jump statements: break, continue and pass. break ends its loop, continue skips to the next iteration, and pass performs no operation.

What does break do in a Python loop?

break immediately ends the loop that contains it. Program control moves to the statement after the loop. In a nested loop, it ends only the innermost loop. When break exits a loop, that loop's else part is not executed.

What is the difference between break and continue in Python?

break ends the loop and moves control to the code after it. continue skips the remaining statements in the current iteration and starts the next iteration. So break stops the loop, while continue lets the loop carry on.

What is pass used for in Python?

pass is a null statement, so executing it does nothing. It can be used in an if clause or loop when no action is needed. It is also useful as a placeholder for a code block that will be implemented later.

Book back answers

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

Choose the best answer 1 Marks

  1. How many important control structures are there in Python?

    • A) 3
    • B) 4
    • C) 5
    • D) 6
    Try it, then show the answer

    A) 3

    The chapter lists three important control structures: sequential, alternative or branching, and iterative or looping.

    From the book: “There are three important control structures”

  2. elif can be considered to be abbreviation of

    • A) nested if
    • B) if..else
    • C) else if
    • D) if..elif
    Try it, then show the answer

    C) else if

    The chapter states that ‘elif’ can be considered an abbreviation of ‘else if’.

    From the book: “‘elif’ can be considered to be abbreviation of ‘else if’.”

  3. What plays a vital role in Python programming?

    • A) Statements
    • B) Control
    • C) Structure
    • D) Indentation
    Try it, then show the answer

    D) Indentation

    Indentation is required to show which block of code a statement belongs to.

    From the book: “Indentation plays a vital role in Python programming”

  4. Which statement is generally used as a placeholder?

    • A) continue
    • B) break
    • C) pass
    • D) goto
    Try it, then show the answer

    C) pass

    The chapter describes pass as a placeholder for a block that is to be implemented later.

    From the book: “pass statement is generally used as a placeholder.”

All 18 book back answers →

Practice

Question 1 of 5Score 0

What does a sequential statement do?

Exam questions

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

Name the three types of flow of control described in this lesson.2 marks

They are sequencing, branching or alternative, and iteration.

How do if..else statements decide which block to execute?2 marks

They test a condition. If it is true, statements-block 1 runs; otherwise, statements-block 2 runs.

Explain the difference between break and continue.2 marks

break terminates the loop and transfers control to the statement after it. continue skips the remaining part of the current iteration and starts the next iteration.

What is the purpose of range() in a for loop, and how does its stop value work?2 marks

range() generates a series of values for a loop. The sequence runs up to stop minus one, not including stop.

Watch out

  • Do not confuse break with continue: break exits the loop, while continue moves to the next iteration.
  • Do not treat indentation as decoration; Python uses it to show which statements belong to a block.

Still stuck on this lesson?

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

Ask AI about this topic →