C++ Tutorial for beginners – If Statements

Basics of C++ | Variables | If statements | Loops | Functions

 

 

Before I start I want to explain TRUE and FALSE. If something is TRUE then it does not equal zero, if something is FALSE then it equals 0.

 

>          Greater than

<          Less than

>=        More than or equal to

<=        Less than or equal to

!=         Does not equal to

==        Is same as

=          Equals (used to set variables eg. variable1 = “Hello”;)

 

The structure of an If statement is as follows:

 

if (TRUE)

// What to do if true

 

if (FALSE)

// What to do if false

 

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

#include <iostream.h>

int main()

 

{

            int numofplayers;

            cout << "How many players are there? ";

            cin >> numofplayers;

 

            if (numofplayers == 20)

            {

                        cout << "You entered 20" << endl;

                        return 0;

            }

 

            if (numofplayers < 20)

                        cout << "You entered a number less than 20" << endl;

            if (numofplayers >= 20)

                        cout << "You entered a number more than or equal to 20" << endl;

 

            return 0;

}

 

 

 

9

if (numofplayers == 20)      

 

This is saying if the user enters the number 20 it will execute the line under it, in this case the line under it is { which means there is more than one thing to execute.

 

10

13

{

 

}                                        

 

 

 

The reason I have put { & } in is because I have a return; in there, and the reason I have return in there is so if you type in 20 it will execute the cout statement and then finish instead of executing the cout statement and then going to the next if statement (if (numofplayers >= 20)). I could have made that line

if (numofplayers > 20) but for this example I haven’t

 

 

15

if (numofplayers < 20)

 

If the number you entered was less than 20 it will execute the cout statement after it

 

17

if (numofplayers >= 20)

 

If the number is 20 or higher then it will execute the cout statement after it

 

 

 

Here is another If Statement example:

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

#include <iostream.h>

 

int main()

{

            int numofplayers;

            cout << "How many players are there? ";

            cin >> numofplayers;

 

            if (numofplayers == 20 || numofplayers == 61)

            {

                        cout << "You entered the number 20 or 61" << endl;

                        return 0;

            }

            if (numofplayers >= 30 && numofplayers <= 40)

            {

                        cout << "You entered a number between 30 and 40" << endl;

                        return 0;

            }

            if (numofplayers >= 20)

                        cout << "You entered a number more than or equal to 20" << endl;

 

            else {

                        cout << "You didnt enter a number larger than 20." << endl;

                        cout << "You didnt enter 20 nor 61" << endl;

                        cout << "So you must of entered a number less than 20" << endl;

            }

 

            return 0;

}

 

 

 

9

if (numofplayers == 20 || numofplayers == 61) 

 

This is saying if the number you entered is either 20 or 61 then execute the next line

 

14

if (numofplayers >= 30 && numofplayers <= 40)

 

What this does is if numofplayers (the number you typed in) is equal to or greater than 30 it will go to the next part which is numofplayers <= 40 and what that is saying is if numofplayers is equal to or less than 40 then it will continue. So if numofplayers is greater than or equal to 30 and is also less than or greater than 40 then execute the line under it

 

22

else {                                 

 

If the number you entered fits none of the if statements then it will execute the next line