Lesson 5: If StatementsIf you are reading this tutorial, then you are probably a programmer. Else, you aren't reading this tutorial. The preceding sentences were examples of if statements. If a condition is true then a certain generalization can be made about you, such as "IF you are reading this, you are probably a programmer". If the condition is not true then a generalization can still be made "Else, you aren't reading this tutorial". I like to draw a parallel to electrical currents here. As you probably know electric currents have two states: on and off; 1 and 0; true or false. This is a good example of if statements, but it is not solely representable of if statements. What if we want if statements to have more than two states, on or off? What if we want on, halfway on, or off? Well thats what I'll be teaching today.
Now you're probably thinking, "Enough formalities! Where is the witty guy who wrote the Input/Output lesson?". Well here I am! I was struck with a sudden gift of intellectual clarity and I felt I had to write that bit on the relation between if statements and electrical currents. So anyway, by now you are already an extremely adept programmer and you've probably written at least a few of your own programs. But wait! Let me guess: you're tired of making programs with no variation, that is, programs that just keep displaying text and getting input. How do we work with this input!?, you ask. Well I'll stop rambling and be concise: this is how you perform if statements:
- Code:
-
if (condition is met)
{
perform code;
}
Now when I said that's how you do if statements, I wan't completely honest. In a language like ruby, (http://www.ruby-lang.org/en/), you could copy and paste that into your program and it would work. But as a joke I once heard attests to: "In ruby, just take the comment lines out of your pseudo-code and your program will run. In c++, take the comments out, change the supporting syntax, then write another hundred lines of code to make it work." If you don't know what pseudo-code is, its plans for your program. Like this:
Tic Tac Toe
While Player doesn't want to stop
get input.....
So basically the joke says "C++ requires a lot of syntax to do simple things". But trust me this does pay off in more ways than just making your program look cool. Also, at the beginner level you won't notice this as much, but as you progress and your google search for programming help returns a link for a way to do what you need in ruby and the code is like "ChangeNumber();" whereas in c++ its something like creating a new function and using vectors and strings that takes up hundreds of lines of code, you'll know what I mean.
So anyway, I was rambling again. So here is the programming bit:
- Code:
-
#include <iostream>
using namespace std;
int main()
{
int x;
cout << "Enter the number 10" << endl;
cin >> x; // you remember cin right?
// heres our if statement:
if (x == 10)
{
cout << "Good job!" << endl;
}
else
cout << "That was wrong!" << endl;
system("PAUSE");
return 0;
}
What that program did was tell you to enter the number ten and if you entered anything other than 10, it told you that your number was wrong. Instead of saying "If 10: do this" And "If not 10: do this". I said "If 10: do this. or else do this:". Basically I was lazy and instead of two if statements, i used one if statement and one else statement.
You'll notice I didn't put {} around my else statement. IF your code is only one line, no matter if you are using else or if, you dont have to put {}. But if your code is more than one line, you need to use {}. Now you're probably wondering: "What if we want to have more than one condition?". Good question. Observe:
- Code:
-
#include <iostream>
using namespace std;
int main()
{
int x;
cout << "Enter the number 10" << endl;
cin >> x; // you remember cin right?
// heres our if statement:
if (x == 10)
{
cout << "Good job!" << endl;
}
if (x == 5)
cout << "You entered 5, not 10!" << endl;
system("PAUSE");
return 0;
}
Another way of checking if they entered 10 is this way:
- Code:
-
if (x!=10)
that says "If x doesn't = 10".
I'll show you how to use boolean operators now: just read the comments in the code.
- Code:
-
#include <iostream>
using namespace std;
int main()
{
cout << "Enter 6 or 5" << endl;
int x;
cin >> x;
if (x == 6 || x == 5) // the || operator means "OR" so they can //enter 5 or 6
{
cout << "Good job!" << endl;
}
system("PAUSE");
return 0;
}
- Code:
-
#include <iostream>
using namespace std;
int main()
{
cout << "Enter 6" << endl;
int x;
cin >> x;
cout << "Now enter 5" << endl;
int y;
cin >> y;
if (x == 6 && y == 5) // the && operator means "AND" so they have to //enter 5 and 6
{
cout << "Good job!" << endl;
}
system("PAUSE");
return 0;
}
Input with characters (chars) are slightly different:
- Code:
-
char c;
cin >> c;
if (c == 'a')
{
cout << "You entered a" << endl;
}
characters are surrounded by ''. not "", but ' '.
Thats it for this lesson.
You Try!
1. Create a text based RPG that lets you make decisions by typing letters and or numbers. This is ALOT of fun, if you do make one, please send it to me @
[You must be registered and logged in to see this link.] I'd love to see them.
Please watch the video tutorial for this lesson at:
[You must be registered and logged in to see this link.]