0
I really dont understand if , else, statements . Is there a rule that could help me remeber because it looks crazy everytike i code
Dont understand if else statements
5 odpowiedzi
+ 5
think of it as if/then.
say you want to go out to eat,
IF you have enough money:
THEN you will go out to eat.
ELSE: (if you dont have money)
THEN you will stay home.
or
IF you want Arby's:
then go to arby's
elIF you want McDonald's:
go to McDonald's
elif you want Panda Express:
go to panda express
else: (you dont want any of those)
go to burger king
so essentially:
if condition1 is true:
do something
elif condition2 is true:
do something else
elif condition3 is true:
do another thing
else:
do this only if none of the other conditions are true.
+ 1
So think about it like this.
If(statement is true) :
Run this code
Else(if the statement isn't true(false)) :
Then run this code.
It's boolean. Go back and look at boolean. Think about it as a lock. If the statement is true, then it unlocks the code. If it's false, it unlocks different code.
0
if else statement is lIke a game . There are two conditions if first condition is true then we ll not check the other condition , we ll stop there. If my first condition get fails then it will check the second condition, by using (else).
0
Java if statement is used to test the condition. It checks boolean condition: true or false.
if your condition is acceptable as you mention in your codes than your program will run
public class IfExample {
public static void main(String[] args) {
java.util.Scanner s = new java.util.Scanner(System.in);
System.out.println("Please Enter Your Age");
int age = s.nextInt();
if(age>18){
System.out.print("Welcome");
}
else {
System.out.println("Sorry");
}
}
}
So the condition you have given if your Age greater than 18 you will be able to use this app
if less than 18 you unable to use it.
0
If ans else if statements are to complare multiple outcomes. Such as
If <condition> satisfies :
Print (statement)
elif <another condition> :
Print (statemwnt)
elif <another condition>:
print (statement)
Else :
Print (another statement )