Control Flow
Control Flow is used to determine what code gets executed.
Conditions
Conditions are statements that result in a boolean.
ans = "the sky red?"
# ans = No
Conditional Statements
If-Else Statements
If-else are statement in which if the condition is true, the if block code is executed. For values where condition is false, the else block is executed.
graph LR
A[Start]
B{"Am I Tired"}
C["Sleep"]
D["Code More"]
E["Do Something"]
A --> B
B -- Yes --> C --> E
B -- No --> D ---> E
ans = 2
if ans >= 0:
return "Yes"
else:
return "No"
# Output: Yes
Loops
For Loop
For loop goes through a sequence of element one by one, executing the block of code within itself
graph LR
A[Start]
B{"For Condition"}
C["Execute<br>code below"]
D["Exit the loop"]
E["Move on"]
A --> B
B -- In range ---> C --> B
B -- Out of range --> D --> E
friends = ["alice","bella","charlie"]
for i in friends:
print (i)
''' Output: alice
bella
charlie
'''
While Loop
While loop executes the block of code within itself as long as the condition holds true
graph LR
A[Start]
B{"While Sunny"}
C["Go out for a run"]
D["Exit while loop"]
E["Move on to other code segments"]
A --> B
B -- Sunny ---> C --> B
B -- Rainy(not sunny) --> D --> E
number = 5
while number > 0:
print (number)
number -= 1 #reduces number by one each time, i.e. 5-1=4, 4-1=3, etc
''' Output: 5
4
3
2
1
'''