Basic Python | Control Flow Statements


Control flow is where the rubber really meets the road in programming. Without it, a program is simply a list of statements that are sequentially executed. With control flow, you can execute certain code blocks conditionally and/or repeatedly, these basic building blocks can be combined to create surprisingly sophisticated programs!
Conditional Statements: 
The “if”, “elif,” and “else” statements are the most commonly used control flow statements.
For Loops
Loops in Python are a way to repeatedly execute some code statement. So, for example, if we'd like to print each of the items in a list, we can use a for loop:
While loops
The argument of the while loop is evaluated as a boolean statement, and the loop is executed until the statement evaluates to False.
break and continue: Fine-Tuning Your Loops
There are two useful statements that can be used within loops to fine-tune how they are executed:
  • The break statement breaks-out of the loop entirely
  • The continue statement skips the remainder of the current loop, and goes to the next iteration
These can be used in both for and while loops.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.