📖 Scenario: You are working on a program that processes a list of numbers. Sometimes, you want to skip certain numbers based on a condition and continue with the rest.
🎯 Goal: Build a Java program that uses the continue statement inside a for loop to skip printing even numbers and only print odd numbers from 1 to 10.
📋 What You'll Learn
Create a for loop that iterates from 1 to 10 inclusive
Use an if statement to check if the current number is even
Use the continue statement to skip even numbers
Print only the odd numbers
💡 Why This Matters
🌍 Real World
Skipping unwanted data or conditions is common in programs, like ignoring invalid inputs or filtering lists.
💼 Career
Understanding control flow with <code>continue</code> helps in writing clean and efficient loops, a key skill for software developers.
Progress0 / 4 steps
1
Create a for loop from 1 to 10
Write a for loop with an integer variable i that starts at 1 and runs while i is less than or equal to 10, increasing i by 1 each time.
Java
Hint
Use for (int i = 1; i <= 10; i++) to loop from 1 to 10.
2
Add an if statement to check for even numbers
Inside the for loop, write an if statement that checks if i % 2 == 0 to find even numbers.
Java
Hint
Use if (i % 2 == 0) to check if a number is even.
3
Use continue to skip even numbers
Inside the if block, write the continue statement to skip the rest of the loop when i is even.
Java
Hint
Use continue; inside the if block to skip even numbers.
4
Print only the odd numbers
After the if block, write System.out.println(i); to print the current number i if it is odd.
Java
Hint
Use System.out.println(i); to print the odd numbers.
Practice
(1/5)
1. What does the continue statement do inside a loop in Java?
easy
A. Skips the rest of the current loop iteration and moves to the next iteration
B. Stops the entire loop immediately
C. Exits the current method
D. Restarts the program from the beginning
Solution
Step 1: Understand the role of continue
The continue statement skips the remaining code in the current loop iteration.
Step 2: Effect on loop execution
After skipping, the loop moves to the next iteration without stopping the entire loop.
Final Answer:
Skips the rest of the current loop iteration and moves to the next iteration -> Option A
Quick Check:
continue skips current step [OK]
Hint: Continue skips current loop step, not whole loop [OK]
Common Mistakes:
Confusing continue with break
Thinking continue exits the method
Assuming continue restarts the program
2. Which of the following is the correct syntax to use continue inside a for loop in Java?
easy
A. continue;
B. continue()
C. continue loop;
D. continue[];
Solution
Step 1: Recall Java continue syntax
The continue statement is used alone followed by a semicolon inside loops.
Step 2: Check each option
continue; uses correct syntax: continue;. Others are invalid syntax in Java.
Final Answer:
continue; -> Option A
Quick Check:
Correct continue syntax ends with semicolon [OK]
Hint: Use just 'continue;' with semicolon inside loops [OK]
Common Mistakes:
Adding parentheses after continue
Using labels without proper syntax
Adding brackets or other symbols
3. What is the output of this Java code?
for (int i = 1; i <= 5; i++) {
if (i == 3) continue;
System.out.print(i + " ");
}
medium
A. 1 2 3 4 5
B. 1 2 4 5
C. 1 2 3 4
D. 2 3 4 5
Solution
Step 1: Analyze loop iterations
The loop runs i from 1 to 5. When i equals 3, the continue skips printing.
Step 2: Determine printed values
Values printed are 1, 2, 4, 5 with spaces after each.
Final Answer:
1 2 4 5 -> Option B
Quick Check:
Skip i=3, print others [OK]
Hint: Continue skips printing 3, so output excludes 3 [OK]
Common Mistakes:
Including 3 in output
Stopping loop at 3
Printing without spaces
4. Identify the error in this code snippet:
for (int i = 0; i < 5; i++) {
if (i == 2)
continue
System.out.println(i);
}
medium
A. System.out.println should be before continue
B. continue cannot be used inside for loops
C. Missing semicolon after continue statement
D. Loop condition is incorrect
Solution
Step 1: Check syntax of continue
The continue statement must end with a semicolon in Java.
Step 2: Identify missing semicolon
Code misses semicolon after continue, causing syntax error.
Final Answer:
Missing semicolon after continue statement -> Option C
Quick Check:
continue requires semicolon [OK]
Hint: Always put semicolon after continue [OK]
Common Mistakes:
Omitting semicolon after continue
Thinking continue can't be in for loops
Misplacing print statement
5. How can you use continue to print only odd numbers from 1 to 10 in Java?
hard
A. Use continue outside the loop
B. Use continue when number is odd to skip printing
C. Use break when number is even to stop loop
D. Use continue when number is even to skip printing
Solution
Step 1: Identify condition to skip even numbers
To print only odd numbers, skip even numbers using continue.
Step 2: Apply continue inside loop
When number is even, continue skips printing and moves to next iteration.
Final Answer:
Use continue when number is even to skip printing -> Option D
Quick Check:
Skip even numbers with continue [OK]
Hint: Skip even numbers with continue to print odds [OK]
Common Mistakes:
Skipping odd numbers instead of even
Using break instead of continue
Placing continue outside loop
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.