what is the difference between a y loop and a 4 loop?
Both of them are conditional loops. They execute till the condition mentioned in them is true. When it fails then they come out of the loop. But in for loop you can also assign a value and increment or decrement as in the syntax... For eg: for(i=0;i<10;i++) can be written equivakkently in while loop as i=0; while(i<10) { ... ... i++; } both do the same thing.
Thank you. Still unclear on the advantage of a while loop over a for loop or vice versa? Is the advantage of a for loop over a while loop that you are allowed to assign a value?
depends on the program you write.. when you only need to execute a set of statements for a given condition then we use while loop.. for instance when reversing a number.. generally we use for loops when manipulating arrays and while loops otherwise.. But having said that while loops can also be used to manipulate arrays.. but for provides better functionality..
for loop is entry controlled loop means condition provided is checked first and then the body of the loop is executed,whereas in while loop,there are 2 cases:-- 1.)while loop[entry controlled] 2.)do-while loop[exit controlled,i.e.,body of the loop gets executed first and then the condition gets checked]. Since while loop provides 2 ways to use it according to logic of your program, so it may be considered advantageous!
Thank you for the replies. As I understand it now: while and for loops are both conditional loops. conditional loops continue to execute as long as the loop condition remains true. for loops are better for manipulating arrays than while loops.
Join our real-time social learning platform and learn together with your friends!