Conditional and Loops


        

Table of Contents

  1. Conditional
  2. Loops

Conditional

Conditional statement enables a program to execute only specific block of code. There are two type of conditional statements in general:

If, Else, and Else If

if statement specifies a block of code to be executed when a condition is true.

Example:

if condition: #block of code to be executed if the condition is true

Example:

a = 400 b = 20 if a>b: print("a is greater than b")

In the example, the code print("a is greater than b") will only be executed when the value of a is greater than b. Note: Python relies on indentation to know which code that the if statement specifies. So, remember to always put an indentation after an if statement.


        

However, in other cases, we need another statement to specify code to be executed when the condition is false. Then, we can use else for this case.

Syntax:

if condition: #block of code to be executed if the condition is true else: #block of code to be executed if the condition is false

Example:

a = 400 b = 20 if a>b: print("a is greater than b") else: print("a is less than b")

        

Lastly, the elif statement is used when there are more than two conditions that may occur. Elif statement specifies a new condition if the first condition is false.

Syntax:

if condition1: #block of code to be executed if condition1 is true elif condition2: #block of code to be executed if the condition1 is false and condition2 is true else: #block of code to be executed if the condition1 is false and condition2 is false

Example:

myNum = 10; if myNum < 1: print("myNum is less than 1") elif myNum < 10: print("myNum is less than 10") else: print("myNum is greater than or equal with 10")

Short-hand if If we only have one statement to execute, we can put it on the same line as the if statement

Syntax: if condition: expression

Example:

a = 400 b = 20 if a>b: print("a is greater than b")

Short-hand if-else, also called ternary operator, is a simple construction of an if-else statement. The ternary operator is used to simplify a block of if-else statements to a single line of code.

Syntax:

expressionTrue if condition else expressionFalse

Below is the simplified version of the previous if-else example:

Example:

a = 400 b = 20 print("a is greater than b") if a > b else print("b is greater than a")

        
        
Match Case

Besides the if-else statement, there is also the match case. The match case is used when there are too many conditions involving one variable. Instead of using many if-else statements, you can use match case.

Syntax:

match parameter: case x: #code block case y: #code block default: #code block

The expression in the code specifies which variable that will be inspected. If the expression fits the case x, then the program runs the first code block, etc. If the expression does not fit in any of the cases, it will run the default code block.

Note:
the default code block is optional.

Example:

day = 2; match day: case 1: print("Monday") case 2: print("Tuesday") case 3: print("Wednesday") case 4: print("Thursday") case 5: print("Friday") case 6: print("Saturday") case 7: print("Sunday")

Loops

As its name says, loops are used to loop codes. Loops enable a code to run endlessly as long as it still corresponds with the specified condition. There are two types of loops in Python:

While Loop

While loop loops through a block of code as long as the specified condition is true.

Syntax:

while condition: #code block to be executed

Example:

a = 1 while(a < 5): print(f"a is {a}") a+=1

The loop in the example will produce the output below:

Output:


            a is 1
            a is 2
            a is 3
            a is 4
            

Notice that the loop while keeps looping until a < 5. When the loop reaches a = 5, it will stop looping and the program is terminated.


    
        
For Loop

For loop is used when we know how many times we want to loop through a block.

Syntax:

for variable in variables: #code block to be executed

Example:

fruits=['apple', 'mango', 'banana'] for i in fruits: print(i)

The output of the program above will be:


            apple
            mango
            banana                
            


        

Python also enables a loop to loop through a string. For example:

Example:

for i in 'mango': print(i)

The result will be:


            m
            a
            n 
            g
            o                
            


        

We can also use the range() function to specify the number of times we wanted to loop.

Syntax: for variable in range (start, end, step)

  • The start value is optional. If we don't specify it, then it wil start from 0 by default.
  • The end value is a must when using this function.
  • The step value is also optional. If we do not specify it, then it will use increment by 1 (by default).

Example:

for i in range (3): print(i)

The output will be:


            0
            1
            2               
            


    
        
Break and Continue

A break statement can be used to jump out of a loop.

Example:

a = 1 while a < 10: print("Hello!") if(a == 4): break a++

The code above will produce the output of Hello! printed 4 times. When the variable a reaches 4, it will automatically jump out of the loop, resulting in only 4 printed outputs.


        

The continue statement is used to skip the rest of the code and jump to the next iteration in the loop.

Example:

for i in range(5): if i==3: continue print(i)

If we run the code above, we will get the output 0124 with no '3'. This is because when i reaches 3, we use the continue statement to skip the rest of the loop and move on to the next iteration (i = 4).