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
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 :')
@byu
wait what?? which college is that
@@1stplace362 That must be the best university ever.
Or the best teacher, don't know.
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++.
Fr
Learn more in 12 minutes than in a 80 minute lecture with my professor
Samee 🤦♀️🤦♀️
That's the beauty in good teachers on RUclips
I keep seeing people saying this. Y'all are wasting your god damn money lmao
yah man they will do anything except teaching easily
"game done, i rendered my one frame, we're done here"
XD
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....
Bethesda: hold my beer
@@slavsquatsuperstar
CDPR: ( ͡° ͜ʖ ͡°)
@@NeoKailthas most importantly its not pay to win
EA be like
excellent video! perfect amount of detail and moves at a good pace.
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
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
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: “自从看了你的片子,现在我程序写得越来越浪了。。。” 🤣🤣🤣🤣😂
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
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.
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.
+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.
}
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.
Ty i was using while loops and this is a much better alternative
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 .
This is explained so well, thanks man
Really loving this tutorial as it generates interest, Thanks Cherno
Thank you, what an excellent explanation!
Very well explained. Awesome video as usual :)
The best video series I ever had seen! Thanks From Brasil!
For loops in depth with disassembly? We're waiting for it! :D
Yeah very much
this is the best video in youtube explains loops, KEEP GOING DUDE
+ ITS THE SHORTEST ONE!
Thank you sir love you every much for saving my day with this tutorial keep up the excellent work! 😃
Bröther, may I have some Lööps?
Brøther, where äre my Løøps
Lööplar nerde lan Cafer!
eqsene hırrıme lööpne defakardo!!!
Thanks you for this best speaking and it is very easy to understand.
when this guy is teaching better than the professors in my university in Bulgaria. Awesome video dude. You are amazing. Thank you!
Great as always :)
ah thanks dude my teacher failed to explain what the order of the for loop actually was
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?
*My takeaways:*
1. for loop vs while loop 8:25
2. do while loop 10:00
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
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.
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. 👍🏼
@Shah Bhuiyan Are you for real?
@Shah Bhuiyan tf, he gave a reasonable answer.. so aggressive man.
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.
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.
Always the best explanations. what is normally about as pleasurable as pulling teeth is actually fun here.
Wow so glad I'm following this tutorial instead of the Standford one I almost selected
are you still working on android support for Sparky Engine?
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!
Very good vid cherno! 😘
Thank you! Now I comprehended loops in c++ better!
Whoa that's a cool lamp shade
exactly what i was searching fr
Great series!!
I want to know the difference between them in memory ?
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
Thank you!
best explanation out there
super helpful explanation. cheers bru
Thanks alot bro!!!!
Perfect!
This video help me a lot in understanding this concept thanks a lot btw watching in 2021
later on in the series can you cover unit testing?
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?
some sick guy!!! i understood this better than my lectuer explaining it which some sh*** powerpoint slides. Lifesaver!!
Thanks alot. It was well depicted.
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
you are the best dude
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 ++
awesome vid cheers
Thank you a lot.
Thank you
best cpp tutorial ever!
Thanks for this video broo... can you please do more on nested loops
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.
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?)
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.
I is used because you can think of a for loop like the x-axis of a coordinate system. which is represented with I.
Very helpful thanks..though I didn't get the "while loop" good...great thanks though
2021 is here, still works
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.
Really should have talked about range-based for loops eg for (int n : numbers) as they are extremely common since C++11.
excellent
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 😬
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.
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 😊
Feburary 2021? Still the GOAT.
Thanks bro,
thanks
The whisky in the background table looks delicious. Good job tho.
How and on what based the condition is true ??? How the compiler comes to know about value of i variable??
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!!!
Why did you swear then sorry?you can just not swear you know
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
I think that if you use ++i instead if i++ in the for loop, the increment is done before the i
Your background music is awesome! Where do you pull them from?
Thank you so much for explaining do while loops, I finally understand them now.
The Cherno: for loops are actually really, really flexible.
Python: hold my beer.
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
i think: this is great even though he talks a bit fast thanks anyway :)
I got more from this than my college classes lol
i looped this video
ayy
I need help with the logic of using for loops to draw triangles.
1:26 "Video up here". Which video? Where?
If I wanted to ask the user to enter the input (their name) with the while and for loop,How would I do that?
can you do an updated version of this!
Your lamp looks like a renaissance painting.
Which software you are using because I have turboc8
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?
I want to know the difference between them in memory ?
can we take input from user while loop running??
Excelente video omg!!! What IDE did you use in the video??
VisualStudio (not VisualStudioCode)
Sir I want to make plagiarism.. What should I choose python or c++.. Pls let me know.. Rply is appreciated..
I arrived very soon this time xD
@
The Cherno. Is there a place I can get a hoodie, pillow or teeshirt of yours?
Don't make it weird.
what about for each loop?
Does for(char i = 0; i
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.
Is there any way to delay time inbetween these loops?
Great video! Whats the name of the song in the background?
will u do a video o sorting pls (selection sort, bubble sort)
I want to know the difference between them in memory ?
You never drink that scotch right? :p
Take a shot every time Cherno says Hello World!
@@TheMastercheeff I did get the same thoughts xD
A new bottle for every video maybe?
Is a new bottle every day!