How to convert a for loop to a while loop in C. Source code: github.com/portfoliocourses/c.... Check out www.portfoliocourses.com to build a portfolio that will impress employers!
When I was first thaught 'C', some 4 decades ago, my tutor advised me to use 'for' only when the exact number of iterations is known the moment the loop starts. In all other cases use 'while'. The use of 'do while' is highly discouraged, not because of it not being right, but because it produces a quite more complex executable. Often for no reason.
Sometimes we get confused, especially when we have an infinite loop, but with this very good transposition of every single property of each loop, we will never be lost again.! I think the while loop is more interesting than the for loop... it is closer to our way of thinking and solving a problem but the trap is when we fall into the infinite loop and the stack overflow!!! is there gonna be examples of the differents cases using a while loop? Thanks
I like to use for loops whenever there is a counter variable because I can see all the important information in the first line to help me know how the loop will execute. For other situations a while loop can be better. :-) There is this video on while loops: czcams.com/video/Ceq7Cq1I0sc/video.html. And a lot of the examples in the examples playlist use while loops. Is there anything in particular you would like to know about while loops? Or any particular examples you would like to see?
That’s a great question Juan! :-) Often times students will be asked to do this as part of assignments and tests to see if they really understand how the different kinds of loops work, so the video is mostly to help those studies. That said we might switch a for loop to a while loop to conform to some kind of coding standard, but that’s unlikely in practice. :-)
let's say that the loop body has a test condition that might cause the loop to break iteration prematurely. Well, there may be code that follows the loop that needs to know how far the loop iteration progessed to - the answer is in the loop index value. Hence the declaration of the index variable would need to be hoisted out of the loop lexical scope context so the code following the loop can examine it. In practice I would just lift the initialization section up out of the for loop as the statement that immediately precedes the loop (as a variable declaration that is initialized). But for aesthetic reasons some may not like such for loops that have an empty initialization section, and prefer it be a while loop instead. This kind of thing is not entirely contived - it can come up from time to time. But now one understands that a for loop is a specially structured manner of loop - the essential test expression no different than the while loop test expression.
The for loop is more suited to situations where we have a counter variable because it puts all the relevant information for the counter variable on one line. But everything we can do with the one loop we can do with the other, so sometimes it’s just a matter if preference too. :-)
At compilation time, it's actually the same machine language code. You may use a for loop to simulate a while loop, with no initialisation ; the continue / exit condition ; no increment
A really great way to teach about both kinds of loops!
I'm glad you enjoyed it Karim! :-)
just a detail, but I think the scope of the variable i is not the same.
The scope is different, the declaration of i and the while loop should really be inside {}
When I was first thaught 'C', some 4 decades ago, my tutor advised me to use 'for' only when the exact number of iterations is known the moment the loop starts. In all other cases use 'while'. The use of 'do while' is highly discouraged, not because of it not being right, but because it produces a quite more complex executable. Often for no reason.
Excellent.
Sometimes we get confused, especially when we have an infinite loop, but with this very good transposition of every single property of each loop, we will never be lost again.!
I think the while loop is more interesting than the for loop... it is closer to our way of thinking and solving a problem but the trap is when we fall into the infinite loop and the stack overflow!!!
is there gonna be examples of the differents cases using a while loop? Thanks
I like to use for loops whenever there is a counter variable because I can see all the important information in the first line to help me know how the loop will execute. For other situations a while loop can be better. :-) There is this video on while loops: czcams.com/video/Ceq7Cq1I0sc/video.html. And a lot of the examples in the examples playlist use while loops. Is there anything in particular you would like to know about while loops? Or any particular examples you would like to see?
@@PortfolioCourses Thanks sir , I'll come back when I have a specific case
Great video, and explanation. But I have to ask, why would you want to change it from a for loop, to a while loop?
That’s a great question Juan! :-) Often times students will be asked to do this as part of assignments and tests to see if they really understand how the different kinds of loops work, so the video is mostly to help those studies. That said we might switch a for loop to a while loop to conform to some kind of coding standard, but that’s unlikely in practice. :-)
@@PortfolioCourses Ahhhh okay, thanks!
@@juanmacias5922 You're welcome! 😀
let's say that the loop body has a test condition that might cause the loop to break iteration prematurely.
Well, there may be code that follows the loop that needs to know how far the loop iteration progessed to - the answer is in the loop index value. Hence the declaration of the index variable would need to be hoisted out of the loop lexical scope context so the code following the loop can examine it.
In practice I would just lift the initialization section up out of the for loop as the statement that immediately precedes the loop (as a variable declaration that is initialized). But for aesthetic reasons some may not like such for loops that have an empty initialization section, and prefer it be a while loop instead.
This kind of thing is not entirely contived - it can come up from time to time.
But now one understands that a for loop is a specially structured manner of loop - the essential test expression no different than the while loop test expression.
Is there an advantage of one over the other? Or is one an alternative to the other?
The for loop is more suited to situations where we have a counter variable because it puts all the relevant information for the counter variable on one line. But everything we can do with the one loop we can do with the other, so sometimes it’s just a matter if preference too. :-)
@@PortfolioCourses, acknowledged. Thanks!
@@Mnogojazyk You're welcome! 😀
At compilation time, it's actually the same machine language code. You may use a for loop to simulate a while loop, with no initialisation ; the continue / exit condition ; no increment
Plz,discuss do while into for loop
That is a good idea for a video, I have added that to my list of video ideas. :-)
I love to have funs with for loops i made without condition and then inside it i made if to break loop there is no programer to never do this
I agree it’s fun to make loops structures that way with “while (true)” type conditions that “break” at different points when it makes sense. :-)