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
Error codes! Ah yes. You are customer support, and will be taking phone calls from people who were unfortunate enough to have an issue with a universal remote control device. The program gave them an error code. They will also tell you the duration of which they have been having an issue. So, you will have to utilise switch-case statements and compound conditional if-statements. Thankfully, I will give a handful of useful examples below.
Compound if statements
You know how to write if statements by now (if not, please go to Lab C). But what if you wanted to meet two conditions rather than just one?

### Example: Check if number is between 10 and 100.

Two conditions here... C++ lets you do this in many ways, but some ways are better than others. You can do the lazy way and put an if-statement inside of another like this:

The WRONG way
``````if (num < 100) {
if (num > 10) {
cout << "The number is between 10 and 100!" << endl;
}
}``````
Sure, this code will work, and it demonstrates you understand how if-statement nesting works... but it looks ugly and uses two if-statements whenever we can get away with just one. The better way? Observe:

The RIGHT way
``````if (num > 10 && num < 100) {
cout << "The number is between 10 and 100!" << endl;
}``````
There you have it. "&&" means "AND". "||" means "OR" and "^^" means "XOR". You can stack these as many times as you want. So you can have an if statement with 20 conditions all chained together with && and it will gladly accept it.
Compound while and do/while loops
So there are going to be cases where you want a program to repeat until you tell the program to stop (Usually with 'n' or 'N'). Assuming you read the section above about compound if-statements... it works the same with while and do/while loops.

### Example: Tell a program to repeat on 'y' or 'Y'

``````int main() {
char result;
do {
//<Do whatever your program wants to do here>

cout << "Would you like to repeat? (y/N) ";
cin >> result;
}
while (result == 'y' || result == 'Y');
}``````

### Example: Tell a program to stop on 'n' or 'N'

``````int main() {
char result;
do {
//<Do whatever your program wants to do here>

cout << "Would you like to repeat? (y/N) ";
cin >> result;
}
while (result != 'n' && result != 'N');
}``````
Switch/Case Statements
So let's say you had to choose a number between 1 and 4... and depending on the number selected, the program will print out a colour.
• 1 - Red
• 2 - Blue
• 3 - Yellow
• 4 - Green
Sure, we can write this with a bunch of if-statements:
``````if (num == 1) {
cout << "Red" << endl;
} else
if (num == 2) {
cout << "Blue" << endl;
} else
if (num == 3) {
cout << "Yellow" << endl;
} else
if (num == 4) {
cout << "Green" << endl;
}``````
Doesn't that look a little... clustered though? There is a much easier way to write this. Introducing the switch/case statement.
``````switch (num) {
case 1: cout << "Red" << endl; break;
case 2: cout << "Blue" << endl; break;
case 3: cout << "Yellow" << endl; break;
case 4: cout << "Green" << endl; break;
}``````
So the switch/case statement will read the value of "num", and go to a case based on its value. Simple enough, but it has its catches. Notice the break; there. If that isn't there and we were at case 1... it would end up going down to case 2 and executing its code! Observe:
``````switch (num) {
case 1: cout << "Red" << endl; break;
case 2: cout << "Blue" << endl; break;
case 3: cout << "Yellow" << endl; break;
case 4: cout << "Green" << endl; break;
}``````
``````Input Number: 1
Red
Blue``````
This can be good however, because what if you actually wanted 1 and 2 to do the same thing? You can write a switch/case statement in this style:
``````switch (num) {
case 1:
case 2:
//Do something
break;
}``````

### The "default" keyword

Remember how if-statements had "else"? Switch/Case statements have their own form of this as well, known as "default". If none of the cases match the number given, it will go to the default case.
``````switch (num) {
case 1: cout << "Red" << endl; break;
case 2: cout << "Blue" << endl; break;
case 3: cout << "Yellow" << endl; break;
case 4: cout << "Green" << endl; break;
default: cout << "Unknown" << endl; break;
}``````
The Lab
Once again, I have given you more than enough resources to do this lab. If you want more examples of code, feel free to check out this page, as it has examples I gave in class. Make sure you read the lab writeup, and have fun! :)