Question

In: Computer Science

For each of the following answers, determine whether the answer better describes a for loop or a while loop.

For each of the following answers, determine whether the answer better describes a for loop or a while loop.
It performs one iteration for every value in a sequence.

It uses a loop variable.

It continues the loop until the condition becomes false.

It continues the loop until reaching the end of the sequence.

It can easily cause infinite loops.

The exact number of iterations are usally known when starting the loop.

Solutions

Expert Solution

both for loops and while loops are using interchangeably.. for loop provide more readability than the while loop as we can initialize, check a condition and increment in a single line.. usually its best practice to use a for loop when we know how many times we want that loop to execute and while loop when we don't know how many iterations it takes to complete..

it performs one iteration for every value in a sequence:

it is for loop..

It uses a loop variable:

it's a for loop.. for loop always need a loop variable to create.. but while loop doesn't need it it can use the existing variable as a loop variable

It continues the loop until the condition becomes false:

While loop.. i know that both for and while run untill the condition becomes false.. but in case of while loop it deals with the actual condition not just some variable like for loop

It continues the loop until reaching the end of the sequence:

it's a for loop.

It can easily cause infinite loops:

While loop.. i know that both for and while loops can cause infinite loops but in while loop condition the chances are high at stake due to the condition variables can be used in program and they can be behave abruptly sometimes

The exact number of iterations are usally known when starting the loop:

its a for loop.. ya it's always best practice that to go with the for loop when we know how many iterations exactly we are going to do..


Related Solutions

Determine whether each of the following statements is TRUE or FALSE. Justify all the answers in...
Determine whether each of the following statements is TRUE or FALSE. Justify all the answers in few sentences. 1. In the Ricardian model with 2 goods (Wine and Cheese) and 2 countries (Home and Foreign) and (except otherwise noted) identical preferences between countries: (a) If both countries have the same number of workers and Home has absolute advantages in both goods then Home must have a higher wage. (b) The Home country is never made worse off by productivity growth...
Determine whether each of the following statements is TRUE or FALSE. Justify all the answers in...
Determine whether each of the following statements is TRUE or FALSE. Justify all the answers in few sentences. 1. In the Ricardian model with 2 goods (Wine and Cheese) and 2 countries (Home and Foreign) and (except otherwise noted) identical preferences between countries: (a) If both countries have the same number of workers and Home has absolute advantages in both goods then Home must have a higher wage. (b) The Home country is never made worse off by productivity growth...
Study the following code with a while-loop and convert it to a for-loop (fill in the...
Study the following code with a while-loop and convert it to a for-loop (fill in the blanks). int i=4, result=1; while(i>1) { result *= i; i--; } The following for-loop performs the same functionality: int result=1; for (__________ i=4; i _________1;____________) { result *= i; }
For each of the following characteristics, say whether it describes a monopoly firm,
For each of the following characteristics, say whether it describes a monopoly firm, a monopolistically competitive firm, both, or neither. Characteristic: Monopoly /Monopolistically Competitive1. Faces a downward-sloping demand curve2. Has marginal revenue less than price3. Faces the entry of new firms selling similar products4. Earns economic profit in the long run5. Equates marginal revenue and marginal cost6. Produces the socially efficient quantity of output
Determine whether each of the following statements is true or false. Justify your answer for any...
Determine whether each of the following statements is true or false. Justify your answer for any that you think are false. a) The margin of error for a 95% confidence interval for the population proportion p increases as the sample size increases. b) The margin of error for a confidence interval for the population proportion p, based on a specified sample size n, increases as the confidence level decreases . c) The margin of error for a 95% confidence interval...
Determine whether each of the following is an internal control strength or weakness. Explain your answer....
Determine whether each of the following is an internal control strength or weakness. Explain your answer. 1. Receiving department manager places an order with the vendor for merchandise inventory. 2. A voucher is used to certify a transaction and authorize recording of the liability. 3. Goods are received by the receiving department and documented with a receiving report. 4. Accounting department pays the vendor's invoice before goods are received. 5. Invoices are paid after completion of the invoice approval documenting...
Determine whether each system is LTI, causal and/or stable and with or without memory. Justify answers....
Determine whether each system is LTI, causal and/or stable and with or without memory. Justify answers. a) y[n + 1] + 4y[n] = 3x[n + 1] - x[n] b) y[n] = nx[2n]
Determine whether each system is LTI, causal and/or stable and with or without memory. Justify answers....
Determine whether each system is LTI, causal and/or stable and with or without memory. Justify answers. a) y(t) =sen(x(t)) b) y(t) = dx/dt + x(t)
Re-write following while loop into Java statements that use a Do-while loop. Your final code should...
Re-write following while loop into Java statements that use a Do-while loop. Your final code should result in the same output as the original code below. int total = 0; while(total<100) { System.out.println("you can still buy for"+(100-total)+"Dollars"); total=total+5; }
Determine the electron configuration and whether each of the following is paramagnetic. Also, determine if the...
Determine the electron configuration and whether each of the following is paramagnetic. Also, determine if the complex undergoes a Jahn-Teller distortion. 1. [Fe(CN)6]4- 2. [Fe(CN)6]3- 3. [Co(H2O)6]3+ 4. [CoF6]3- 5. [Ni(CO)6]2+
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT