0
If else-statements
Could someone help me understand if else-statements and how it works? I am having trouble understanding them.
7 Réponses
+ 7
If and else statement
If a > b :
print yes
else:
print no
The if and else statements are used to compare and contrast a statement. Print 'yes' if it's true and 'no' if it's false, like the example above.
+ 3
It is very simple imagine someone selling You a pen . the sales man will ask do u want pen . your answer will be yes or no . Lets see this in code
if( condition)
{
//if you want the pen
document.write("yes");
}
else
{
//otherwise
document.write("no");
}
like this you can use if else statement for this or that answers
- 1
Is the statement the numbers? How could I do that with words instead of numbers in order to get an outcome?
- 1
Zo Nelson , yes you can also check for strings values,
Ex:
let text="test";
if(text=="test"){
your instructions here
}else{
your instructions here
}
Also as strings are case sensitive ,if(text=="Test") would be considered false...
The best for you would be to play with differents type of value(numbers,string,Boolean) and to console.log the results to see what happen...
You also have the challenges in sololearn which have such question and will help you understand,after challenge is completed go and check the answers.
- 1
They are of c++
- 2
An if statement runs if a condition is true and an else statements run if a condition is false eg.
if(4>3){
document.write("true")
}
else{
document.write("false")
}
the output will be true because the condition in an if statement is true
- 5
simply xD
if somthing happened....do this
if not (else i mean)....do this