+ 1
I don't really understand 'if-statements' can someone please help me find a way to make it easier for me to learn?
5 Respuestas
+ 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
0
In which language?
0
in Python. Sorry for not clarifying before
0
thank you so much! it really helped!
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😅