Conditional statements
Conditional statements allow you to execute a block of code only if a specific condition is true.
Syntax
Examples
- Simple
if
statement
if-else
statement
if-elif-else
ladder
- Nested
if
statements
Control Flow Statements
Control flow statements are used to alter the natural sequence of execution in a program.
Types of Control Flow Statements
a. for
Loop
Iterates over a sequence (list, tuple, string, etc.).
Example:
b. while
Loop
Executes a block of code as long as a condition is true.
Example:
c. break
Exits a loop prematurely.
Example:
d. continue
Skips the current iteration and moves to the next.
Example:
e. pass
Does nothing; a placeholder for future code.
Example:
f. else
with Loops
You can attach an else
block to a loop, which executes if the loop completes without a break
.
Example:
3. Example: Combining Conditional and Control Flow Statements
Comments
Post a Comment