CCC 3: Control Flow - TEAM1771/Crash-Course GitHub Wiki
Control structures and statements
If you've ever read some code, you've probably seen control statements like if
, else
, for
, or while
.
- “Control flow structures” (formed from control statements) make up the basis of logic in code.
- They are called control structures because they affect the “control flow,” or the order in which the code “flows” or executes.
Normally, the control flow begins at the top of the main function and "flows" down.
int a = 0;
void increment()
{
a++; // 5th
}
int main()
{
std::cout << a; // This runs first and prints 0
a += 3; // This runs 2nd
std::cout << a; // 3rd (this prints 3)
increment() // 4th, which jumps up to the increment function
std::cout << a; // And finally this runs 6th (and prints 4)
}
You can think of control flow as a laser pointer which follows the lines of codes and runs each one in order.
As stated before, control structures allow us to change control flow.
IF
The most recognizable control structure starts with the control statement if
.
The idea is simple, if an inputted boolean is
true
(or 1), control flow moves inside of the control structure. Otherwise, control flow passes over it.
void check(bool z)
{
if(z)
{
std::cout << "Yes";
}
}
int main()
{
bool a = true; // Same thing as a = 1
bool b = 0; // Same thing as b = false
check(a); // This will result in "Yes" being printed to the console
check(b); // Nothing will happen here
}
ELSE
The if
control statement can be paired with else
.
else
defines an alternative control structure to theif
structure.
void check(bool z)
{
if(z)
std::cout << "Yes"; // The bracets are not required if only one line follows the control statement
else
std::cout << "No";
}
int main()
{
bool a = true; // Same thing as a = 1
bool b = 0; // Same thing as b = false
check(a); // This will result in "Yes" being printed to the console
check(b); // This will result in "No"
}
if
and else
can be combined to have powerful logic in your code.
std::string typeOfNumber(int num)
{
if(num == 0) // This could also be replaced with if(!num) since 0 is the same thing as false
return "Neither";
else if(num % 2 == 0) // If dividing by 2 leaves no remainder. This is the same as if(!(num % 2))
return "Even";
else
return "Odd";
}
int main()
{
std::cout << typeOfNumber(3); // "Odd"
std::cout << typeOfNumber(0); // "Neither"
std::cout << typeOfNumber(-2); // "Even"
}
In this case, the else
statements aren't even necessary, since once the program runs return
, it exits the function.
The following example is equivalent to above.
std::string typeOfNumber(int num)
{
if(!num)
return "Neither";
if(num % 2) // If dividing by 2 results in any remainder other than 0, it is interpreted as true
return "Odd";
return "Even";
}
int main()
{
std::cout << typeOfNumber(3); // "Odd"
std::cout << typeOfNumber(0); // "Neither"
std::cout << typeOfNumber(-2); // "Even"
}
Extra Resources
If you're still confused, or just want to see more examples, check out these resources: The Cherno video, W3Schools
FOR
Another common control statement is “for”.
- “for” is great for looping code a set number of times.
- for loops work by making an “iterator” (a variable that “iterates”), iterating that variable (adding 1 to it) each time the loop runs, and then checking if a condition is met. All of this happens inside the () after the word “for”.
- As long as the condition is met, the code inside the {} of a "for” loop is run.
for(int i = 0; i < 3; i++)
{
std::cout << i;
}
The example above would output 0, 1, and then 2.
The for statement contains three statements within the parenthesis
- The first statement contains code to only be run once, In this case creating the variable “i” as the iterator
- The second statement contains the condition to be met to continue, When it doesn't pass the condition (i becomes 3), the code stops looping, and control flow resumes after the for loop.
- The third statement contains code to be run after each loop, this example iterates “i” by adding 1 to it, but then checks to see if “i” is still < 3.
WHILE
“while” loops are similar to “for” loops because they loop code “as long as” (aka “while”) a condition is met.
The only difference between while and for loops is that while loops do not include an iterator
However, you can implement one yourself if you like:
int i = 0;
while(i < 3)
{
std::cout << i++;
}
This example code would have the same result as the for loop example above.
- At first, while loops seem like a worse version of for loops.
- However, while loops are useful in other cases where for loops don't make sense.
For example, if you want to run code for as long as button is pressed down, you could use
while(BUTTON_PRESSED){}
. You could also run a while loop for a certain number of seconds by checking if the time has exceeded the limit:while(current_time < 30_s)
.
Extra Resources
If you want to see a couple more examples, check out: The Cherno video, or W3Schools (while and for).