Three most primary forms of loops: the while loop, the do..

Assignment Help JAVA Programming
Reference no: EM13164948

explored the world of loops we have accepted the three most primary forms of loops: the while loop, the do...while loop (even though Python does not use this loop), and the for loop. The for loop has approximately three primary implementations that will be reviewed in the next class period. To finish the chapter formally we will discuss the sentinel-controlled loop and the nested loop.

The Sentinel-Controlled Loop

Loops by themselves are indeterminate except for counting loops. That simply means that we will not know how many times the loop will execute. Sometimes there will be a programming situation in which we need the user to input as many data entries and then enter a special value to signify the end of the input session. The special value that is used is known as the sentinel. The goal of the loop is to execute the loop until that one particular sentinel value is executed. Our first approach would be to program like this.

print("Welcome to this accumulating program")

SENTINEL = -555

entry = 0
total = 0

while entry != SENTINEL:
entry = int(input("Enter a number (Enter -555 to quit): ")
total += entry

print("The total of the numbers entered: ", total)

If you were to program this and execute this, you would see one small problem. Even though it is syntactically correct (to my knowledge), the total will always be short by 555. Why? The sentinel value will have to be accounted for and added back to the total. In my estimation and that of programming experts everywhere, this is not a great programming practice. This also defeats the purpose of using it effectively.

The best way to use sentinel-controlled loops is to have the sentinel on be concerned with stopping the loop but not being a part of the data. In fact (in type disciplined languages) the sentinel should be of the data type but not a part of the data set. Since, for this course, we use Python, we can get away with even a string for the sentinel [even though that is highly unsafe]. Let's revise the above code to understand what should take place.

print("Welcome to this accumulating program")

SENTINEL = -555

total = 0
entry = int(input("Enter a number (Enter -555 to quit): ")

while entry != SENTINEL:
total += entry

entry = int(input("Enter another number (Enter -555to quit): ")

print ("The total of the numbers entered: ", total)

This programming approach is a much more disciplined approach. First the code requires the user to input the first entry before the loop. This is known as the priming input. If the first entry is the sentinel, the loop will not execute, otherwise, the first entry is processed and then all remaining inputs are required inside the loop. I call this the looping input. In this program the limitation is that -555 (or whatever you set the sentinel to) cannot be processed as data. It ends the loop. It is best when choosing a sentinel to pick a value that is far from the data set.

Your task:

Take this code and keep count of how many entries (inputs the user makes). Without trying to use arrays [since I know that you will search Google], display the average of the entries, the highest value entered and the lowest value entered.

Nested Loops

Loops are conditional structures that test a condition, execute a set of statements and then recheck the condition to see if it still exists. You are to permitted to nest loops like you can nest decisions but you have to be aware that there is a different outcome when you nest loops. Since a loop is inside another loop, the complete execution of the inner has to occur before the iteration of the outer loop. Think about minutes and seconds. We have to count 60 seconds before we can get one minute. We have to count another 60 seconds to get to two minutes. After 60 minute iterations we have accomplished an hour iteration.

for hours in range (24):
for minutes in range (60):
for seconds in range (60):
print (hours, " : ", minutes, " : ", seconds)

Now write this program and watch the execution. You should be impressed at how many executions can occur in just four lines of code. In fact that was 24 * 60 * 60 -> 86,400 executions. WOW! You should also notice how the seconds increase through all its executions before one minute iteration.

Your next task:

When I was younger we used to have composition note books that had multiplication tables that ran from 1 through 12. So you would see:

1
-----------
1 x 1 = 1
1 x 2 = 2
.
.
.
1 x 12 = 12

2
-----------
2 x 1 = 2
2 x 2 = 4
.
.
.
2 x 12 = 24
.
.
.
12
--------------
12 x 1 = 12
12 x 2 = 24
.
.
.

12 x 12 = 144

You are to use two nested loops to output the multiplication like the one printed above from 1 to 12 (both multiplicand and multiplier). Try to get your format to be like the output as well.

 

 

Reference no: EM13164948

Questions Cloud

How many grams of sodium carbonate present after reaction : A solution containing 3.50 of sodium carbonate is mixed with one containing 5.00 of silver nitrate. How many grams of sodium carbonate are present after reaction?
A method that takes a two-dimensional array : A method that takes a two-dimensional array of int's as a parameter and searches the array for the second parameter, returning true if the second parameter matches any of the integers in the array, and false otherwise.
How many grams of carbon dioxide are produced : how many grams of carbon dioxide are produced when 2.50g of sodium hydrogen carbnate reacts with excess citric acid according to the equation: 3NaHCO3 + H3C6H5O7 -> Na3C6H5O7 + 3CO2 + 3H2O.
Neurophysiological and evolutionary : Provide a framework for the theoretical concepts associated with Donald Hebb the Neurophysiological and Evolutionary write 200-300 cite references
Three most primary forms of loops: the while loop, the do.. : explored the world of loops we have accepted the three most primary forms of loops: the while loop, the do...while loop (even though Python does not use this loop), and the for loop. The for loop has approximately three primary implementations that w..
How many wheels are left over : A manufacturer of bicycles has 4819 wheels, 2305 frames, and 2250 handlebars. How many wheels are left over? How many frames are left over? How many handlebars are left over? Which part limits the production of bicycles?
Plan the testing required for the system : Plan the testing required for the system. You should consider unit, integration, and system testing in your test plan and determine who should participate in the testing.
The mode of a list of values is the score : For this project you will write a program to compute the arithmetic mean (average), median, and mode for the values read in from TopicFin.txt. The program results will be written to TopicFout.txt.
The program should ask the user to enter the student'' answer : Your program should store these correct answers in an array. (Store each question's correct answer in an element of a string array.) the program should ask the user to enter the student's answers for each of the 20 questions, which should be stored i..

Reviews

Write a Review

JAVA Programming Questions & Answers

  Write a while loop that lets the user enter a number

1) Write a while loop that lets the user enter a number. The number should then be multiplied by 10, and stored in a variable called product. The loop should then iterate as long as product contains a value less than 100.

  Implementation of memory management

Implementation of memory management

  Design a single class that expresses the commonality

Design a single class that expresses the commonality of these concepts.

  A remote web-based file storage application

Develop a web-based application. The server-side part of the application must be developed using Java Servlets (possibly including JSP). The development of the client (browser) side will typically be a combination of HTML and Javascript.

  Reads contents of two vectors

Write a program that reads contents of two vectors, and then displays the sum of these two vectors. The program should prompt the user to enter the size of the vectors first.

  Program to find maximum-minimum of sequence of values

Common task which should be done in loop is to find maximum and minimum of sequence of values. File Temps.java contains program which reads in sequence of hourly temperature readings over 24- hour period.

  Personalize the time zone application of section 24.3

Personalize the time zone application of Section 24.3. Prompt the user to log in and specify a city to be stored in a profile. The next time the user logs in, the time of their favorite city is displayed automatically.

  Java program to read line of text which ends with period

Write down the java program which will read the line of text which ends with the period, which serves as sentinel value. Show all the letters which occur in the text.

  Give at least one constructor without any parameter

We learned as popular Set implementation. Write own TreeSet class that implements following interface: You are to give at least one constructor without any parameter.

  Design an abstract data type in java

Design an abstract data type in Java that represents a musical pitch

  The program reads in names

Write a program and include the following methods. The program reads in names and ages (use 2 parallel arrays, maximum size 50) from a file called Stuff.dat.

  If there are 4 command line arguments

If there are 4 command line arguments (a b c filename.dat) then the engine should take a,b,c and store the resulting x1 and x2 in filename.dat working in silent mode (no use of screen)

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd