Hello, and welcome. We're starting a new section,
and we're going to start talking about control.Flow
control flow basically deals with
conditional statements, statements, if statements and making decisions in your program
as well as repeating code, which is loops.
So this is an important section and these are fundamental programming
techniques used in most languages. We're going to talk about how they're specific to c.
But to give you an overview,
before we start talking about syntax and diverging into examples.
I want to tell you what control flow means in terms of a program.
So the statements inside your source files are generally executed from top to bottom
in the order they appear, that's called sequential.
You have all these statements. We've learned about expressions inside the statements
or outside, but you go from the beginning to the end.
That's what we've learned so far.
We've never jumped around. We've never done anything where we repeated stuff automatically,
beginning to end.
Control flow statements, however, will break up this flow of execution
by adding things like decision making, asking questions essentially,
repeating code, looping and branching.
This enables your program to conditionally execute
particular blocks of code.
So it's not going to just be start at the top of the main function go to the bottom.
You now may be going back and forth.
You may be branching out or you may be
jumping in and out of blocks of code. So the code in the
program is going to become a little more complicated.
So things that we're talking about in terms of control flow
decision making statements. This is where you ask a question,
essentially and then you receive an answer, true or false answer.
Things that are supported in c are the if
decision statement and then you have if else
and then you also have a switch statement and a go to statement.
These are all decision making statements. Go to, I put in here
because it exists in c.
You're probably not going to want to use it. I'll briefly talk about it.
It's really just something that the programmer will hard code to a label.
We also have looping statements that are supported in c language.
The 4 keyword, the while keyword and the do while keyword.
Those are the three types of loops. Loops will repeat code.
And then branching statements are similar. They're usually
used inside of a loop or inside of a decision.
These are things like the break continuing return.
So we're going to try to focus in this section specific lectures for decision
versus looping and then also talk about breaking continue a bit.
Again, a very important lecture.
So decision making. What does this really mean inside of a program?
Again, up till now, we've basically created variables, declared them,
perform calculations.
But we never really made any specific logic decisions.
So a decision making concept is
it will require that the programmer specify one or more conditions to be evaluated
or tested by the program.
What is essentially happening is you're asking a question inside the program, that's the logic.
And if the condition or the answer to that question is true,
then you're going to execute some code,
some statements. If the answer to that question is false,
then you may execute other statements or you may not do anything at all.
So here's our little data flow diagram that shows you
the concept of decision making in a program. You have this condition.
Depending on the answer to that condition, whether it's true or false,
you're going to either execute code or not execute code.
It depends. It gets a little more complicated, but this is the basic structure
of how you can make decisions in your program.
And so what this means is inside your source code, you may be skipping some code
depending on the answer to a question,
depending on whether or not it's true or false.
So the statements that are supported by c are the if statement.
The if statement has a special keyword called.
If we're going to talk about this in detail and give examples,
but this statement is going to ask the question essentially.
Inside the parentheses is going to be a Boolean expression.
We know what Boolean expressions are.
Basically
they can return a true or false value,
a statement that returns a true or false value except in its expression,
and it can have one or more statements inside the body the if statement.
You also have an if else.
So this is an if statement that can be followed by an optional else.
If it's true, do this. If it's false, do something else.
And then we have nested if statements.
This is when you have an if statement inside another if statement.
So you may be asking multiple questions.
And again, this is going to be
detailed with specific examples and syntax. I'm just trying to give an overview at this point.
As far as repeating code goes which is again another control flow mechanism
because you're no longer again going sequentially.
With repeating code, you're going to be executing the same code a number of times.
So there may be a situation when you need to execute a block of code several number of times,
the statements are executed sequentially,
the first statement in a function is executed first followed by second and so on.
It all depends. This is kind of the block of the code of the loop body.
So a loop statement allows us to execute a statement or group of statements multiple times
without having to actually have each statement
repeated that many times.
Loop control statements change execution from its normal sequence.
When execution leaves the scope,
all the automatic objects that were created in that scope are destroyed.
So
this is talking about scope with relation to variables and
basically saying that when you're in the body of a loop
and you have local variables, you're not going to be able to access them outside of the loop.
A loop becomes an infinite loop if a condition is never becomes false.
And that means your program will hang
because you'll be executing code over and over and over, and you'll never actually exit.
Sometimes you will need to do that actually.
When you're monitoring something or you're listening for something.
And the way to actually implement that and see is you would use a for loop most likely,
but you could use a while loop as well.
We'll talk about infinite loops and why you may need them and what that means
in a later lecture.
So the actual data flow of a loop looks something like this.
You check a condition. And if the condition is true,
you execute some code and then you keep doing it over and over and over and over again
until the condition evaluates to false.
Once the condition evaluates to false, you exit the loop.
The loops that are supported in c are the while loop, the for loop and the do while loop.
Special keywords.
The while loop is repeats a statement or a group of statements while a given
condition is true. So the condition is right at the top in the wild parentheses.
Condition is test at the top before it executes loop body.
Even though there's three different types of loops that are supported, you may use one more
than the other. The while the for loops are pretty much the same,
they just have different syntax. And you can do more things differently.
The do while loop is different because you actually
have the test at the bottom. So you're always guaranteed to execute the loop
at least once, the body of the loop.
But again, we'll talk about this stuff. For loop, special keyword, executes
sequence statements multiple times. It just has basically
easier syntax. You can initialize the loop control variable
and the conditions all in one statement.
And then the do a while loop, as I said, is a little different because
it's a post-test loop, an exit control loop,
which means that you're always guaranteed to enter the body of the loop at least once.
And we'll also talk about nested loops. This is again just a loop inside of a loop,
just like a nested for if statement is an if statement inside of an if statement,
same applies to loops, and we'll see how this
sometimes required and can complicate things,
but it's essential for programming.
A nice quick overview of the different control flow
mechanisms, high level and then some of the keywords that are
supported by c to actually implement this.
Now let's move on and give some examples and start talking about decision making.
Thank you.