3.4 Conditional Execution




You've been looking at simple, sequential issues and programs so far. To develop more complicated and useful programs, we must offer our programs the capacity to make decisions based on numerous variables and modify their behavior accordingly. We must utilize conditional execution for this.

 The simplest form of conditional execution in Python is the "if" statement.

 if time >  12:

    print("Good Afternoon!")

 The if statement in Python is fairly simple and obvious. In Python, the keyword "if" is followed by a logical statement that returns True or False. You have already studied expressions and variables in this course. The above example employs a variable called "time" and utilizes the "greater than" comparison operator to determine whether the time has passed 12. As illustrated in the example, an if statement always finishes with a colon.

 Under the if statement, the instructions/code to be executed if the if condition returns True are indented. These indentations are used by Python to indicate the scope of the if statement. Unlike in many other programming languages, the leading whitespaces in Python have syntactical value. There will be more instances of

The visualization of a simple if statement is shown below. 


In the figure above, notice how the program only executes specific code if the condition returns True. The program does nothing if it returns False. However, it is frequently beneficial to develop code that provides an alternate option. We can use an if-else statement to do this.

if time < 12:

    print("Good Morning!")

else:

    print("Good Afternoon!")

The diagram below shows an if - else block. Notice how both outcomes of the condition are utilized.


The Python "elif" keyword can be used to implement a multi-way condition. Elif might be thought of as a "else-if". The software can select one of several pathways by using elifs. The following is the syntax.

if marks > 80:

    print(“A”)

 elif 60 <= marks < 80:

    print(“B")

 elif 50 <= marks < 60:

    print("C")

 else:

    print(“F")

 Unlike using several if statements one after the other, the program evaluates the subsequent elif conditions only if the first if statement returns False. If any of the successive elif conditions returns True, we execute that branch and skip over the other elif conditions. The following is a graphic representation of an if-elif-else block. We don't examine the conditions that follow if the initial if condition or the subsequent elif conditions return True.

The best conditional construct for your program is determined by the solution you use. Visualizing the solution to grasp it before you begin writing might assist you in selecting the best conditional execution.

 As you progress through this course, you will see Try- except blocks that serve a similar purpose. Exceptions in your code are handled using try except blocks. They are also known as conditional execution because they define pathways for the program to pursue if it encounters a problem. More to come on that later!