Convert A For Loop To A While Loop | C Programming Tutorial

Sdílet
Vložit
  • čas přidán 21. 08. 2022
  • 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!

Komentáře • 23

  • @karimshariff7379
    @karimshariff7379 Před rokem

    A really great way to teach about both kinds of loops!

  • @KnottyDoctor
    @KnottyDoctor Před 5 měsíci +5

    just a detail, but I think the scope of the variable i is not the same.

    • @antekpostola765
      @antekpostola765 Před 4 měsíci +1

      The scope is different, the declaration of i and the while loop should really be inside {}

  • @tjdewolff5104
    @tjdewolff5104 Před 5 měsíci +3

    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.

  • @aknsagdic9388
    @aknsagdic9388 Před 18 dny

    Excellent.

  • @zoquevil4792
    @zoquevil4792 Před rokem +1

    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

    • @PortfolioCourses
      @PortfolioCourses  Před rokem +2

      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?

    • @zoquevil4792
      @zoquevil4792 Před rokem

      @@PortfolioCourses Thanks sir , I'll come back when I have a specific case

  • @juanmacias5922
    @juanmacias5922 Před rokem

    Great video, and explanation. But I have to ask, why would you want to change it from a for loop, to a while loop?

    • @PortfolioCourses
      @PortfolioCourses  Před rokem +1

      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. :-)

    • @juanmacias5922
      @juanmacias5922 Před rokem

      @@PortfolioCourses Ahhhh okay, thanks!

    • @PortfolioCourses
      @PortfolioCourses  Před rokem

      @@juanmacias5922 You're welcome! 😀

    • @TheSulross
      @TheSulross Před 5 měsíci

      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.

  • @Mnogojazyk
    @Mnogojazyk Před rokem

    Is there an advantage of one over the other? Or is one an alternative to the other?

    • @PortfolioCourses
      @PortfolioCourses  Před rokem +1

      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. :-)

    • @Mnogojazyk
      @Mnogojazyk Před rokem

      @@PortfolioCourses, acknowledged. Thanks!

    • @PortfolioCourses
      @PortfolioCourses  Před rokem

      @@Mnogojazyk You're welcome! 😀

    • @CaptainDangeax
      @CaptainDangeax Před 5 měsíci +1

      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

  • @mjamilgujjar2352
    @mjamilgujjar2352 Před rokem

    Plz,discuss do while into for loop

    • @PortfolioCourses
      @PortfolioCourses  Před rokem

      That is a good idea for a video, I have added that to my list of video ideas. :-)

  • @harge4091
    @harge4091 Před rokem

    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

    • @PortfolioCourses
      @PortfolioCourses  Před rokem

      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. :-)