1.4 Flow of Control - NO-skcaj/FRC-Cpp-Crash-Course GitHub Wiki
As we've seen before, code is executed linearly, one line or statement at a time and flows down. Now, code will always start at main, and then always flow down, but we can change that by using a control structure like a function. A control statement, like a function call, will take us out of our main code, and runs code within the structure until it ends and we return back to the original main. To illustrate this, here's an example:
#include <iostream>
using namespace std;
int func1(int arg) // control structure
{
arg = arg + 4; // 3rd statement
arg = arg * 2; // 4th statement
return arg; // 5th statement, return takes us back to the main function
}
float func2(float arg) // control structure
{
arg = arg / 2; // 8th statement
return arg * 5; // 9th statement
}
int main() // This is called first so we start here
{
int x = 0; // 1st statement
x = func1(x); // 2nd statement, a control statement which pushes over to the function
// were here after func1
int y = 4; // 6th statement
y = func2(y); // 7th statement, a control statement which pushes over to func2
// here after func2
int z = x + y; // 10th statement
cout << z << endl; // 11th statement, also a control statement, but we didn't define this one
// Aaaaaaaand the program ends!
}As you moved down the main function, you encountered control structures which took us into the function and popped us back out onto main.
Away from functions, other control statements like if, for, while, etc.
A control structure that only runs its code if a given condition is true.
if can be used in conjunction with else to cover more
void check(bool z)
{
if(z) // if z is true, then we go into the code right next to it and prints "Yes"
{
cout << "Yes" << endl;
}
else // if z isn't true, then we go onto the next control statement, else is a catch-all so it prints "No"
{
cout << "No" << endl;
}
}So, when we put a true into the function, it goes into the if statement, prints "Yes", and then pops us back out onto main (we skip the else statement because we passed a true into a previous connected if statement).
Alternatively, we can use an expression as the condition in an if statement.
if (4 > 3)
{
// Your code here
}We can also "chain" if-else statements:
if (a > b)
{
// if (a > b)
// notice how it doesn't matter if future conditions are true
// if the first condition is true, the rest of the statement is skipped
}
else if (c == 3)
{
// if !(a > b) && (c == 3)
}
else
{
// if !(a > b) && !(c == 3)
// default catch-all if all else is false
}The while statement allows us to repeat a specific piece of code over and over again while a condition is true.
int x = 0;
while (true)
{
std::cout << x << std::endl;
x++;
}Until the end of time (whenever we stop the code) this will continue to print "x", because the condition will always be true. Normally, we would use a variable in the while statement so that we can repeat code over an over again until it becomes true.
So if we had a randomNum function that picked a number 1-10 then we could use a while statement to continue picking numbers until we get, lets say, a 5:
int x = 0;
while (x != 5) // at the end of every cycle it will check if x != 5, if x isn't 5 it will loop again
{
x = randomNum();
}