006-005-012
Using Break and Continue
Medium
Problem Description
Using Break and Continue
In this problem, you will read upper bound N and break trigger target from Scanner, then create a program that uses break to exit a loop when target is found and uses continue to skip even numbers and display only odd numbers.
Learning Objective: Understand the behavior of loop control statements break and continue
Overview
break immediately exits the loop, while continue skips the current iteration and proceeds to the next.
Input Format
N
target
- Line 1: Loop upper bound N (N >= 1)
- Line 2: Break trigger value target (1 <= target <= N)
Specifications
- Loop from 1 to N, break when target is found
- Loop from 1 to N, skip even numbers with continue and display only odd numbers
Output Format
Break example:
[numbers printed before break, space-separated]
Continue example:
[odd numbers only, space-separated]
Sample I/O
Input:
10
5
Output:
Break example:
1 2 3 4
Continue example:
1 3 5 7 9
Ready to Try Running Code?
Log in to access the code editor and execute your solutions for this problem.
Don't have an account?
Sign Up