Initial value nested loop

89 views 9:10 am 0 Comments March 23, 2023

CHAPTERFOUR
CHAPTER SUMMARY
For loops are used to automate the initialize, test, and update process. » In Visual Logic, the For loop flowchart element appears as a six-sided figure with a loop vari-able, a start value, an end value, and two exit arrows. (The step value also appears if the step value is something other than 1.) The body of the loop is to the right and below the element. When executed, the first action is to initialize the loop variable to the start value. » The body of the loop executes as long as the value of the loop variable does not exceed the final value. (Note: If the step value is negative, then the body of the loop executes as long as the value of the loop variable is not smaller than the final value.) After the body of the loop executes, the loop variable is updated by the step value, and the process is repeated. » Any valid statements can occur inside the body of a loop, including input, assignment, output, conditions, and even other loops. 5) A nested loop refers to a loop that appears inside the body of another loop.
KEY TERMS final value For loop
initial value nested loop
step value
REVIEW QUESTIONS 1. What is the difference between a For loop and a While loop? 2. When using a For loop, what happens when the loop variable is equal to the final value? What happens when the loop variable exceeds the final value? 3. Consider two For loops, one with a positive step value and one with a negative step value. How are these two loops similar? How are these two loops different? 4. When writing a program that reads input terminated by a sentinel value, which loop is more appropriate to use, a For loop or a While loop? Explain your answer. 5. Consider the following statement: “An infinite loop is more likely to occur when using a While loop than when using a For loop.” Do you agree or disagree with the statement? Explain your answer. PROGRAMMING EXERCISES 4-1. Blast Off! Write a program that uses a For loop to generate the output shown below. The text “Rocket launch” should appear only once in your program.
Roc et aunt n 10 secon s Rocket launch in 9 seconds Rocket launch in 8 seconds Rocket launch in 7 seconds Rocket launch in 6 seconds Rocket launch in 5 seconds Rocket launch in 4 seconds Rocket launch in 3 seconds Rocket launch in 2 seconds Rocket launch in 1 seconds Blast Off!

810
69

FOR LOOPS AND NESTED LOOPS
4-2. Pass It Around. Write a program that uses a For loop to generate the output shown below. Note that the program should only show the text for 99, 98, 9Z and 96 bottles (Hint: Have a start value of 99 and a final value of 96, with a step value of -1).
99 bottles of rootbeer on the wall 99 bottles of rootbeer Take one down and pass it around One less bottle of rootbeer on the wall 98 bottles of rootbeer on the wall 98 bottles of rootbeer Take one down and pass it around One less bottle of rootbeer on the wall
97 bottles of rootbeer on the wall 97 bottles of rootbeer Take one down and pass it around One less bottle of rootbeer on the wall 96 bottles of rootbeer on the wall 96 bottles of rootbeer Take one down and pass it around one less bottle of rootbeer on the wall
4-3. Very Merry Christmas. Write a program that uses a nested loop to display “Ho Ho Ho Merry Christmas” five times to console output. Note: Your program should use the word “Ho” only once.
Ho Ho Ho Merry Christmas Ho Ho Ho merry Christmas Ho Ho Ho Merry Christmas Ho Ho Ho Merry Christmas Ho Ho Ho Merry Christmas
BO
4-4. Number Stack Different. Write a program that uses a nested loop to generate the output shown below. Be sure that the value on the line changes with each digit displayed. For example, line 5 has the values “1”, “2”, “3”, “4”, and “5”.
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 6 1 2 3 4 5 6 7 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 9
eo
70
Copyright 2009 Cengage Learning, Inc. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Copyright 2009 Cengage Learning, Inc. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.