Chapter 4
In this chapter, I will learn you about the if statement, it's siblings, and how it lets the user, through input, make a choice. But before we cover that I need to tell you about something I have neglected to say. It isn't rerquired but it is good habit that every time you use an open delimiter (curly bracket) you indent, and you go back one indent every time you use a close delimiter. Now back to the if statements. There are different types of the if statement. They are:
if
if else
if else if
nested if
switch
If statement
The if statement is used to say if this statement is true, then do this before continuing on, otherwise be on you way. The syntax for the if statement is pretty simple. It is as follows:
Code:
int var1 = 1;
if (var1 == 1)
statement;
or it could be done like this for multiple statements:
Code:
int var1 = 1;
if (var1 == 1)
{
statments;
} Notice that at the end of the if statement, that there is not a semi-colon. That is because if there was a semi-colon, the compiler would say ,"okay so if var1 is 1 then do everything after the if statement." But putting a semi-colon at the end says this is the end of the if statement which would cause all of the statements that you wanted to be controlled by the if statement to execute no matter what. The next control structure is the if else statement. It acts like a two way choice. The syntax for it is:
Code:
bool var1 = true;
if (var1 == true)
{
statements;
}
else
{statements;
} Now you may be saying to yourself, "What is bool?" Well if you read one of those links I provided in chapter 2 about variable types, then you would know that bool is a bolean variable type that can have only one of two values: true or false. So that is how wer do a two way choice controlled structure. But what if you want to have a multiple choice function? Well you can do one of two things. Either nest the ifs or use an if else if statement. To nest if statements is pretty easy. To do that, do this:
Code:
if (condition)
{
if (second_condition)
{
if (third_condition)
{
statements;
}
else
{
statements;
}
}
else
{
statements;
}
}
else
{
statements;
} That to me was very easy because what you are saying is if this is true go to the next if otherwise do this statement. To use the if else if statement which is much easier, then do this:
Code:
int var1;
cout << "Enter a number from 1 to 3: ";
cin >> var1;
if (var1 == 1)
{
statements;
}
else if (var1 == 2)
{
statements;
}
else if (var1 == 3)
{
statements;
}
else if (var1 < 1 || var1 > 3)
{
cout << "Not a valid number";
} Notice the last else if. I made it to where if some idiot user tries to put in the wrong number then it says "FU" and continues on its way. Now let us take a look at the switch statement. I decided to put this with the if statements because that was who it was with in the if statement's section of the book. The switch statement gives the power to choose to the computer (sorta). To do a switch command, do this:
Code:
switch (expressionn)
{
case value1:
statements;
break;
case value2:
statements;
break;
case value3:
statements;
break;
...
case valueN:
statements;
break;
default:
statements;
} Case, break, default, and switch are all reserved words. Break is used to escape the switch statment early. In fact you can use the break statement to escape any function early. Now what happens when this executes is that the computer sees the word switch and then evaluates the expression. Whatever the result of the expression is determines what happens. If the expression does not evaluate to any of the values listed, then the program executes the statements under default. The switch statement is very good for when the user must input something because the can not only try to enter in a wrong value of the same type but they can try to enter in anything and it will just do the default. Now let's review everything we have learned by me showing a program that incorporates most of what we have learned so far.
Code:
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
int main()
{
string loop_var = "yes";
while (loop_var == "yes")
{
int var1;
cout << "Enter the number that corresponds to the month of your birth: ";
cin >> var1;
switch (var1)
{
case 1:
cout << "Your month of birth is January." << endl;
break;
case 2:
cout << "Your month of birth is February." << endl;
break;
case 3:
cout << "Your month of birth is March." << endl;
break;
case 4:
cout << "Your month of birth is April." << endl;
break;
case 5:
cout << "Your month of birth is May." << endl;
break;
case 6:
cout << "Your month of birth is June." << endl;
break;
case 7:
cout << "Your month of birth is July." << endl;
break;
case 8:
cout << "Your month of birth is August." << endl;
break;
case 9:
cout << "Your month of birth is September." << endl;
break;
case 10:
cout << "Your month of birth is October." << endl;
break;
case 11:
cout << "Your month of birth is November." << endl;
break;
case 12:
cout << "Your month of birth is December." << endl;
break;
default:
cout << "That is not a valid option." << endl;
}
cout << "Would you like to calculate another birth month? Type \"yes\" to continue. ";
cin >> loop_var;
}
cin.get();
cin.get();
return EXIT_SUCCESS;
} What that basically does is ask you what is your birth month (number) and converts it to the month's name. It then asks you would you like to do it again.
I am not going to make another chapter for a while because I am at the limit of my knowledge so when I learn more from my class, I will most likely make another chapter. Until then, peace.
~Daniel