Question

In: Computer Science

What is the difference between a for loop and a while loop? When is it ideal...

What is the difference between a for loop and a while loop? When is it ideal to use a for loop, and when is it ideal to use a while loop? Can they be used interchangeably? Does it depend on the circumstance? Furthermore, what is your professional opinion on the advisability of the absence of the do while loop in the MATLAB programming language? If you use outside resources or ideas that are not your own to help make your case, be sure that they are properly cited in the citation style of your choice.

Consider a program involving at least one while loop that could be used in your (intended) field or that could be of use to you as a student. This program should be distinct from any other program you have discussed in a discussion previously.

First, in plain English, describe the program and how it works. If the program is especially complex, you may instead examine a smaller discernible section of the program for this discussion.

Based on your plain English description of the program, share one or more code fragments that you write for this discussion post to illustrate the concept of at least one while loop in your program. If the program is especially complex, you may instead share a smaller discernible section of the program for this discussion, but be sure that the subset of the program includes at least one while loop.

Solutions

Expert Solution

1) The difference between a for loop and a while loop are as follows:

  • The structure of for loop is : for(initialisation;condition;iteration) { //body for 'for' loop}

The structure for while loop is : while(condition) { //body for 'while' loop }

  • In for loop the initialisation step once done is never repeated. In while loop initialization is done during condition check, hence is repeated in every iteration.
  • In for loop the number of iterations are lnown from before-hand but in while loop the number of iterartions are not exactly known.
  • If the iteration is not mentioned then the 'for' loop iterates infinitely; in 'while' loop the absence of iterartion step provides a compilation error.
  • The iteration step is written at the top in 'for' loop but in 'while loop it's written anywhere in it's body.

2) Some basic situations in which 'for' loop can be used are as follows :

  • When the number of iterations is known
  • When data structure used is an array
  • When the working of the loops is simple

Some situations where 'while' loop is used are:

  • The number of iterations for the loop is unknown
  • In programs having user inputs and where use cases are implemented
  • While reading a file into a variable
  • When the incrementing variabe is non-standard

'for' and 'while' loops can be used interchangebly because both are used for iterating purposes but 'for' loops are prefered for simple logics, known number of iterations and it makes the code more lucid and clean.

3) According to me, the absence of do-while loop in MATLAB makes coding all the way more tasking. The logic can be implemented by using 'while' loop in MATLAB which is calculated only once.


Related Solutions

Open Average Test Scores while loop, comment out the while loop and add a for loop...
Open Average Test Scores while loop, comment out the while loop and add a for loop that averages 4 test scores. Code C# While loop code using System; class Program { static void Main() { int count = 0, total = 0, number;    while (count < 3) { Console.Write("Enter a number: "); number = Convert.ToInt32(Console.ReadLine()); total += number; count++; }    double average = total / 3.0; Console.Write("Average = " + average.ToString("####0.00")); } }
What is the difference between an ideal standard and a practical standard? How can a standard...
What is the difference between an ideal standard and a practical standard? How can a standard cost system help manufacturers meet global competition?
Q17. With a sketch explain what is the difference between REAL as opposed to IDEAL algorithms?...
Q17. With a sketch explain what is the difference between REAL as opposed to IDEAL algorithms? When would you choose between REAL as opposed to IDEAL algorithms?                                                                                                                                                                                 Q21. Does a FEED FORWARD controller have PID settings?       
What is the difference between isothermal and adiabatic? 300 cm3 of an ideal diatomic gas at...
What is the difference between isothermal and adiabatic? 300 cm3 of an ideal diatomic gas at a pressure of 1 atmosphere and a temperature of 300 K (i.e. room temperature) is contained by a cylinder with a piston top. A camshaft turns and drives the piston further into the cylinder, reducing the volume to 100 cm3 . (a) What is the final pressure if this compression takes place isothermally? What is the final pressure and temperature if the compression takes...
In the context of decision making theories, describe the difference between open-loop and closed loop models.
In the context of decision making theories, describe the difference between open-loop and closed loop models.
Modify the previous program to use the Do-While Loop instead of the While Loop. This version...
Modify the previous program to use the Do-While Loop instead of the While Loop. This version of the program will ask the user if they wish to enter another name and accept a Y or N answer. Remove the "exit" requirement from before. Output: Enter the full name of a person that can serve as a reference: [user types: Bob Smith] Bob Smith is reference #1 Would you like to enter another name (Y or N)? [user types: y] Enter...
l. what is the difference between a real independent voltage source and an ideal independent voltage...
l. what is the difference between a real independent voltage source and an ideal independent voltage source? Give an example of a real voltage source and explain how it differs from the ideal. 2. What does it mean when we say that a circuit containing multiple sources is invalid?Give an example of such a circuit and explain why it is invalid. 3. What is resistance? What is a node? What is a closed loop? 4. Write the equations for Ohm's...
Using a while loop. Write a JAVA program that asks a user for an integer between...
Using a while loop. Write a JAVA program that asks a user for an integer between 1 and 9. Using the user input, print out the Fibonaccci series that contains that number of terms. Sample output: How many terms would like printed out for the Fibonacci Sequence? 7 Fibonacci Series of 7 numbers: 0 1 1 2 3 5 8
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; }
C language and it has to be a while loop or a for loop. Use simple...
C language and it has to be a while loop or a for loop. Use simple short comments to walk through your code. Use indentations to make your code visibly clear and easy to follow. Make the output display of your program visually appealing. There is 10 points deduction for not following proper submission structure. An integer n is divisible by 9 if the sum of its digits is divisible by 9. Develop a program that: Would read an input...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT