Getting Started
If you missed lab or failed to copy my code from the projector during lab session, the files I typed/utilised during lab sections 1 and 2 are available here: [LINK]
Synopsis
This is a redux of Lab A, with a slight twist. You are now armed and dangerous with if-statements and some form of looping. You are now going to modify Lab A to restrict inputs. In other words, you want the user to input certain information. If they input information that doesn't meet your requirements, you will constantly ask them until they do meet your requirements. In this lab, you are restricting how much money they can spend and how much money they have in their pockets.
If Statements
If statements allow the programmer to control what happens in the program more. You use if statements in real life all the time. Let's say you were driving a car, for instance. If a traffic light is red, then you stop. Else, you go (or keep going, for that matter).

### Example: Check if a number is too large (Over 100)

Let's say with code, you want to input a value and then print out a result based on how big it is.
``````#include <iostream>

using namespace std;

int main() {
int number;
cout << "Number: ";
cin >> number;
if (number > 100) {
cout << "Number is too big!" << endl;
}
else {
cout << "Number is perfect!" << endl;
}
}``````
Outputs:
``````Number: 25
Number is perfect!``````
``````Number: 125
Number is too big!``````
So what exactly is going on here? Simple, the program will read in a number, and then it will check if that number is over 100 or not. If it is, then it will execute everything in between the {...} after the if statement. If the number is not over 100, it must be less than or equal to 100. Therefore, it would run whatever is after "else {". The brackets and your indentation are very important.
Technically, we can make the same program with checking if the number is less than or equal to 100:
``````#include <iostream>

using namespace std;

int main() {
int number;
cout << "Number: ";
cin >> number;
if (number <= 100) {
cout << "Number is perfect!" << endl;
}
else {
cout << "Number is too big!" << endl;
}
}``````
Outputs:
``````Number: 25
Number is perfect!``````
``````Number: 125
Number is too big!``````

### Example: Three or more conditions (Mutual Exclusion)

Let's extend the traffic light example. Let's say if the light was red, you stop. Okay fair enough. Otherwise... if the light was yellow, you slow down. Simple enough. If the light is green, you fly by it in stride. Now we have three conditions to consider. Let's look at it. in the form of another example. We will have a user input the colour of a traffic light and determine what happens:
``````#include <iostream>
#include <string>

using namespace std;

int main() {
string colour;
cout << "Light Colour: ";
cin >> colour;

if (colour == "GREEN") {
cout << "Go!" << endl;
}
else
if (colour == "YELLOW") {
cout << "Slow down!" << endl;
}
else
if (colour == "RED") {
cout << "Stop!" << endl;
}
else {
cout << "What kind of light is this!?" << endl;
}
}``````
If we run the program a few times, we get this:
``````Light Colour: GREEN
Go!``````
``````Light Colour: YELLOW
Slow down!``````
``````Light Colour: RED
Stop!``````
``````Light Colour: BLUE
What kind of light is this!?``````
"else" is very handy in this situation. When we check for traffic light colours, if it isn't green, it will then check to see if it is yellow. If it isn't yellow, it will then check if it is red. If it isn't red, then clearly it isn't green, yellow, or red. So it must be something else. That is where we print "What kind of light is this!?".

### Example: Nested If Statements

Let's branch off of the traffic light example once again. Let's say you wanted to run the red light... So far, our if statement (from above) is:
``````...
if (colour == "RED") {
cout << "Stop!" << endl;
}
...``````
Let's allow the user to input whether or not they want to run the light. If they input "YES", then they will go regardless of the light being red.
``````...
if (colour == "RED") {
string run;
cout << "The light is red! Run it? ";
cin >> run;

if (run == "YES") {
cout << "You ran the light, you horrible person." << endl;
} else {
cout << "You stopped!" << endl;
}
}
...``````
Do you see what is going on here? Inside of the {...} for the if statement checking if the colour is "RED", we add another input to see if the user wants to run the light. And if so, have another if statement (nested inside the first one) checking that answer.

Putting it all together now:
``````#include <iostream>
#include <string>

using namespace std;

int main() {
string colour;
cout << "Light Colour: ";
cin >> colour;

if (colour == "GREEN") {
cout << "Go!" << endl;
}
else
if (colour == "YELLOW") {
cout << "Slow down!" << endl;
}
else
if (colour == "RED") {
string run;
cout << "The light is red! Run it? ";
cin >> run;

if (run == "YES") {
cout << "You ran the light, you horrible person." << endl;
} else {
cout << "You stopped!" << endl;
}
}
else {
cout << "What kind of light is this!?" << endl;
}
}``````
Outputs:
``````Light Colour: RED
The light is red! Run it? YES
You ran the light, you horrible person.``````
``````Light Colour: RED
The light is red! Run it? NO
You stopped!``````
While Loops
Remember how you write if statements? The code inside of the ()'s is called your condition. If that condition is true, the if statement's code inside {...} runs. While Loops are one of the loops C++ allows a user to use. Its syntax is simple, it will allow you to repeat lines of code until the condition specified is false.
``````while (CONDITION) {
//Code here to be looped
}``````
Makes sense... So let's say we wanted the user to input a number, and force them to re-enter numbers until they enter one that is under 1000:
``````int number;
cin >> number;
while (number >= 1000) {
cin >> number;
}``````
In the next section, you will learn a better way to write this. So the program runs, and will ask the user to input a number. But then the while loop comes in. Is the number greater than or equal to 1000? If so, the code inside the while loop will run, asking the user to input a number once more. If the number is still over or equal to 1000, you are very stubborn. The program will go back to the beginning of the while loop and repeat until the number is less than 1000.
Do/While Loops
Let's be honest, there has got to be a better way to write the code in the "While Loops" section. As a matter of fact, there is:
``````int number;
do {
cin >> number;
}
while (number >= 1000);``````
This code does the exact same thing as listed above. It will ask for input until the user gives a number below 1000.

### The difference between While and Do/While Loops

Simple, a Do/While loop is guaranteed to run at least once. Hence why we were able to write "cin >> number" only once in that loop compared to twice in the while loop. For a while loop, if the condition is false from the beginning, the loop won't even run. The program will skip it. So it can run "0 times". If you want something to run at least once, go with a Do/While.
The Lab
I've given you more than enough information to do this lab. Have fun! :)