Chapter 5: Problem 6
Compare and contrast the while and for repetition statements.
Short Answer
Step by step solution
Key Concepts
These are the key concepts you need to understand to accurately answer the question.
Chapter 5: Problem 6
Compare and contrast the while and for repetition statements.
These are the key concepts you need to understand to accurately answer the question.
All the tools & learning materials you need for study success - in one app.
Get started for freeWhat does the following program do? 1 // Exercise 5.10: Printing.java 2 public class Printing 3 { 4 public static void main(String[] args) 5 { 6 for (int i = 1; i <= 10; i++) 7 { 8 for (int j = 1; j <= 5; j++) 9 System.out.print('@'); 10 11 System.out.println(); 12 } 13 } 14 } // end class Printing
Fill in the blanks in each of the following statements: a) Typically, ______________ statements are used for counter-controlled repetition and __________ statements for sentinel-controlled repetition. b) The do...while statement tests the loop-continuation condition __________ executing the loop's body; therefore, the body always executes at least once. c) The __________ statement selects among multiple actions based on the possible values of an integer variable or expression, or a String. d) The ______________ statement, when executed in a repetition statement, skips the remaining statements in the loop body and proceeds with the next iteration of the loop. e) The _________ operator can be used to ensure that two conditions are both true before choosing a certain path of execution. f) If the loop-continuation condition in a for header is initially _____________, the program does not execute the for statement's body. g) Methods that perform common tasks and do not require objects are called methods.
Write a Java statement or a set of Java statements to accomplish each of the following tasks: a) Sum the odd integers between 1 and 99 , using a for statement. Assume that the integer variables sum and count have been declared. b) Calculate the value of 2.5 raised to the power of \(3,\) using the pow method. c) Print the integers from 1 to 20 , using a while loop and the counter variable i. Assume that the variable i has been declared, but not initialized. Print only five integers per line. \([\text {Hint: Use the calculation i } \%\) 5. When the value of this expression is 0, print a newline character; otherwise, print a tab character. Assume that this code is an application. Use the System.out.print 1 n () method to output the newline character, and use the System .out.print \(\left.\left(' \backslash t^{\prime}\right) \text { method to output the tab character. }\right]\) d) Repeat part (c), using a for statement.
Describe the four basic elements of counter-controlled repetition.
Compare and contrast the break and continue statements.
What do you think about this solution?
We value your feedback to improve our textbook solutions.