castillo.anthony28
castillo.anthony28 2d ago β€’ 0 views

if statement examples in Java for High School: AP Computer Science A

Hey there! πŸ‘‹ Ready to ace your AP Computer Science A test? Let's break down those tricky 'if' statements with some easy examples and a fun quiz! πŸ€“
πŸ’» Computer Science & Technology

1 Answers

βœ… Best Answer
User Avatar
WildLife_Watcher Dec 29, 2025

πŸ“š Quick Study Guide

  • πŸ” An if statement executes a block of code only if a specified condition is true.
  • πŸ’‘ The basic syntax is: `if (condition) { // code to execute }`
  • πŸ“ You can add an else block to execute code when the condition is false: `if (condition) { // code if true } else { // code if false }`
  • 🌱 Use else if to chain multiple conditions: `if (condition1) { // code 1 } else if (condition2) { // code 2 } else { // default code }`
  • 🧱 Conditions use relational operators (==, !=, <, >, <=, >=) and logical operators (&&, ||, !).
  • 🍎 Always use curly braces `{}` to define the code block, especially for multiple lines.
  • πŸ”‘ Nested `if` statements are allowed, placing an `if` statement inside another.

πŸ§ͺ Practice Quiz

  1. Which of the following is the correct syntax for an `if` statement in Java?
    1. `if condition then { code }`
    2. `if (condition) then { code }`
    3. `if condition { code }`
    4. `if (condition) { code }`
  2. What will be the output of the following code? java int x = 5; if (x > 10) { System.out.println("x is greater than 10"); } else { System.out.println("x is not greater than 10"); }
    1. `x is greater than 10`
    2. `x is not greater than 10`
    3. No output
    4. Error
  3. Which operator is used to check for equality in Java `if` statements?
    1. `=`
    2. `==`
    3. `!=`
    4. `>`
  4. What does the `else` block do in an `if-else` statement?
    1. It executes code only if the condition is true.
    2. It executes code always.
    3. It executes code only if the condition is false.
    4. It defines a new condition.
  5. What will be the output of the following code? java int age = 17; if (age >= 18) { System.out.println("Eligible to vote"); } else if (age >= 16) { System.out.println("Almost eligible"); } else { System.out.println("Not eligible to vote"); }
    1. `Eligible to vote`
    2. `Almost eligible`
    3. `Not eligible to vote`
    4. No output
  6. Which of the following is a logical AND operator in Java?
    1. `||`
    2. `&&`
    3. `!`
    4. `&`
  7. What happens if the condition in an `if` statement is always true?
    1. The code inside the `if` block never executes.
    2. The code inside the `if` block executes once.
    3. The code inside the `if` block executes repeatedly, creating an infinite loop if not handled carefully.
    4. It causes a compilation error.
Click to see Answers
  1. D
  2. B
  3. B
  4. C
  5. B
  6. B
  7. C

Join the discussion

Please log in to post your answer.

Log In

Earn 2 Points for answering. If your answer is selected as the best, you'll get +20 Points! πŸš€