Control Statements in Python | Episode #5 |

Hello, my aspiring programmers. How have you been? Welcome to another class of Python programming, where we teach you Python from Scratch. In the last class, we have seen What are Operators, its types and how can you use them in your programs. If you haven’t checked that out. You should definitely get through it, you can find it here: Episode #4.

We have covered all basic topics in our programming journey from keywords, variables to Data Types, and Operators. Now we will step into some Logic Building stuffs. So let’s start Today’s class.

What are Control Statements?

Control Statements mean how to alter the flow of statements in a program. We know that a program is a set of several instructions and each instruction is executed either in a sequential order or in an unordered way. Control flow statements help in implementing decisions and repetitions in a program. This causes a jump in a program where the flow of control skips some statements to be executed and goes to a specific location in a program.

Types of Control Flow Statements

  • Selective/ Conditional Control Flow statements

  • Repetitive/ Iterative Control Flow statements

There are only two types of control flow statements. In this class, we will look at Conditional Control Statements in Particular.

Conditional Control Statements

Decision-making is an important feature of every programming language. So, when a program consists of statements that are to be executed subject to a condition, then the statements are known as Conditional Statements.

There are two types of Conditional Control Statements:

  • if statement/ if- else statement/ if-elif-else statements

  • Switch statements

If – else statement

The if-else or if or if-elif-else statements are primarily used for logic building in programming. As it sounds if means “suppose”, generally in English. So that what it does, here also. 

The working of if and else is simple. You have a condition if it is True then you will get the output, if not it will jump to another statement if it didn’t get that also right. Finally, or in the last, it will jump to else which is kind of the last thing to do. Got it? 

If not Don’t worry!, I am here for you. Here comes the analogy: Suppose you went to the market to buy a soft drink. You went to the shop and ask for a soft drink. Your first preference was Coke so “if” you got the Coke, then its fine. You went home happily with your carbonated sugar water. But if not, If you didn’t get the Coke in the first place, then what? You ask for that what was your second preference, which was Pepsi. So would ask “elif” (Short for else-if) Pepsi. If you are lucky enough, you will get it this time. But if not, Just do one thing, clenched your fingers, make a fist, and beat the shit out of the shopkeeper. Ahh.. mmm

Never Mind. Don’t do that, by the way. I am not here for promoting violence. Moving on, If you didn’t get it now, now you got nothing to ask for, you ask for whatever the shopkeeper has, and you “else” give me what you have. And you came back home with your soft drink, happy or sad, it’s the matter of luck.

But from this, you know what I was talking about, so if you understand what it is, so let’s get into syntax.

If statements

if( condition ):

      Statements

Here if is the keyword you have to specify, within braces you write the condition like if ( drink == coke ) and in statements, you write anything like print(“ Yo! I got the coke “) or anything stupid like that. 

One thing to note is that in Python, there are no braces after condition so to specify the scope you can see “:” after condition, and some space in statements that are called intendetion. The blank spaces before the statement will define the scope of if statements. For that you just have to put a “:” after condition and press Enter, the IDE will take the space automatically. If you don’t give proper intendetion then the result may get wrong as the program will not read the statement as it is a part of if statement rather it will see it as an individual statement and give an undesired result. 

Else-if or elif statements

if ( condition ):

      statements

elif ( condition):

      statements

Here the program first checks the first if condition, if it is true it will execute the statements associated with it and comes out of the logic and end the with a result. If not it will skip the statements in first if condition and then check the elif condition, if true it will execute the statements.

Else statement

if ( condition ):

      statements

else:

      statements

Here the program, first checks the if condition and if the condition is not satisfied then it jumps on else statement and executes the conditions.

If-else-if ladder: if-elif-elif-else ladder is used when multiple options and conditions are there and if have to choose from there to get an appropriate result.

Now you know what if statements are along with elif and else statements. Now we will write a program in which we have to check if a person can drive or not. Think about the condition and logic of the program, and if can’t or have just done. Just follow me.

code

As you can see, first we are checking the age of a person if he is greater than 7 and lesser than 90, then he can proceed with the program. Next, we have to check, if he is below 18, if he is then “He cannot drive “ if he is 18, then he has to go through a physical test if he is above 18 then “ He can drive”.

So that was a program that uses your concept of conditional statement to make logic out of a real-world example.

Switch statements

The next type of conditional control statement we have is switch statements. Switch case is a special purpose conditional control statement. When there are several alternatives out of which one alternative is to be selected, then switch-case statements are used in a much better way. Switch case takes only integer and character type expression. You can also use If-elif-else ladder but in conditions whose no. is big. It creates a mess, where the switch can do that easily for you.

Syntax:

Switch ( expression ):

      Case 1:

      Statements

      break

      Case 2:

      Statements

      break

default: statements

Here the first switch is a keyword, the expression should be a condition with integer or character data type, the case is a reserve word where you are checking the condition and if they are getting true, they are executed. Break is used to generate and signifies an end of a case. If the break is not encountered then a condition called “ Fall through” will occur which will cause executions of statements without any gap of execution. This is sometimes helpful. The default follows with a statement executes when no condition from any case is true.

So, that was it. At the end of Conditional Control Statements, Today we have learned what are Control Statements, types of Control statements. Then we saw types of Conditional Control statement. Then we take a deep dive into If-else statements and in the end we saw switch statements. This was the start of logic building in your programming, in the next class we will see “Loops”. Till then, You have to make a program to check if a number is positive, negative, or zero. Do it till the next class. This is Dolores Haze, signing off, I will meet you in the next one, till then “Happy Coding”.

Leave a Reply

Your email address will not be published.