SETTINGS
Appearance
Language
About

Settings

Select a category to the left.

Appearance

Theme

Light or dark? Choose how the site looks to you by clicking an image below.

Light Dark

Language

Preferred Language

All content on utk.claranguyen.me is originally in UK English. However, if content exists in your preferred language, it will display as that instead. Feel free to choose that below. This will require a page refresh to take effect.

About

"utk.claranguyen.me" details

Domain Name: claranguyen.me
Site Version: 3.0.1
Last Updated: 2019/08/18
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.
Code (C++)
#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:
Output
Number: 25
Number is perfect!
Output
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:
Code (C++)
#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:
Output
Number: 25
Number is perfect!
Output
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:
Code (C++)
#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:
Output
Light Colour: GREEN
Go!
Output
Light Colour: YELLOW
Slow down!
Output
Light Colour: RED
Stop!
Output
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:
Code (C++)
...
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.
Code (C++)
...
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:
Code (C++)
#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:
Output
Light Colour: RED
The light is red! Run it? YES
You ran the light, you horrible person.
Output
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.
Code (C++)
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:
Code (C++)
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:
Code (C++)
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! :)