0% found this document useful (0 votes)
14 views8 pages

Unit II 03 Control Structures

Uploaded by

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

Unit II 03 Control Structures

Uploaded by

victor.seelan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Control Structures

Definition
Control structures are blocks of code that determine the flow of program
execution based on conditions or repetition needs. They enable decision-making, looping, and
branching, which are essential for data processing, algorithm implementation, and automation
in statistical programming.
Types of Control Structures in Python
Python mainly has three categories:
A. Conditional Statements (Decision-Making) : if, elif, else
B. Looping Statements (Iteration) : for, while, and (break, continue, pass)
C. Nested Control Structures
Practical Relevance in Statistics & Data Science
 Conditionals: Apply different models or transformations based on data conditions.
 Loops: Automate repetitive statistical computations, iterate over datasets, simulation
runs.
 Nested structures: Implement complex algorithms such as hypothesis testing
workflows or iterative optimization.

Conditional Statements
Conditional statements are control structures that allow a program to execute different blocks
of code depending on whether a specified condition is true or false. They enable decision-
making, which is vital for implementing logic, data filtering, and automation in programs.
 The if Statement
 The if-else Statement
 The if-elif-else Statement
Looping Statements
Definition

Looping statements in Python are control structures used to execute a


block of code repeatedly, either for a specific number of times or while a
condition holds true.
In statistical programming, loops are often used for iterating through
datasets, automating tasks, and running simulations.

A. for Loop

B. while Loop

C. Loop Control Statements

A. for Loop – Iteration over sequences

A for loop in Python is used to iterate over a sequence (such as a list,


tuple, string, or range) and execute a block of code for each item in that
sequence. It is a fundamental tool for repeating tasks and processing
collections of data.
Example 3: Using for loop with else

Example 4: Nested for loop

B. while Loop – Condition-based repetition

A while loop repeatedly executes a block of code as long as a given


condition remains True. It is used when the number of iterations is not
predetermined, and the loop depends on dynamic conditions.
Note:

Update condition variables: Make sure to update variables affecting


the condition inside the loop; otherwise, the loop may run infinitely.

Infinite loops: A loop whose condition never becomes False leads to an


infinite loop, which causes the program to hang or crash unless
interrupted manually.

D. Loop Control Statements

Break Statement:

continue Statement:

Skips the current iteration and continues with the next one.
Pass Statement:

Does nothing — it's a placeholder used when a statement is required


syntactically but no action is needed.

Example:

Nested Control Structures


Nested control structures are control structures (like if, for, or while) placed inside another
control structure. They allow more complex decision-making and iterative logic.
You can nest: Conditionals inside conditionals, Loops inside loops, Loops inside conditionals
and Conditionals inside loops.
1. Loop with Conditional Inside
2. Nested if Statement
3. Nested for statement
4. Nested while Statement

You might also like