The if and else statement, what are they for exactly? | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
0

The if and else statement, what are they for exactly?

16th Jun 2017, 9:03 PM
Quinton Motete
Quinton Motete - avatar
4 ответов
+ 3
They are very useful. They work on condition and allows you as programmer to control the flow of information. Example: If you got 60% or more in Math, then you passed this grade, else you failed! marks = 61 if(marks > 60) then Print("Passed!") else Print("Failed...")
16th Jun 2017, 9:54 PM
Limitless
Limitless - avatar
+ 6
They constitute the conditional statement. You can manipulate the flow of the code with them.
16th Jun 2017, 9:05 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 6
@Jamie explained very well.... 👍
17th Jun 2017, 12:53 AM
Pulkit sharma
Pulkit sharma - avatar
+ 5
The if statement is crucial to all things. It compares values in registers and helps decide where to jump the program to based on whether the result is zero or not zero... In lay terms it helps compare things and decide if they're true or false. If true, all the code within if's "body" (think of if as a function of sorts if we're talking Python) will be run. If not, we skip the body. if (1 ==1): do_something() print("Yaaay") continue_program() * I use parenthesis for clarity. Now 1 equals 1 so the condition is true, ergo do_something() and print() are run. if (9 == 10): print("This is a secret.") continue_program() Well 10 is not 9 so it's false so print() won't be run. Where else comes in... Well it's a convenient way of saying "Only if the if above was false, run this"... if (1 == 1): print("Runs") else: print("Does not") continue_program() Here we won't see "Does not" because 1 does equal 1 which means our if is true. if (99 == 1): print("The world is mad") else: print("The world is normal") continue_program() Here 99 is not equal to 1 so our if condition is false. Because it's false, our else is executed and "The world is normal" is printed. For further reading, try going over the course a few times.
17th Jun 2017, 12:35 AM
Jamie
Jamie - avatar