I can't not understand else statement ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I can't not understand else statement ?

If there is any solution, give to me. I am beginner in python.

22nd Mar 2019, 4:04 AM
Hamza Hassan
Hamza Hassan - avatar
3 Answers
+ 6
if stament is used to execute a code if something is true, but when isn't true, you can execute other code with the else stament. example if 5 is equal to 4 print 4 else print 5 this code(without a real language) print 5 because 5 isn't equal to 4
22nd Mar 2019, 4:18 AM
InvBoy [ :: FEDE :: ]
InvBoy [ :: FEDE :: ] - avatar
+ 6
Else statements can be put after if statements and execute when the if statement fails. Example I: ---------------------------------------- number = 5; if (number == 10): print("ten"); else: print("not ten"); ---------------------------------------- This code outputs "not ten" since number was not equal to 10 so the if statement failed and the else statement is executed. Example II: ----------------------------------------- grade = "pass"; if (grade == "pass"): print("You passed."); else: print("You failed."); ---------------------------------------- This code only outputs "You passed." since the if statement is executed and that means the else statement is not executed.
22nd Mar 2019, 4:26 AM
LynTon
LynTon - avatar
+ 2
Else statement is used with if statement. Through the if statement, we check a condition and if the condition is true, the codes belongs to if statement executes. Now can you imagine what happens if the condition is false? Maybe the answer is nothing happens. Now if we want to do something when the condition is false, else statement helps us. Let's see an example. a = 3 b = 4 if a > b: print("A is bigger than B") else: print("B is bigger than A") It this example we have two variables a & b containing value 3 & 4. Now through the if statement, we check if a > b. In the example a is less than b. So the if statement doesn't execute. In this case the else block executes. If the expression would true, then the if block would execute. Note: You have not mentioned any language. As you are learning python, I have given the example in Python.
22nd Mar 2019, 4:31 AM
Adnan Zawad Toky
Adnan Zawad Toky - avatar