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

7th Feb 2017, 4:03 PM
Aryonne Link
Aryonne Link - avatar
5 Respostas
+ 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.
7th Feb 2017, 10:27 PM
Luke Armstrong
+ 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.
7th Feb 2017, 4:17 PM
Paul Silvestri
Paul Silvestri - avatar
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).
7th Feb 2017, 4:26 PM
Reshab Sharma
Reshab Sharma - avatar
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.
17th Feb 2017, 5:05 PM
Mohamed S A Eltumi
Mohamed S A Eltumi - avatar
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 )
20th Feb 2017, 2:32 AM
Nobel
Nobel - avatar