C++ Tutorial #4 – If Statements


IF you want to learn if statements, then you are on the right page, ELSE try looking at my other posts. In this tutorial I will be showing you how to use if statements.


They are very easy to use, but before I begin, I believe I should tell you more about operators. Remember last tutorial when we made a calculator and we used operators to add, subtract, multiply etc.? These operators are called ARITHMETIC OPERATORS. The operators we will be using today are called RELATIONAL & COMPARISON OPERATORS. With it, your programs you write can compare two expressions, and be able to determine whether two values are equal, or if one value is greater than the other.


Look at the picture below. Remember in school, when you you had to use these symbols? Do they look familiar to you? Remember when you asked your teacher “Why are we doing this? When will we ever use something like this in the future?”. Then your teacher didn’t tell you why, or she didn’t give you a very good answer. Today, I’ll tell you that you will need to use these symbols in C++ programming. So you can make a video game!




This is how relational & comparison operators look like in c++:


==    Equal to
!=    Not equal to
<    Less than
>    Greater than
<=    Less than or equal to
>=    Greater than or equal to



We will use these in a second.
If statements are written like this.



The code inside the parenthesis are called conditions. When the conditions are true then it will run the statement, which is the code inside the curly braces. The program compares two numbers x and 10. If x is equal to 10, then it will display the message. If x doesn’t equal 10, then program won’t use the code inside the if statement.


You can use “else if” and “else” if you want to work with more statements. If the first condition is false then it will try the next condition, and the next, and the next until it reaches a condition that’s true and the program will run the statement or block.
For example:



Let’s write a program using if statements. You can either type it or copy and paste this code. Though I would recommend you type it. It will be a lot easier to learn.



As you can see we created integer ‘x’, a didn’t assign it a value. We had the program tell the user to “Enter a number”. The user enters a number to assign a value to integer ‘x’. With the if statements we told the program to check if ‘x’ was equal to 7. If the condition was true then we had the program print “‘x’ is my favorite number!”. If the condition was false, the program would check the next condition, “else if” to see if ‘x’ was less than 7. Again, if the condition was true it would print the message. If it is false it would try the next condition “ELSE”, if all conditions were false then it would run the statement or block inside the curly braces, and the program would print “‘x’ is greater than my favorite number!”.


You could use else if and used the greater than symbol (>), but I just wanted to show you how “else” worked. You usually want to place “else” in your code when you don’t want your program to try anymore conditions or when all your conditions are false.


I will end this tutorial here. I hope you’ve enjoyed this one. I will post new tutorials when I can. In the meantime, practice your coding, and mess around with some of the things you learned. Write some programs of your own. It’s fun to do, and it will help you understand how video games work.


About Justin

Computer programmer and owner of kauergames.com.
Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.