I don't really understand 'if-statements' can someone please help me find a way to make it easier for me to learn? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

I don't really understand 'if-statements' can someone please help me find a way to make it easier for me to learn?

30th May 2019, 1:23 PM
Alec
Alec - avatar
5 Answers
+ 2
Thats cool, I like Python Anyway, here's the explanation: An if statement is a "decision-making" statement. It checks to see if an expression evaluates to "True". For example, "4 > 3" will be True because 4 is greater than 3 If the expression in the if-statements definition evaluates to True, the if-statement's code block will be run. Otherwise, the interpreter will skip over the if-statement until the expression eventually evaluates to True The definition of an if-statement is as follows: if #condition#: code to run if condition is True For example: x = int(input()) if x > 0: print("Input is greater than 0") Thats how easy it is😁. Follow-up comments incoming on elif and else statements
30th May 2019, 1:44 PM
Trigger
Trigger - avatar
0
In which language?
30th May 2019, 1:34 PM
Trigger
Trigger - avatar
0
in Python. Sorry for not clarifying before
30th May 2019, 1:36 PM
Alec
Alec - avatar
0
thank you so much! it really helped!
30th May 2019, 1:49 PM
Alec
Alec - avatar
0
As we saw in the first comment, when an if-statement's expression evaluates to False, the interpreter will skip over it. But what if you want something to happen if the expression is False🤔 This is where the elif and else statements come in The else statement holds code that will be run if the if-statement preceeding it has an expression Evaluating to False. For example: x = int(input()) if x > 0: print("Number is positive") else: print("Number is negative") The next statement is called elif. This is short for "else if". This statement denotes additional decision making if preceeding if- and elif-statements evaluate to False. For Example: age = int(input()) if age < 0: print("You cant be less than 0 years old") elif age == 0: print("Happy Birthday!") elif age >= 18: print("You are an adult") else: print("You are a child") As you can see, multiple elif-statements can be chained together for more complex decisions This concludes the short tutorial I guess😅
30th May 2019, 1:54 PM
Trigger
Trigger - avatar