Control Flow in C++ (continue, break, return)

  Рет қаралды 189,350

The Cherno

The Cherno

Күн бұрын

Twitter ► / thecherno
Instagram ► / thecherno
Patreon ► / thecherno
Loops in C++ ► • Loops in C++ (for loop...
Today we're talking about control flow statements in C++. Control flow statements allow us to change the "flow" of our program so that we can change what it does under certain circumstances. This is another one of those really important fundamentals that you'll want to make sure you understand really well.
Number one strategy to learn this? Start writing some code and see what these statements do! Additionally, step through the code line-by-line using the debugger if you're still confused about what actually happens.
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

Пікірлер: 151
@ToxicityGameDev
@ToxicityGameDev 6 жыл бұрын
Honesty, the best C++ videos out there... Hands down.
@nicolerieux8593
@nicolerieux8593 5 жыл бұрын
i totally agree. these are suuuper helpful
@mastershooter64
@mastershooter64 3 жыл бұрын
@Peyton Ryker nice job using an alternate bot account to reply to your own shitty bot account
@vanity_stars1064
@vanity_stars1064 3 жыл бұрын
4 years after, It still is !
@hexiy_dev
@hexiy_dev Жыл бұрын
5 years after too!
@Gecko730
@Gecko730 9 ай бұрын
and now 6 years@@hexiy_dev
@orley1994
@orley1994 3 жыл бұрын
I have to say that: 6 years ago I started a course Computer Engineering... I studied C++ Continuously for some time and i consider myself to be a good c++ programmer. I am watching those videos just to widen all my knowledge and fill some blank spaces in my memory, and your videos are just excellent. If I were to teach c++ for someone I would definitely use your videos
@mistervoldemort7540
@mistervoldemort7540 7 жыл бұрын
When he said: "Let's take a look at break.", I first understood: "Let's take a little break." LOL
@TheElitedeath
@TheElitedeath 7 жыл бұрын
A bit off topic but I'm happy at how much the quality of programming tutorials have raised. I remember when I first started teaching myself most tutorials were people with heavy accents and bad mics trying to explain something but you had to raise the quality to 360p and squint to see the code.
@Salien1999
@Salien1999 4 жыл бұрын
When I started, they didn't even have mics. Just typed out stuff into notepad to explain what was going on. Truly dark times.
@ladyViviaen
@ladyViviaen 3 жыл бұрын
the only good tutorial i've found that is semi-144p friendly
@Bloodandeath
@Bloodandeath 3 жыл бұрын
Hey Cherno, I’ve been watching your videos for years now back when you started the 3D Java game tutorial from scratch when you were known as TheChernoProject and I just have to say thank you for everything, you have always had amazing tutorials and now that I’m starting a degree in computer science I’ll know where to look when I need help. Keep being the GOAT for programming
@radekseky4571
@radekseky4571 3 жыл бұрын
That's so awesome. Good luck with your degree!
@NphiniT
@NphiniT 9 ай бұрын
How's your degree going buddy?
@fahmiabdulaziz1300
@fahmiabdulaziz1300 Жыл бұрын
In case some of you wanted to know what "instruction pointer" is, this is the answer from Bard: ``` In C++, the instruction pointer (IP) is a register that holds the address of the next instruction to be executed. The IP is incremented after each instruction is executed, and it is used to control the flow of execution of the program. The IP is a critical part of the CPU's instruction execution pipeline. When the CPU fetches an instruction from memory, it first loads the address of the instruction into the IP. The CPU then uses the IP to access the instruction in memory and decode it. Once the instruction has been decoded, the CPU executes it. The IP is also used to implement control flow statements such as loops, conditional statements, and function calls. When a control flow statement is executed, the IP is updated to point to the next instruction to be executed. This allows the CPU to execute instructions in a non-linear order. The IP is a volatile register, which means that its value can change at any time. This is because the IP is updated whenever the CPU fetches a new instruction from memory or executes a control flow statement. The IP is an important part of the CPU's architecture, and it plays a critical role in the execution of C++ programs. ```
@radekseky4571
@radekseky4571 3 жыл бұрын
I can't overstate how much sunlight helps these videos. I'm always so happy when the next one begins and it's all bright and promising.
@ericsankey5756
@ericsankey5756 Жыл бұрын
Cherno, I am in school for Game Design, and these videos help me make sense of the confusing C++ textbook I have been reading. I am very grateful you made these videos, you are an excellent teacher!
@roja
@roja 2 жыл бұрын
Commenting for your exposure. The best channel on KZbin for C++ !!!
@shaharyarahmed5777
@shaharyarahmed5777 4 жыл бұрын
The Break Point really helps me understand!
@jairambhardwaj716
@jairambhardwaj716 3 жыл бұрын
Awesome videos ❤ Keep doing the great work 👍
@ohadchaet8519
@ohadchaet8519 4 жыл бұрын
Amazing videos. Thank you a million times
@carolinekim4144
@carolinekim4144 5 жыл бұрын
I love it.... clean and straightforward!! (Honestly, you are better than my professor... :P)
@joshuam2263
@joshuam2263 7 жыл бұрын
Another awesome video!
@sir.niklas2090
@sir.niklas2090 Жыл бұрын
I have a job technical test and have been watching these videos on 2x speed. Very possible and very easy to listen to. :D (Just gotta brush up on C++)
@h.hristov
@h.hristov 7 жыл бұрын
Thanks for the video!
@romanzitlau3739
@romanzitlau3739 5 жыл бұрын
I love you and your videos!!!
@CaNNaDark
@CaNNaDark 2 жыл бұрын
Great series! It's been years since the last time I coded c++ and this is the perfect resource to start again. Just a note on if((i + 1) % 2 == 0) I would've used just if(i%2)
@luuminhhuy9459
@luuminhhuy9459 2 жыл бұрын
Explanation please? I tried it and it worked, but I am still confused about how it worked
@dhavalgosai2572
@dhavalgosai2572 2 жыл бұрын
@@luuminhhuy9459 Because (even numbers%2) always equals to 0 , so result will always be if(0) with even numbers means conition is false and hello world and 'i' will be printed.
@nadjibam6384
@nadjibam6384 2 жыл бұрын
@@luuminhhuy9459 if (i%2) means if i%2 is not 0 (not false) execute the code
@candle-likeghost9523
@candle-likeghost9523 3 жыл бұрын
This C++ series is very satisfying (edit: correct my grammar)
@colorlord98
@colorlord98 7 жыл бұрын
You said you'll be on the back of a kangaroo :(
@mikejames6888
@mikejames6888 3 ай бұрын
good lesson!
@nesumvladi8719
@nesumvladi8719 2 жыл бұрын
Everything is perfect, even the whiskey in the background.
@deltarambo6230
@deltarambo6230 4 жыл бұрын
When you have double, triple, etc., nested-loops, then breaking out entirely from the nested-loops is somewhat cumbersome using the 'break' keyword. In that particular case, it is totally OK if you use the dreadful 'goto' keyword to break out entirely from the nested-loops via jumping towards a label strategically positioned just outside the nested-loops. In my opinion, this technique should have been mentioned alongside the control flow statements which have been presented in this video.
@Rose_Harmonic
@Rose_Harmonic 3 жыл бұрын
At least we have you to put the thought in our innocent c++ learning heads
@devgamez8527
@devgamez8527 2 жыл бұрын
Hi there! I really enjoy your tutorial. I'm no longer a total beginner, but also far from expert. I'm looking forward to keep watching next lessons. BUT, what would be really great, if you could give some practical exercise to try out and better remember new topic. or maybe point to some external source of tasks to do, while learning. Because just listening isn't very effective way to learn.
@valentinneufeld6182
@valentinneufeld6182 Жыл бұрын
Thanks!
@hamidrezarahimi6651
@hamidrezarahimi6651 3 жыл бұрын
Thank you!
@ahmedghallab5342
@ahmedghallab5342 Жыл бұрын
شكرا جزيلا ♥️ thank you
@ExVersion83
@ExVersion83 2 жыл бұрын
THANK YOU!!!
@meh1672
@meh1672 7 жыл бұрын
Nice!👍
@Zerefse
@Zerefse 10 ай бұрын
Thanks
@phantomstriker7996
@phantomstriker7996 Жыл бұрын
- Return will exit the function. (Like return 0) - Continue skips the current iteration of the loop and the control of the program goes to the next iteration - Break ends execution of the nearest enclosing loop or conditional statement in which it appears
@Samoniel2910
@Samoniel2910 2 жыл бұрын
ty
@AbdulMoiz-ho8rx
@AbdulMoiz-ho8rx 2 жыл бұрын
excellent
@Cynokine
@Cynokine 7 жыл бұрын
Good, glad these episodes keep coming up. Could you tone down the background sound a bit ?
@Cynokine
@Cynokine 7 жыл бұрын
Sorry for the confusion, I meant background music ;)
@Gaelrenaultdu06
@Gaelrenaultdu06 2 жыл бұрын
I thought the same thing, the music is a bit too loud, the rest is perfect :P
@astralchan
@astralchan 11 ай бұрын
0:46 In terms of power scaling, I would say exit() wins ^^ In terms of travel ability, I gotta give it to goto and goto labels ^^
@h.jpouya4715
@h.jpouya4715 5 жыл бұрын
over 1K like and only 8 dislike. that means huge success. great job. thank you
@asifkhan-mr8nj
@asifkhan-mr8nj 8 ай бұрын
great
@FratNightGaming
@FratNightGaming 4 жыл бұрын
at 2:50 shouldnt the program write "hello world" only when i is greater than 2, ie when i = 3 and 4. It shouldnt print out hello world when i is 0, 1, or 2, correct? I didnt understand that.
@adrianoldchannel2494
@adrianoldchannel2494 6 жыл бұрын
OK. That part kinda got me for a few seconds.
@ozgunus4161
@ozgunus4161 5 жыл бұрын
the videos are great, I start understanding more. However, please consider not to put background music for non-native speakers. It is very distracting.
@filip6472
@filip6472 2 жыл бұрын
I really just dislike for loops.While loops just make so much for sense to me.Always have.I feel like its just so much clearer.Im sure others feel the exact opposite and I don't know if either would be wrong.
@joshmarkovich5301
@joshmarkovich5301 6 жыл бұрын
best ++ tutorial
@accessdenied9393
@accessdenied9393 2 жыл бұрын
Example of how use continue: Suppose we have to write a program to print all odds from 0-20 int main(void) { for (int i = 1, n = 2; i
@EpicBunty
@EpicBunty 2 жыл бұрын
printf ? PRINTF !?? you dare bring you C programming here, child!!
@murtkhafoor464
@murtkhafoor464 3 жыл бұрын
I wanna like the vid twice
@varuntaneja7073
@varuntaneja7073 4 жыл бұрын
1:54 how did you do that can you make a video on keyboard shortcuts to help us write code faster
@lewisnorth1188
@lewisnorth1188 4 жыл бұрын
In visual studio if you hold shift and press the up or down arrow key it will do that Edit: It's Alt not shift sorry
@i12Milky
@i12Milky 6 жыл бұрын
What's this track in the background of the first 2 minutes?
@seymurmammadov3868
@seymurmammadov3868 3 жыл бұрын
Do you need to use continue? what if you just left it out, wouldn't it still work?
@jacott1382
@jacott1382 Жыл бұрын
If I use the break statement in a loop in order to get a specifiek amount or type of data, can that data still be used outside the loop by the rest of my program?
@mytech6779
@mytech6779 6 жыл бұрын
Using " return " in some type of nested function would have made it more clear, using it in main() threw me for a moment. ---- Regarding your comment about return as a normal line of its own creating dead code and compilers removing it; was the comment in regard to this specific case or more general? Because I can imagine some instruction earlier in the code that has some sort of jump or goto line X, with X being the line just after the return statement.
@mytech6779
@mytech6779 6 жыл бұрын
That was my question. I just edited it out as it was poorly written, it must have been late at night, I can not recall to what I was referring.
@weet7584
@weet7584 5 жыл бұрын
hi i wonder know how you can display the looping process on the screen without system("pause")
@zoriiginalx7544
@zoriiginalx7544 4 жыл бұрын
He's using cin.get() which awaits user input to terminate the program.
@wisteria4360
@wisteria4360 Жыл бұрын
return only has to return a value if it's in a function which isn't returning a void data type
@SuperCoolHandle94
@SuperCoolHandle94 Жыл бұрын
This video on 1.5x speed has some crazy background music ahahha
@mars3142
@mars3142 7 жыл бұрын
I would lower the volume of the music in the next episode.
@vighnesh153
@vighnesh153 4 жыл бұрын
I wouldn't
@mouzedrift3629
@mouzedrift3629 4 жыл бұрын
i would zoom out of the code
@jaz093
@jaz093 3 жыл бұрын
Background music is really annoying for tutorial videos.
@avivraviv8460
@avivraviv8460 4 жыл бұрын
i have a function that should return a struct but it has a condition to exit .. what should i return in that case?
@kallht2079
@kallht2079 2 жыл бұрын
What does the % sign do? Edit: I looked it up and I now understand it. Would have been nice to have it explained in the video though :p
@EpicBunty
@EpicBunty 2 жыл бұрын
now I gotta look it up. would have been nice if you had explained it after looking up though. to everyone else- The modulo operator, denoted by %, is an arithmetic operator. The modulo division operator produces the remainder of an integer division.
@marcusk7855
@marcusk7855 4 жыл бұрын
What about switch?
@nazar1744
@nazar1744 7 жыл бұрын
Set videospeed to 0.75
@randomguy-qr3ft
@randomguy-qr3ft 6 жыл бұрын
sounds like he made the video after drinking a lot of alcohol lmao.....
@alessandromorelli5866
@alessandromorelli5866 5 жыл бұрын
i play these at like x1.5/x2 cause i got like 1 week to learn to code AND do the project LMAO
@edd4851
@edd4851 9 ай бұрын
6:35 but how we're getting away with it in recursive functions (it returns only once)
@encryptal
@encryptal 4 жыл бұрын
whats the music he's playing?
@soniablanche5672
@soniablanche5672 6 ай бұрын
"this line of code will never be executed" unless you use goto :^)
@samdavepollard
@samdavepollard 3 жыл бұрын
buckle up boys and girls, pointers is next ....
@TheATYGamer
@TheATYGamer 4 ай бұрын
What is the %?
@nandharamya9612
@nandharamya9612 Жыл бұрын
u forgot the goto statement buddy as one of the control flows available
@jamesmnguyen
@jamesmnguyen 7 жыл бұрын
What about goto statements? Yes I know there's a hot debate on whether to use them in C++ but I find it very useful in special cases. It's better if the beginner knows it exists in the off-chance they might need it.
@sablanex
@sablanex 7 жыл бұрын
why wouldn't you use them?
@longbra
@longbra 6 жыл бұрын
You wouldn't use them so that your code remains clearer and easier to read (avoid tangled / spaghetti code). Also, you are more likely to miss variable declarations and such along the way if you aren't careful with usage. If you are using a lot of goto statements, you may want to rethink how your code is designed.
@deltarambo6230
@deltarambo6230 4 жыл бұрын
When you have double, triple, etc., nested-loops, then breaking out entirely from the nested-loops is somewhat cumbersome using the 'break' keyword. In that particular case, it is totally OK if you use the dreadful 'goto' keyword to break out entirely from the nested-loops via jumping towards a label strategically positioned just outside the nested-loops.
@chonkychungus
@chonkychungus 2 жыл бұрын
I wish these had no music :c
@skyseed3005
@skyseed3005 2 жыл бұрын
But I want to build loops with if and goto T.T
@TheRealFFS
@TheRealFFS 3 жыл бұрын
Side note: continue in a while loop can lead to somewhat different behavior than in an "equivalent" for loop (if naively implemented). for(int i=0; i < 5; ++i) { continue; // ++i will be evaluated } int i=0; while(i < 5) { continue; // infinite loop ++i; }
@372leonard
@372leonard 7 жыл бұрын
How does one write to the instruction pointer?
@tylerbertz8739
@tylerbertz8739 2 жыл бұрын
[Random] your lamp looks like a dress
@utsavpoudyal4421
@utsavpoudyal4421 4 жыл бұрын
goto???
@_sunlines_
@_sunlines_ Жыл бұрын
Hi guys im a ce freshman and wanted your help See i dont have any programing background that means im below 0 I wanted to see if theres a free toturial or maybe a comunity that could help me obtain my basic c++ skils It can be a video or maybe an article it doesnt matter it only needs to be very basicy The free part is important im strugling to pay my rent rn
@ItsTop7.
@ItsTop7. 3 жыл бұрын
what the word (log) means
@computerprogrammer7942
@computerprogrammer7942 3 жыл бұрын
He defined log to the way you print things in c++
@ItsTop7.
@ItsTop7. 3 жыл бұрын
@@computerprogrammer7942 thanks
@wesleylim5932
@wesleylim5932 3 жыл бұрын
4:23 Anyone thought their phones were ringing?
@shubhamkapoor3973
@shubhamkapoor3973 2 жыл бұрын
that background sound sucks!! I wanted to learn but now I am getting a headache, Thanks to you
@patricktorgerson5810
@patricktorgerson5810 7 жыл бұрын
"So return [...] can be absolutely anywhere in your code." Is this accurate? do you mean anywhere inside a function? Pretty sure putting return in global space or in a class declaration wouldn't make much sense...
@mrgruntlesworth
@mrgruntlesworth 5 жыл бұрын
But Cherno... what happens if I write into the instruction pointer?.
@sammariofan
@sammariofan 6 жыл бұрын
who is he and how did he do it?
@qazawatzirak5048
@qazawatzirak5048 5 жыл бұрын
What is the music at the end ?
@leonardomarinovic3492
@leonardomarinovic3492 7 жыл бұрын
SECONDDDDDDD
@Mr_cl0wn
@Mr_cl0wn 2 жыл бұрын
your videos are awesome but the background music is so disturbing.
@TheATYGamer
@TheATYGamer 4 ай бұрын
I have a Question. ❓
@eggmeister6641
@eggmeister6641 3 жыл бұрын
he forgot about goto... which is probably for the best
@reshin4978
@reshin4978 6 жыл бұрын
which c++ are u using ,is it different from turbo c++
@Deltastatics
@Deltastatics 5 жыл бұрын
He is using Visual Studio.
@biqbicle4982
@biqbicle4982 Жыл бұрын
there is no "different type of c++" all of them are the same with minor changes
@reshin4978
@reshin4978 Жыл бұрын
@@biqbicle4982 i was a newbie 4 year's ago 😂
@biqbicle4982
@biqbicle4982 Жыл бұрын
@@reshin4978 lol
@andrewmalcolm3209
@andrewmalcolm3209 5 жыл бұрын
It'd be cool if the music could not be way too loud
@KekWeren
@KekWeren Ай бұрын
its not hard to say that its checking if its even or odd you made me waste 30 minutes to understand what % meant
@MichaelDavydoff
@MichaelDavydoff 7 жыл бұрын
Hello! Can you please continue tutorial seris "How to make game engine"?
@mathewmccloskey8242
@mathewmccloskey8242 7 жыл бұрын
He has..
@MichaelDavydoff
@MichaelDavydoff 7 жыл бұрын
Also this isn't looks like proper game engine, but more like just renderer, we also need to implement Level editor to make it usable for game development
@MichaelDavydoff
@MichaelDavydoff 7 жыл бұрын
but why he stopped video tuts and then started devlogs (what is actually useless, cuz you can't follow) and then he stopped it for eon
@victorsarkisov4480
@victorsarkisov4480 4 жыл бұрын
@@MichaelDavydoff No, you don't
@MichaelDavydoff
@MichaelDavydoff 4 жыл бұрын
@@victorsarkisov4480what do you mean dude?
@rotatingdisk
@rotatingdisk 5 жыл бұрын
if only, the music could stop.
@cprn.
@cprn. 3 жыл бұрын
7:08 Wait, what? Why "maybe"!? Is there a legitimate use case for writing to `eip`?? Normally I'd call bulls**t but you were writing GPU code for a living so... I'm suspicious.
@TommySnow-uj7zn
@TommySnow-uj7zn 9 ай бұрын
you are so cute, i normally like girls, but i like you too. someone explain to me. lol
@neelimgoswami6336
@neelimgoswami6336 6 жыл бұрын
nothing left for me to understand in c++
@justafighter1346
@justafighter1346 6 жыл бұрын
There is always more
@datascienceandmachinelearn2537
@datascienceandmachinelearn2537 21 күн бұрын
everything is great except from the background music. Can you remove the music please?
@breachplanting7398
@breachplanting7398 5 жыл бұрын
how about goto xd
@Christian_for_Life86
@Christian_for_Life86 6 жыл бұрын
how you can use break, continue.......in a short if
@micheleforese9937
@micheleforese9937 7 жыл бұрын
FIRSTTTTTTT
@friedbertmoessner2625
@friedbertmoessner2625 3 жыл бұрын
The noisy background music is very much desturbing
@konradgebura3985
@konradgebura3985 5 жыл бұрын
0 / 0 = 0 tell that to a mathematician. If you do be ready to get see a lot of graphs
@FunMaths15
@FunMaths15 4 жыл бұрын
dividing by 0 = infinity(theoretically think about gradient)
@JustinK0
@JustinK0 4 жыл бұрын
skipping through a lot of these because they are general programming basics and less specific to c++
@rajkumarshukla9586
@rajkumarshukla9586 6 жыл бұрын
Messy video
@mdcomputercenter2155
@mdcomputercenter2155 Жыл бұрын
#include void main() { printf("For Loop "); for(int i = 0; i < 10; i++ ) { if( i % 2 == 0) continue; printf("%d is ODD ",i); } printf("While Loop "); int i = 0; while(i < 10) { if( i % 2 == 0) { i++; continue; } printf("%d is ODD ",i); i++; } printf("Do While Loop "); i = 0; do { if( i % 2 == 0) { i++; continue; } printf("%d is ODD ",i); i++; }while(i < 10); printf("Goto "); i = 0; repeat: if( i % 2 == 0) { i++; goto repeat; } printf("%d is ODD ",i); i++; if(i < 10) goto repeat; } is this good?
@vidhishetty4130
@vidhishetty4130 5 жыл бұрын
The background music is very distracting
POINTERS in C++
16:59
The Cherno
Рет қаралды 1 МЛН
How the C++ Compiler Works
17:55
The Cherno
Рет қаралды 787 М.
КАРМАНЧИК 2 СЕЗОН 7 СЕРИЯ ФИНАЛ
21:37
Inter Production
Рет қаралды 536 М.
I CAN’T BELIEVE I LOST 😱
00:46
Topper Guild
Рет қаралды 108 МЛН
1❤️
00:17
Nonomen ノノメン
Рет қаралды 13 МЛН
why are switch statements so HECKIN fast?
11:03
Low Level Learning
Рет қаралды 390 М.
The "auto" keyword in C++
17:04
The Cherno
Рет қаралды 200 М.
How C++ took a turn for the worse
5:03
Code Persist
Рет қаралды 262 М.
Loops in C++ (for loops, while loops)
12:20
The Cherno
Рет қаралды 398 М.
How to DEBUG C++ in VISUAL STUDIO
19:20
The Cherno
Рет қаралды 465 М.
Arrays in C++
18:31
The Cherno
Рет қаралды 417 М.
WHY did this C++ code FAIL?
38:10
The Cherno
Рет қаралды 204 М.
why is it illegal to use "goto"?
5:23
Low Level Learning
Рет қаралды 245 М.
Dynamic Arrays in C++ (std::vector)
14:14
The Cherno
Рет қаралды 373 М.
КАРМАНЧИК 2 СЕЗОН 7 СЕРИЯ ФИНАЛ
21:37
Inter Production
Рет қаралды 536 М.