JF 5 2 Practice
JF 5 2 Practice
5-2 : Control
Statements Practice
Activities
Lesson Objectives:
Create a while loop
Create a do-while loop
Create a for loop
Vocabulary:
Identify the vocabulary word for each definition below.
A pre-test loop that uses an iterator to keep track of how many times a loop will
execute.
A keyword used to skip over the remaining code in a loop and return program
control to the beginning of the loop to execute again.
1 represents “D”
2 represents “W”
3 represents “E”
4 represents “L”
5 represents “H”
6 represents “O”
7 represents “R”
Write a program that prompts the user for 10 numbers, one at a time, and prints out the decoded message. If the
user enters a number that is not one of those already deciphered, prompt him/her for a new number.
Copyright © 2022, Oracle and/or its affiliates. Oracle, Java, and MySQL are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of
their respective owners.
2. Suppose you are implementing a search routine that searches through a String, character by character, until
it finds a space character. As soon as you find the first space character, you decide that you do not want to
continue searching the string. If you are using a WHILE loop and your loop will continue to execute until you
have gone through the entire string, should you use the keyword break or continue when you find the first
space character? Why? Why would you not use the other keyword?
3. Imagine you are writing a program that prints out the day of the week (Sunday, Monday, Tuesday, etc.) for
each day of the year. Before the program executes, can you tell how many times the loop will execute?
Assume the year is not a Leap year. Given your answer, which type of loop would you need to implement?
Explain your reasoning.
4. An anagram is a word or a phrase made by transposing the letters of another word or phrase; for example,
"parliament" is an anagram of "partial men," and "software" is an anagram of "swear oft." Write a program
that figures out whether one string is an anagram of another string. The program should ignore white space
and punctuation.
Copyright © 2022, Oracle and/or its affiliates. Oracle, Java, and MySQL are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks 2