[go: up one dir, main page]

0% found this document useful (0 votes)
20 views2 pages

JF 5 2 Practice

Uploaded by

Rayhan Ramadan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views2 pages

JF 5 2 Practice

Uploaded by

Rayhan Ramadan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Java Fundamentals

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 post-test loop that executes an unknown number of times until a condition is


met, but always executes the first time through the loop.

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.

A pre-test loop that executes an unknown number of times until a condition is


met.
A keyword used to terminate a loop from executing before the loop condition is
met.

Try It/Solve It:


1. Consider you are asked to decode a secret message. The coded message is in numbers and each
number stands for a specific letter. You discover enough of the secret code to decode the current
message. So far, you know:

 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.

Test your code with the following input:5 3 4 4 6 2 6 7 4 1

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

You might also like