Welcome to Day 6. Please DO NOT CHEAT AND USE COPY AND PASTE. ALWAYS TYPE.
On
Day 5 I showed you how to create a console application that accept input from a keyboard. Today I am going to teach you about conditional statements.
In real life, you wear clothes based on weather conditions. If it's cold, you wear warm clothes, if it's hot, you were clothes that won't make you feel too hot. If you get offered food, and you aren't hungry, you may decline the food because of your present condition.
It works the same way in programming. Sometimes you will design a piece of code that will only run under a certain condition. This concept is called conditional statements.
In C++, conditional statement can be done using the following options:
- if statements.
- switch
Let's say you wanted to design a system that can only be used by people over the age of 18. You will prompt the person to enter his/her age, then the system will tell the user if user is allowed to use it or not.
This is our program, please type it on Dev-C++ and test it for yourself:
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
int age;
cout<<"Please enter your age: ";
cin>>age;
if(age >= 18)
{
cout<<"You are over 18 and allowed to use system!";
}
else{
cout<<"You are under 18 and not allowed to use this system";
}
system("PAUSE");
return EXIT_SUCCESS;
}
That's how you use an if statement. and this equality/inequality operators are used:
- > for greater than
- < for less than
- >= for greater than or equals to
- <= for less than or equals to
- == for equals to.
Let's say the scope of the system is changed and now we are supposed to allow only people who are 18 and older, but not over 35 to use it.
We can 1) include another if statement inside another if current statement to test our second condition. Including an if statement inside another one is called nesting, it makes code smaller, but it is the most common way of introducing bugs to the system, even experienced programmers make mistakes from time to time, or 2) we can also test for those 2 conditions in one if statement.
Let's start with a solution that uses a nested if statement.
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
int age;
cout<<"Only people between 18 and 35 allowed!"<<endl;
cout<<"Please enter your age: ";
cin>>age;
if(age >= 18)
{
if(age <= 35)
{
cout<<"You are between 18 and 35 and allowed!";
}
else{
cout<<"Only people between 18 and 35 allowed!";
}
}
else{
cout<<"Only people between 18 and 35 allowed!"<<endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}
This will test for the first condition, if it's met, it will go inside the if block and test 2nd condition, if met, it displays message that user is allowed to use system. If it is not meeting the condition, then it will display message that shows that user isn't allowed to use the system.
The second option is to test those 2 conditions at one go. I have a simple rule that if your program is testing more than 3 rules, I use nested if statement, otherwise I test all conditions all at once.
When testing two or more conditions, you use this two operators, first one is:
- || double pipes, which is a symbol for OR
- && double ampersand, which is a symbol for AND
The ||(OR) statement only return true if 1 of the condition is met, and if it is, it goes inside the if block. Let's modify our system to test all conditions at one go. First we check if all conditions must be met, which is the case for our program. So, we use and &&, if only one condition was supposed to be met, then we were going to use (||)OR.
Our system now looks like this:
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
int age;
cout<<"Please enter your age: "<<endl;
cin>>age;
if(age >= 18 && age <= 35)
{
cout<<"You are between 18 and 35 and allowed!"<<endl;
}
else{
cout<<"Only people between 18 and 35 allowed!"<<endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}
The structure of the program changed, but it is still functioning the same way.
Another conditional statement you can use is a switch. I prefer using a switch when I have multiple conditions. Let's say we were designing a survey system that ask users for their favourite subjects, and we have only 4 subjects to choose from: Accounting. Physical Science, Mathematics and/or Biology. In this case, I think the best choice will be using a switch. Let's create this program to show you how it's going to like. Using ifs, and how it will look like using a switch.
Using an if statements it will look like this:
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
int choice;
cout<<"Please enter your favourite subject: "<<endl;
cout<<"1 - Accounting"<<endl;
cout<<"2 - Physical Science"<<endl;
cout<<"3 - Mathematics"<<endl;
cout<<"4 - Biology"<<endl;
cin>>choice;
if(choice == 1)
{
cout<<"You chose accounting!"<<endl;
}
else if(choice == 2)
{
cout<<"You chose Physical Sciences!"<<endl;
}
else if(choice == 3)
{
cout<<"You chose Mathematics!"<<endl;
}
else if(choice == 4)
{
cout<<"You chose Biology!"<<endl;
}
else{
cout<<"Invalid choice!"<<endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}
This can be modified to use a switch statement, and it will look like this:
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
int choice;
cout<<"Please enter your favourite subject: "<<endl;
cout<<"1 - Accounting"<<endl;
cout<<"2 - Physical Science"<<endl;
cout<<"3 - Mathematics"<<endl;
cout<<"4 - Biology"<<endl;
cin>>choice;
switch(choice)
{
case 1: cout<<"You chose accounting!";
break;
case 2: cout<<"You chose Physical Sciences!";
break;
case 3: cout<<"You chose Mathematics!";
break;
case 4: cout<<"You chose Biology!";
break;
default: cout<<"Invalid choice";
}
system("PAUSE");
return EXIT_SUCCESS;
}
I don't know about you, but I think our program looks cleaner when using a switch than using if statement. Again, see it as another tool on your toolbox, only use it when it makes it easier to reach your goal.
The switch block start with switch and input of that switch. Then cases for that switch, and the default choice which isn't covered in your cases. The break statement is used to stop executing the switch block and get out of the block when one of the cases is met.
As a rule of thumb, always test user input. Never trust it, and always supply a case when the choice is invalid. On our switch, we used the default case to catch all bad values.
This is the end of Day 6. Let's meet again on
Day 7 when I show you how to create programs that uses loops.