In: Computer Science
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.
1) The difference between a for loop and a while loop are as follows:
The structure for while loop is : while(condition) { //body for 'while' loop }
2) Some basic situations in which 'for' loop can be used are as follows :
Some situations where 'while' loop is used are:
'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.