0
0
Javaprogramming~5 mins

If statement in Java - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of an if statement in Java?
An if statement lets the program decide to run some code only if a condition is true. It helps the program make choices.
Click to reveal answer
beginner
Write the basic syntax of an if statement in Java.
The basic syntax is:<br>
if (condition) {<br>  // code to run if condition is true<br>}
Click to reveal answer
beginner
What happens if the condition in an if statement is false?
If the condition is false, the code inside the if block does not run. The program skips it and continues.
Click to reveal answer
beginner
Can an if statement be used without an else part?
Yes, an if statement can stand alone. The else part is optional and runs code when the condition is false.
Click to reveal answer
beginner
How do you write an if-else statement in Java?
You write it like this:<br>
if (condition) {<br>  // code if true<br>} else {<br>  // code if false<br>}
Click to reveal answer
What does the if statement check in Java?
AA condition that is true or false
BA loop counter
CA variable declaration
DA method call
What happens if the if condition is false and there is no else?
AThe program skips the <code>if</code> block and continues
BThe program runs the code inside the <code>if</code> block
CThe program stops running
DThe program throws an error
Which of these is the correct syntax for an if statement?
Aif condition { }
Bif condition then { }
Cif [condition] { }
Dif (condition) { }
Can you have multiple if statements one after another?
ANo, only one <code>if</code> per program
BOnly if they have <code>else</code> parts
CYes, you can have many <code>if</code> statements
DOnly if they are inside a loop
What keyword do you use to run code when an if condition is false?
Aelseif
Belse
Cwhen
Dotherwise
Explain how an if statement works in Java and write a simple example.
Think about how you decide to do something only if a certain condition is true.
You got /4 concepts.
    Describe the difference between an if statement and an if-else statement.
    Consider what happens when the condition is false.
    You got /4 concepts.