Loops in C++ (for loops, while loops)

Поделиться
HTML-код
  • Опубликовано: 27 май 2017
  • Twitter ► / thecherno
    Instagram ► / thecherno
    Patreon ► / thecherno
    Series Playlist ► • C++
    BEST laptop for programming! ► geni.us/pakTES
    My FAVOURITE keyboard for programming! ► geni.us/zNhB
    FAVOURITE monitors for programming! ► geni.us/Ig6KBq
    MAIN Camera ► geni.us/CYUQ
    MAIN Lens ► geni.us/ZM3CmG
    Microphone ► geni.us/wqO6g7K
    Slack ► slack.thecherno.com
    Stream ► / thecherno
    Website ► www.thecherno.com
    Facebook ► / thecherno

Комментарии • 275

  • @ignacior.dealmeida8581
    @ignacior.dealmeida8581 3 года назад +274

    When your professor from one of the best universities in the US sends you to a video from the Cherno to explain a CS principle :')

    • @soccerz222
      @soccerz222 3 года назад +3

      @byu

    • @1stplace362
      @1stplace362 3 года назад +3

      wait what?? which college is that

    • @ezshroom
      @ezshroom 3 года назад +20

      @@1stplace362 That must be the best university ever.
      Or the best teacher, don't know.

    • @dackerdaw5208
      @dackerdaw5208 3 года назад +43

      That's way better tho, why waste your time explaining basic stuff that anyone could learn in 15 min by just testing or reading the docs or watch videos like this. Instead, you can teach your student stuff like problem-solving and using these tools to do something meaningful. In Uni, you should learn how to solve a problem, not how to use c++.

    • @tate_talks354
      @tate_talks354 2 года назад +1

      Fr

  • @gykg3202
    @gykg3202 3 года назад +105

    Learn more in 12 minutes than in a 80 minute lecture with my professor

    • @anungoob6450
      @anungoob6450 2 года назад +1

      Samee 🤦‍♀️🤦‍♀️

    • @guy-
      @guy- Год назад +1

      That's the beauty in good teachers on RUclips

    • @cweasegaming2692
      @cweasegaming2692 Год назад +3

      I keep seeing people saying this. Y'all are wasting your god damn money lmao

    • @sytnoff1
      @sytnoff1 4 месяца назад

      yah man they will do anything except teaching easily

  • @ETXAlienRobot201
    @ETXAlienRobot201 7 лет назад +231

    "game done, i rendered my one frame, we're done here"
    XD

    • @NeoKailthas
      @NeoKailthas 3 года назад +4

      well, you have a lot going for you with this game. it supports all platforms with almost all hardware configurations. it loads quickly. it is better than Cyberpunk. it has replay value. it levels the playing fields with speed runners. etc....

    • @slavsquatsuperstar
      @slavsquatsuperstar 3 года назад +4

      Bethesda: hold my beer

    • @suntzu1409
      @suntzu1409 3 года назад +2

      @@slavsquatsuperstar
      CDPR: ( ͡° ͜ʖ ͡°)

    • @suntzu1409
      @suntzu1409 3 года назад +1

      @@NeoKailthas most importantly its not pay to win

    • @The_Original_ZacBarton
      @The_Original_ZacBarton 3 года назад +2

      EA be like

  • @168grhp
    @168grhp 6 лет назад +12

    excellent video! perfect amount of detail and moves at a good pace.

  • @Zorrr
    @Zorrr 2 года назад +20

    For C++20 users, with the std::ranges & views library you can also use std::iota to iterate a certain bounds, eg:
    for(const auto& value : std::views::iota(0, 100))
    std::cout

  • @alexandercourtney1075
    @alexandercourtney1075 3 года назад +1

    Thank you so much! This is my first week of coding and this video really helped solidify my new knowledge. Definitely gonna check out your other videos

  • @jeremygong4190
    @jeremygong4190 2 года назад +2

    that for loop tricks are not only just beautiful, but it does also bring me the openings of viewing codes in a much bigger picture. I very carefully watched, practiced, and took notes of every single vid in your watchlist so far. And in my perspective, and in my mother tongue: “自从看了你的片子,现在我程序写得越来越浪了。。。” 🤣🤣🤣🤣😂

  • @aarushiagarwal8938
    @aarushiagarwal8938 2 года назад +6

    after watching this video I felt no need to watch another
    you have a nice talent to explain anything in just a simple way😊😊😊😀😀
    this video is really helpful and informative

  • @groverkennedy2049
    @groverkennedy2049 6 лет назад +6

    thanks man! I got sick and I missed 2 days of class and for and while loops were discussed and of course I had to miss it so when I came back I was confused af.

  • @BossBeneBaby
    @BossBeneBaby 3 года назад +4

    for each loops are also very nice even tho they are also just a fancy for loop. Anyway i just like the syntactic sugar and the legibility "do this for every element of this list/array". I just rewatch this whole series in 2021and I still love it.

    • @crashfactory
      @crashfactory Год назад +1

      +1. once i realised that c++ had a "For each" loop, i was over the moon, and really made me think about c++ in a different light. i thought the "for each" was reserved for "higher level" languages like python. not so!
      For those interested, it's
      for (int x : arr) {
      //use x in code here. arr is array of int type. change int to the type contained in the array.
      }

  • @h0lybehgeZuZ
    @h0lybehgeZuZ 4 года назад +36

    I have noticed that when writing programs that ask the user to input whether or not they want to run the program again, do-while loops are perfect for this because you always run the program at least once, but at the end, you can check the condition of whether the user wants to run the program again, and if it’s false, you don’t, or if it’s true, then the program repeats. You can do the same thing in for or while loops, but the advantage is that the compiler doesn’t need to check the condition for the first run of the loop in the do-while loop, whereas in for and while loops, you do. Very minor optimization, but it all adds up in the end. Great vid by the way.

    • @Manimanocas
      @Manimanocas 2 года назад +2

      Ty i was using while loops and this is a much better alternative

  • @deeplodh6353
    @deeplodh6353 Год назад

    bro u are amaizing i was not undrstanding loop explained in my school. this was giving me a hard time. but u saved the day. thanks for explaining things this good .

  • @syednouman2067
    @syednouman2067 2 года назад +4

    This is explained so well, thanks man

  • @unsalad
    @unsalad Год назад

    Really loving this tutorial as it generates interest, Thanks Cherno

  • @figorachman
    @figorachman 3 года назад +5

    Thank you, what an excellent explanation!

  • @loryon
    @loryon 7 лет назад +2

    Very well explained. Awesome video as usual :)

  • @ReginaldoTrindadeXXI
    @ReginaldoTrindadeXXI 3 года назад +1

    The best video series I ever had seen! Thanks From Brasil!

  • @SrbijaaBree
    @SrbijaaBree 4 года назад +17

    For loops in depth with disassembly? We're waiting for it! :D

  • @user-gs2vw1uy5v
    @user-gs2vw1uy5v 3 года назад +2

    this is the best video in youtube explains loops, KEEP GOING DUDE

  • @notdeeq
    @notdeeq 2 года назад +1

    Thank you sir love you every much for saving my day with this tutorial keep up the excellent work! 😃

  • @universebug5423
    @universebug5423 5 лет назад +127

    Bröther, may I have some Lööps?

    • @Markus_included
      @Markus_included 5 лет назад +14

      Brøther, where äre my Løøps

    • @17reason
      @17reason 3 года назад +3

      Lööplar nerde lan Cafer!

    • @nope2660
      @nope2660 3 года назад

      eqsene hırrıme lööpne defakardo!!!

  • @lbsna3232
    @lbsna3232 6 лет назад +1

    Thanks you for this best speaking and it is very easy to understand.

  • @makov2299
    @makov2299 2 года назад

    when this guy is teaching better than the professors in my university in Bulgaria. Awesome video dude. You are amazing. Thank you!

  • @peiquedq
    @peiquedq 7 лет назад +3

    Great as always :)

  • @mouhou9795
    @mouhou9795 5 лет назад +23

    ah thanks dude my teacher failed to explain what the order of the for loop actually was

  • @gijimbo1337
    @gijimbo1337 3 года назад +11

    So I feel like there's a slight difference between the while and for loops in the scope of the iterator variable.
    - The for loop creates the iterator variable at start (scoped to the for loop) and then frees/deletes it when done (out of scope).
    - The while loop requires a variable that's scoped at least one level above the loop so it doesn't get freed up when the whole loop is over.
    Am I wrong in this?

  • @leixun
    @leixun 4 года назад +76

    *My takeaways:*
    1. for loop vs while loop 8:25
    2. do while loop 10:00

  • @ZeroaccessMusic
    @ZeroaccessMusic 6 лет назад +83

    Can you make a video about best coding practices you would commonly see at big companies or what style most programmers use that would be great

    • @xl_Tacticool_SF
      @xl_Tacticool_SF 4 года назад +13

      As there are tons of conventions across all programming languages there really isn’t a defined “industry standard”
      Coding style and standards differ between companies according to their specific company policies. Larger companies tend to employ software configuration management teams that ensure process and tools are used in the manner that the business has defined.
      Hope this helps.

    • @xl_Tacticool_SF
      @xl_Tacticool_SF 3 года назад +15

      Shah Bhuiyan arrogant know it all? I didn’t realize that being honest as an engineer was being arrogant... better look in the mirror and reconsider how easily you’re offended by honesty.
      It’s called professional courtesy not arrogance. 👍🏼

    • @Dannnneh
      @Dannnneh 3 года назад +7

      @Shah Bhuiyan Are you for real?

    • @violetsnow5546
      @violetsnow5546 3 года назад +4

      @Shah Bhuiyan tf, he gave a reasonable answer.. so aggressive man.

    • @xl_Tacticool_SF
      @xl_Tacticool_SF 3 года назад +8

      The point I was trying to make is that you can’t prepare for the industry because you don’t know the standards of the company you may be hired at.
      Best thing to do is present yourself as a professional that is eager to work and excited to learn something everyday.

  • @murloc_rampage3856
    @murloc_rampage3856 10 месяцев назад

    There's also the matter of time when choosing between for and while loops. While loops take more time to be completed in many languages so in code that might need to iterate millions of times, it is best to use for loops because of the sheer speed the code can execute as well as the Big O scale.

  • @Ferocious_Imbecile
    @Ferocious_Imbecile 2 года назад

    Always the best explanations. what is normally about as pleasurable as pulling teeth is actually fun here.

  • @CalvinMaighan
    @CalvinMaighan 3 года назад +1

    Wow so glad I'm following this tutorial instead of the Standford one I almost selected

  • @fanisdeli
    @fanisdeli 7 лет назад +13

    are you still working on android support for Sparky Engine?

  • @lesterjohn583
    @lesterjohn583 5 лет назад +1

    Thanks for the vid I learned a lot from it. Can you make a video on how to get the average of integers between x and y where x and y are user inputs? Loving your videos. Lots of love from the Philippines!

  • @pradyumnkejriwal3007
    @pradyumnkejriwal3007 7 лет назад +2

    Very good vid cherno! 😘

  • @HBWT
    @HBWT 3 месяца назад

    Thank you! Now I comprehended loops in c++ better!

  • @nathanherder1414
    @nathanherder1414 5 лет назад +2

    Whoa that's a cool lamp shade

  • @rass2498
    @rass2498 4 года назад

    exactly what i was searching fr

  • @websurfer5283
    @websurfer5283 6 лет назад +1

    Great series!!

    • @mohamedatef91994
      @mohamedatef91994 6 лет назад

      I want to know the difference between them in memory ?

  • @bryansenanin7249
    @bryansenanin7249 4 года назад +2

    oh i know what is looping now thanks pretty hard for me even just a basic program im new from programming currently studying 2019 hehehe

  • @kitara_69
    @kitara_69 Год назад

    Thank you!

  • @cocoarecords
    @cocoarecords 5 лет назад

    best explanation out there

  • @TheJoshuamcgowan
    @TheJoshuamcgowan 2 года назад

    super helpful explanation. cheers bru

  • @vanshitdiwaker5077
    @vanshitdiwaker5077 7 лет назад +1

    Thanks alot bro!!!!

  • @sembaeff
    @sembaeff 5 лет назад +1

    Perfect!

  • @beluga3211
    @beluga3211 2 года назад

    This video help me a lot in understanding this concept thanks a lot btw watching in 2021

  • @jackthake7209
    @jackthake7209 6 лет назад +5

    later on in the series can you cover unit testing?

  • @lesptitsoiseaux
    @lesptitsoiseaux 4 года назад +2

    My son is 12 and we are learning c++ thanks to you! My son is interested in robotics and machine vision. Is opencv up your alley?

  • @zamanraja9531
    @zamanraja9531 2 года назад

    some sick guy!!! i understood this better than my lectuer explaining it which some sh*** powerpoint slides. Lifesaver!!

  • @lloydclintonenyia9221
    @lloydclintonenyia9221 2 года назад

    Thanks alot. It was well depicted.

  • @MxCrompli
    @MxCrompli 3 года назад +1

    I just realized when rewatching this that instead of doing while(true) { Function(); } to call a function an infinite amount of times (could be useful for game loops and etc) you could also do for( ; ; function()) which looks a bit neater in my opinion

  • @xvengence5239
    @xvengence5239 3 года назад

    you are the best dude

  • @USELESSFACTSDAILYDAY
    @USELESSFACTSDAILYDAY 2 года назад +3

    Finally someone who actually explained both for loop and while loops I was having problems understanding for 1 month I couldn't understand this guy made it so easy to explain it so well now I finally understand it 😁 thank you so much sub like ++

  • @bryce8793
    @bryce8793 5 лет назад

    awesome vid cheers

  • @krish-ut9de
    @krish-ut9de 2 года назад

    Thank you a lot.

  • @cancetin2652
    @cancetin2652 3 года назад

    Thank you

  • @blackfun4448
    @blackfun4448 3 года назад

    best cpp tutorial ever!

  • @sengetomakama7072
    @sengetomakama7072 2 года назад

    Thanks for this video broo... can you please do more on nested loops

  • @jensknaack2593
    @jensknaack2593 4 года назад

    I think the usage of i, j ... as loop-variables comes from the past: In Fortran variables with names starting I, J, K, L, M and N are integer variables.

  • @pierrebleile2457
    @pierrebleile2457 5 лет назад +4

    Playing with this, I found that the "i" variable declared in the for statement goes out of scope when the for loop exits. It's worth mentioning. Also, does the for loop declared "i" variable have a performance advantage over one created before the loop? (ie is it a register variable, not stack variable?)

    • @drwisdom1
      @drwisdom1 4 года назад +1

      I have been programming in C and C++ since 1982. The only change in the language standard that caused my code to fail to compile and forced me to make changes to my code base was when they changed the scope of variables declared in for loops. Used to be variables declared in for loops were outside of the loop scope, but they changed it so those variables were inside loop scope (which is really the correct way when you think about it). I always declared my loop variables in the for loop so if I had two successive loops using the same variable i, then the second loop could no longer see i and I had to declare i above the first for loop rather than within. I still declare my variables inside for loops except when the previous occurs.

  • @samunemeth
    @samunemeth 3 года назад

    I is used because you can think of a for loop like the x-axis of a coordinate system. which is represented with I.

  • @kretoskim8555
    @kretoskim8555 6 лет назад +2

    Very helpful thanks..though I didn't get the "while loop" good...great thanks though

  • @user-pz5dz4mo8x
    @user-pz5dz4mo8x 2 года назад

    2021 is here, still works

  • @RenkoGSL
    @RenkoGSL 3 года назад

    I noticed you stretching your wrists and went to Japan, do you usually do just one wrist stretch? Or do you know about the other one? Nikkiyo and kategashi are the two that complement each other well.

  • @20thCB
    @20thCB 4 года назад

    Really should have talked about range-based for loops eg for (int n : numbers) as they are extremely common since C++11.

  • @AbdulMoiz-ho8rx
    @AbdulMoiz-ho8rx 2 года назад

    excellent

  • @samnewman7321
    @samnewman7321 7 лет назад +10

    I'm curious, there was no mention of range based for loops, perhaps that would have required too much additional discussion about the related data structures 😬

    • @jamesmnguyen
      @jamesmnguyen 7 лет назад +1

      Sam Newman
      They're the same thing as a regular for loop. Just syntax sugar. But if he were to go into them he would have to explain iterators.

    • @samnewman7321
      @samnewman7321 7 лет назад

      James Nguyen yeah that was kind of what I eluded to. I guess from my perspective I primarily write JavaScript (nodejs) and since the addition of `for of` loops to the language it has been my goto, but this is an easier sell, despite it also being built on the principle of iterators. It's not quite the same as directly iterating over memory allocation though 😊

  • @rezamehrzad5128
    @rezamehrzad5128 3 года назад

    Feburary 2021? Still the GOAT.

  • @hameedahmadzai9768
    @hameedahmadzai9768 9 месяцев назад

    Thanks bro,

  • @prochchhannomahmad8298
    @prochchhannomahmad8298 3 года назад

    thanks

  • @pranav9339
    @pranav9339 Год назад

    The whisky in the background table looks delicious. Good job tho.

  • @rajeshakhade7346
    @rajeshakhade7346 6 лет назад

    How and on what based the condition is true ??? How the compiler comes to know about value of i variable??

  • @georgecristianiordan3712
    @georgecristianiordan3712 5 лет назад +7

    Finally one explanation that i can f**king understand, sorry for swearing but man, you explained and made me understand what others couldn`t with only one example. You are the master, thank you!!!

    • @Laevatei1nn
      @Laevatei1nn 3 года назад

      Why did you swear then sorry?you can just not swear you know

  • @chrissumrall8874
    @chrissumrall8874 6 лет назад +1

    wow this was the best tutorial ive ever seen. all other tutorials go wayyy too fast and dont break it down. they assume we know a lot that we dont

  • @Popart-xh2fd
    @Popart-xh2fd 3 года назад

    I think that if you use ++i instead if i++ in the for loop, the increment is done before the i

  • @zakiali8518
    @zakiali8518 5 месяцев назад

    Your background music is awesome! Where do you pull them from?

  • @stellarestuary589
    @stellarestuary589 6 лет назад

    Thank you so much for explaining do while loops, I finally understand them now.

  • @_MK81
    @_MK81 3 года назад +1

    The Cherno: for loops are actually really, really flexible.
    Python: hold my beer.

  • @AgentM124
    @AgentM124 7 лет назад

    welll, i would not get out of scope after the loop ended, so the value is still accessible and you can use i outside the loop that way :p

  • @divinevine9413
    @divinevine9413 6 лет назад +5

    i think: this is great even though he talks a bit fast thanks anyway :)

  • @javiercampos1668
    @javiercampos1668 3 года назад +1

    I got more from this than my college classes lol

  • @elhamajmotgir2470
    @elhamajmotgir2470 6 лет назад +3

    i looped this video

  • @feezy243
    @feezy243 4 года назад

    I need help with the logic of using for loops to draw triangles.

  • @crystalwulf1800
    @crystalwulf1800 7 лет назад +1

    1:26 "Video up here". Which video? Where?

  • @cloudyplayzyt1679
    @cloudyplayzyt1679 Год назад

    If I wanted to ask the user to enter the input (their name) with the while and for loop,How would I do that?

  • @BEN9116
    @BEN9116 6 месяцев назад +1

    can you do an updated version of this!

  • @GoldenBeholden
    @GoldenBeholden 4 года назад

    Your lamp looks like a renaissance painting.

  • @lbsna3232
    @lbsna3232 6 лет назад

    Which software you are using because I have turboc8

  • @treya111
    @treya111 5 месяцев назад

    so at the end of this vid you said when that indepth vid comes out you'd put the link in the description but I didn't see it, so did you do that indepth vid in the later part of this series?

  • @mohamedatef91994
    @mohamedatef91994 6 лет назад

    I want to know the difference between them in memory ?

  • @rifathossain328
    @rifathossain328 4 года назад

    can we take input from user while loop running??

  • @mattigor5476
    @mattigor5476 2 года назад

    Excelente video omg!!! What IDE did you use in the video??

    • @rtdietrich
      @rtdietrich 2 года назад +1

      VisualStudio (not VisualStudioCode)

  • @shivamshukla932
    @shivamshukla932 5 лет назад

    Sir I want to make plagiarism.. What should I choose python or c++.. Pls let me know.. Rply is appreciated..

  • @krec348
    @krec348 7 лет назад +1

    I arrived very soon this time xD

  • @georgei2480
    @georgei2480 4 года назад

    @
    The Cherno. Is there a place I can get a hoodie, pillow or teeshirt of yours?

  • @MateuszSwieszek
    @MateuszSwieszek 5 лет назад

    what about for each loop?

  • @rafailand3239
    @rafailand3239 6 лет назад

    Does for(char i = 0; i

    • @undac9590
      @undac9590 6 лет назад

      Char is represented on 1byte (8bits; 2^8=256), which means that it can represent the [-127;127] interval (127 negative numbers + 127 positive numbers + number zero + sign bit = 256 values). As you can imagine, you often need to loop more than 256 times. Besides, the size difference between a 1byte (char) and a 4bytes (int) variable is irrelevant from any point of view.

  • @pawprinting
    @pawprinting 3 года назад

    Is there any way to delay time inbetween these loops?

  • @xXtrollplayerXx
    @xXtrollplayerXx Год назад

    Great video! Whats the name of the song in the background?

  • @reshin4978
    @reshin4978 6 лет назад

    will u do a video o sorting pls (selection sort, bubble sort)

    • @mohamedatef91994
      @mohamedatef91994 6 лет назад

      I want to know the difference between them in memory ?

  • @taneja_unchained
    @taneja_unchained 6 лет назад +91

    You never drink that scotch right? :p

    • @TheMastercheeff
      @TheMastercheeff 5 лет назад +15

      Take a shot every time Cherno says Hello World!

    • @NewInfinityRecursion
      @NewInfinityRecursion 5 лет назад +3

      @@TheMastercheeff I did get the same thoughts xD

    • @nickeax
      @nickeax 5 лет назад +18

      A new bottle for every video maybe?

    • @Hydro1738
      @Hydro1738 5 лет назад +5

      Is a new bottle every day!