The if statement
For writing useful programs, we always need the aptitude to check the conditions and change the behaviour of the program consequently. This article will also help you learn about if else statement. Conditional statements make us able to do this. The simplest form is the ‘if statement’, which has the general form:
if BOOLEAN EXPRESSION:
Some important things to note about the ‘if statements’:
- The colon (:) is significant and necessary. It splits the header of the compound statement from the body.
- The line present after the colon should be indented. It is standard in Python for using 4 spaces for indenting.
- All lines indented the equal amount after the colon will be implemented whenever the BOOLEAN_EXPRESSION will be true.
food = 'spam' if food == 'spam': print('Umm, my favorite!') print('I feel like saying it 200 times...') print(200 * (food + '! '))
The boolean expression after the ‘if statement’ is known as the condition. If it is true, then all the indented statements get implemented. What happens in case the condition is false? And the food is not equal to ‘spam’? In a simple ‘if statement’ like this, nothing occurs, and the program carries on to the next statement.
Run this example code and look at what the results come. Then modify the value of food to something other than ‘spam’ and run it once again, confirming that you don’t get any output out of it.
Browse more Topics Under Conditional Constructs and Looping
- For (Range Function)
- Nested if
- Nested Loops
- Use of Compound Expression in Conditional and Looping Construct
The if else statement
It is commonly the case that you want one thing to happen when there is a condition that is true, and something else to happen when the condition is false. For this scenario, we have the if else statement.
if food == 'spam': print('Umm, my favourite!') else: print("No, I won't have it. I want spam!")
Here, the first print statement will implement if food is equal to ‘spam’, and the print statement indented under the ‘else’ clause will get implemented when it is not.
The while statement
The common syntax for the ‘while statement’ is like this:
Similar to the branching statements and the ‘for loop’, the while statement is a compound statement that consists of a header and a body as well. A ‘while loop’ executes multiple numbers of times that are not known, as long as the BOOLEAN EXPRESSION is true.
Here is an example:
number = 0 prompt = "What are the meaning of life, the universe, and everything? " while number != "41": number = input(prompt) Notice that if the number is set to 41 on the first line, the body of the while statement will not execute at all. Here is a much more elegant example program representing the use of the ‘while statement’ name = 'Harrisson' guess = input("So I'm thinking of a person's name. Try to guess it: ") pos = 0 while guess != name and pos < len(name): print("Nope, that's not it! Hint: letter ", end='') print(pos + 1, "is", name[pos] + ". ", end='') guess = input("Guess again: ") pos = pos + 1 if pos == len(name) and name != guess: print("So sad, you couldn't get it. The name was", name + ".") else: print("\nGreat, you got it in", pos + 1, "guesses!")
FAQ on If Else Statement While
Question 1: Is ‘while loop’ a conditional statement or not?
Answer: The ‘While loop’ & the ‘For loop’ are the 2 most commonly used types of the ‘conditional loops’ in most of programming languages.
Question 2: On what type of conditions are conditionals and the loops grounded on?
Answer: Each conditional and each loop is grounded on a ‘boolean’ condition that evaluates to either false or true