Control Flow in C++ | Break continue return | 2021

Control Flow

Control Flow 47 swept with loops somehow what they kind of give us a little bit more control as to how these lives actually run what kind of got 3 main control place that means that we can use continues to break and return and they do different things continue can only be used inside alert and basically continue says go to the next iteration obviously if there is 1 otherwise it’ll just and below break is primarily used in loops however also T. as in switch statements basically break just says get out of the loop and the loop and finally return is for the most powerful 1 if you want to consider that way because the time will just get out of your function entirely so if you’re in a function and you hit tank what you will tag that the function of course the functions which actually need to return a value we type in our tiny conscious have retained by itself for 10 by itself is on the way to get function needs to return a value when you type in return you need to live without you all right not much else let’s just jump in and take a look at some examples so this is a program that we had in the last episode please give that around so. Control Flow Control Flow Control Flow


We have hello world printing a bunch of times and then of course we had a bit of a while loop which is nothing in the wallet that does nothing else get rid of some of these because we don’t really need them anyone at all came from a previous program is pretty much that folder so the first control force them to look at is continue will skip to the next iteration of the folder sorry if I ride continued Bahia just like this it will actually not intact the behavior of the full if I don’t because that is already the end of the fall it when it hits that line it’s going to continue to the next direction anyway so this doesn’t modify the behavior at all however if I do something like if I monitored because the error and then have a continued bad. And on my program you’ll see the basically what’s happening is it’s skipping every second generation starting with the first 1 Sir I better way to illustrate this is probably to include some kind of values instead of just talking hello well we can also log the actual I. bearable to see what impact currently I am our eyes that and I get it 5 you can save it out hello world segment early runs already directions another way when I is there it’s not going to run because of course. Control Flow Control Flow


There are months 00 that’s going to continue because it’s a feminist for otherwise when I is 1 we’re going to get logged when I threw an often when I was 3 we are and then when I went out and then of course I never gets 5 because when it hits 5 we do not do the folder because the statement said that I must be less than 5 with all the changes to something a bit more simple such as if hi it’s great to them through continued so basically what should happen is when I was here and I want an honest truth you should print out a log as well as 1 high is however when I was 3 or 4 it shouldn’t if we run that you’ll see we get hello while printing 3 times great so that’s pretty much it to continue I mean all it’s going to do is when continue gets hit it will skip to the next iteration to full it actually debunk this by putting a break point yeah this will give you some more insight so you can see here I. is 0 which of course is not going to trigger this is going to go down to log in log and everything will be fine when I was 1 and I asked her however as soon as I hit 3 and 3 is quite untrue. Control Flow Control Flow

we’re going to go to continue which means that if I could at 10 right now instead of moving down to blog because it actually jumped back up to the side of the full it then it will it will do I ++ and it will evaluate our condition and then jump back here and of course in this case it’s going to be the exact same behavior all right I think that’s continued sort of let’s take a break so if I just replace these cards with right it’s going to appear to act the same way you can see that we get 012 and we don’t get anything else however if I go back to my first example of if I’m on because there are and have 5 you can see we actually get absolutely nothing. Anything that helps a little bit of a better example the it’s actually trains the first values all just do I plus 11340 if we quickly replaces with continued just so you can see what’s happening here you can say that instead of going all the numbers now it’s going even on the so. Control Flow Control Flow Control Flow

We get the order to install printing sorry if I replace this now with break what’s actually going to happen is it’s just going to print that first 1 because as soon as it hits that break statement that said that Lucas or gamer for that look if we place a bright point over here and we had a 5000 first time of course the if then it’s not going to be true so we don’t break but in the second time around for this fuller and this is true when we had break guess what if I have time right now we jump straight out of the folder into the next life card that’s that Frank Franks how full it completely and of course these kind of control with them inside loops political lips are fuller while loops do while loops they work the same in older all right in the final on behalf is returned now this is a function that has to return an integer so we contemplate being returned we have to return something we try and do something like this and we hit control at 7 it’s going to fail because you can see it’s telling us that may function must return a value because the declaration states the house or Senate instead so. Control Flow Control Flow Control Flow

I could do something like return 00 because of the value chain and if I run this card. It’s actually going to immediately appear to close that program because of course we never get through the STV theme park that statement which is the statement to keep that window open because it’s expecting up provide input the fire again put a breakpoint here and run my program usually the best time to find a local I. well then we do actually get that hello world logging off yeah then the second time when I hit return 0 that said look at it jumps right to that and Kelly Brackett because this function is our vote now one other thing tonight with the return statement is that you don’t have to use return inside a loop the same way that you have to with continue and break return can be written anyway SO I could do something like if 5 is greater than aids return 0 and of course I have to provide a value in this case because this function returns and it’s a shock to return not necessarily inside a loop can be absolutely anywhere on your card you also don’t even have to have an if statement you could have something like this but of course in this case there is no circumstance under which this line would get trade as. Control Flow Control Flow Control Flow

it is actually known is debt card and some compilers especially in other languages will actually restrict you from writing code like this because I mean at this point you might want to delete this line is never going to get cold so that’s pretty much it as far as control flow statements are returned can be used anywhere it would basically just exit the function remember of course if your function has written a value then return has to give it a value to our time this control first admins basically formed the basis of how your floor guys the pad with loops and if statements and like all of these control plus segments that is essentially the logic of programming those are the tools you have access to your to control the flow of your program basically which line gets executed next right those tools.

If statements and conditionals like that loops and control the silence that is it that is the only thing we can modify the behavior of your program chill from you actually riding into the instruction pointer to that maybe as the series progresses and we start actually rising applications I’ll be referencing all these control plus a missile the time ensuring examples of where they use and how I can use them as well as from. Showing examples about how I can write the same card but without using them away using a different statement sorry if you want some more concrete examples of how they actually work just stay tuned to the series and in future episodes when we actually start writing stuff we’re definitely going to cover this because again this is one of the fundamental building blocks of writing application.

Also See

What are Dynamic Arrays in C++ Part 2 | Top and Latest 2021

Leave a Comment

Your email address will not be published. Required fields are marked *