If Statement in C++ Programming

In Part 3, we start using ” If ” in C++, which compares 2 or more variables inside the parenthesis of the If statement, using the Relational Operators in C++. What you will learn, will be tested as you program a real App, a Simple Calculator.

Relational Operators:

== Equals

!= Does not Equal

> Greater Than

< Less Than

<= Less Than or Equals

>= Greater Than or equals

 

Contents:

File Downloads

The “if” statement

Let’s take a closer look at the code

Programming Project – Simple Calculator

A closer look at the Addition block of code

Final Notes

 

File Downloads:

IFstatement1

IFstatement2

SimpleCalc

The if statement

if” is a conditional statement.

If the two variables inside the parenthesis of the “if” statement meet the condition, then the code following the “if” statement will execute.

Here are the basic Conditional Symbols:

== Equals

!= Does not Equal

> Greater Than

< Less Than

<= Less Than or Equals

>= Greater Than or equals

The “if” statement can do just one line of code.


if(var > num) cout << "var is greater than num";

or


if(var > num)

cout << "var is greater than num";

 

The “if” statement can have one or more lines of code, in between its curly braces {….}


if(var > num) { //if var is greater than num then do the cout below

	cout << "var is greater than num";

}

Below you can see an example of “if” conditional statements:


int first_num = 3;

int second_num = 7;

if(first_num == second_num)

	cout << "Print this"; //condition not met, cout won't print

if(first_num < second_num)

	cout << "Print this"; //condition met so cout will print

 

Start a new Console Project. Refer back to Part 1 to see how to start a new project.

Create a new .cpp file in your Source Files folder. Type in the two headers and the Main function.

Or copy and paste the code below.


#include<iostream>

usingnamespace std;

int main() {

	int anykey;

	int input1, input2;

	cout << "Input a random number between 1 and 100 " << "\n";

	cin >> input1;

	cout << "Input a second random number between 1 and 100 " << "\n";

	cin >> input2;

	cout << "\n" << "The following have met the the programs conditional statements:" << "\n\n";

	if(input1 == input2) cout << "The first number is EQUAL to the second number\n";

	if(input1 != input2) cout << "The first number is NOT EQUAL to the second number\n";

	if(input1 > input2) cout << "The first number is GREATER THAN the second number\n";

	if(input1 < input2) cout << "The first number is LESS THAN the second number\n";

	if(input1 <= input2) cout << "The first number is LESS THAN OR EQUAL TO the second number\n";

	if(input1 >= input2) cout << "The first number is GREATER THAN OR EQUAL TO the second number\n";

	cin >> anykey; //pause until: press any key and Enter to end program

	return 0;

}

 

Start Debugging from the menu.

 

Input7 as the first number.

 

Input 99 as the second number.

 

Three conditions have been met:

(7 does not equal 99), (7 is less than 99), and (7 is less than or equal to 99).

 

Of the six conditional statements, three below will meet the condition and print to the screen.

if (input1 == input2) cout << “The first number is EQUAL to the second number\n” ;

if(input1 != input2) cout << “The first number is NOT EQUAL to the second number\n”;

if (input1 > input2) cout << “The first number is GREATER THAN the second number\n” ;

if(input1 < input2) cout << “The first number is LESS THAN the second number\n”;

if(input1 <= input2) cout << “The first number is LESS THAN OR EQUAL TO the second number\n”;

if (input1 >= input2) cout << “The first number is GREATER THAN OR EQUAL TO the second number\n” ;

 

Remember the Conditional Symbols:

== Equals

!= Does not Equal

> Greater Than

< Less Than

<= Less Than or Equals

>= Greater Than or equals

 

Let’s take a closer look at the code:


int anykey;

int input1, input2;

The variable “anykey” is used with the last cin statment to cause the program to pause, which keeps the console window open.

Variables “input1“, and “input2” are declared to be Integers with the “int” keyword.

The first “cin” statement waits for you to enter a number. Type 7 and Enter.

The second “cin” statement waits for you to enter a number. Type 99 and Enter.


cout << "Input a random number between 1 and 100 " << "\n";

cin >> input1;

cout << "Input a second random number between 1 and 100 " << "\n";

cin >> input2;

 

Now lets look at the “if” statements.

Remember that the first variable is being compared to the second variable. If True, then the “cout” statement will execute.

In this case the first number was 7 and the second was 99.


if(input1 == input2) cout << "The first number is EQUAL to the second number\n";

if(input1 != input2) cout << "The first number is NOT EQUAL to the second number\n";

if(input1 > input2) cout << "The first number is GREATER THAN the second number\n";

if(input1 < input2) cout << "The first number is LESS THAN the second number\n";

if(input1 <= input2) cout << "The first number is LESS THAN OR EQUAL TO the second number\n";

if(input1 >= input2) cout << "The first number is GREATER THAN OR EQUAL TO the second number\n";

Three “if” statements above are True, so three “cout” statements print:

 

Programming Project – Simple Calculator

Our first programming project will be 60 lines long. It will be a Simple Calculator.

Run the program and you can Add, Multiply, or Subtract, two numbers at a time.

We will leave out Division because we are not using floating point math (numbers with decimals). The “int” keyword for variables, only handles whole numbers.

Everything you learned so far, will be applied in this program.

Start a new Console Project. Refer back to Part 1 to see how to start a new project.

Create a new .cpp file in your Source Files folder. Type in the two headers and the Main function.

Or copy the code below, into the .cpp file.


#include<iostream>

usingnamespace std;

int main() {

    //Simple Calculator

    int menu_num;

    int first_num, second_num, total;

    int input_pause;

    
    //Menu

    cout << "1. + Add\n";

    cout << "2. * Mutiply\n";

    cout << "3. - Subtract\n";

    cout << "\nEnter a number from 1 to 3: ";

    
    //enter 1 - 3...if you do not enter an number, program closes

    cin >> menu_num;

    //Addition

    if(menu_num == 1) {

        cout << "\nEnter the first number to Add: ";

        cin >> first_num;

        cout << "\nEnter the second number to Add: ";

        cin >> second_num;

        cout << "\nTotal: " << first_num + second_num; 

    }

    //Multiplication

    if(menu_num == 2) {

        cout << "\nEnter the first number to Mutiply: ";

        cin >> first_num;

        cout << "\nEnter the second number to Mutiply: ";

        cin >> second_num;

        cout << "\nTotal: " << first_num * second_num; 

    }

    //Subtraction

    if(menu_num == 3) {

        cout << "\nEnter the first number to Subtract: ";

        cin >> first_num;

        cout << "\nEnter the second number to Subtract: ";

        cin >> second_num;

        cout << "\nTotal: " << first_num - second_num; 

    }

    cout << "\n\npause...press a key then Enter... or close console window";

    cin >> input_pause;

    return 0; 

}

Run the program and only enter numbers. You can Add, Subtract, or Multiply.

Let’s take a closer look at the Menu code:


//Menu

cout << "1. + Add\n";

cout << "2. * Mutiply\n";

cout << "3. - Subtract\n";

cout << "\nEnter a number from 1 to 3: ";

 

Enter a new variable called “menu_num.” This variable will hold the users Menu number.


int main() {

//Simple Calculator

int menu_num;

int first_num, second_num, total;

int input_pause;

//Menu

cout << "1. + Add\n";

cout << "2. * Mutiply\n";

cout << "3. - Subtract\n";

 

Enter three more variables to the program, “first_num” and “second_num” and “total”

first_num” will hold the first number in the calculation.

second_num” will hold the second number in the calculation.

total” will hold the final calculated value of the “first_num” and “second_num” variables.


int main() {

//Simple Calculator

int menu_num;

int first_num, second_num, total;

int input_pause;

//Menu

cout << "1. + Add\n";

cout << "2. * Mutiply\n";

cout << "3. - Subtract\n";

Add the Addition block of code. The “if” statement will compare the variable “menu_num” to the number one. Number 1 on the menu is Addition.

So if the user enters 1, then the “if” statement is True, and the code inside the curly braces will be executed.


cout << "\nEnter a number from 1 to 3: ";

//enter 1 - 3...if you do not enter an number, program closes

cin >> menu_num;

//Addition

if(menu_num == 1) {

	cout << "\nEnter the first number to Add: ";

	cin >> first_num;

	cout << "\nEnter the second number to Add: ";

	cin >> second_num;

	cout << "\nTotal: " << first_num + second_num;

}

//Multiplication

if(menu_num == 2) {

cout << "\nEnter the first number to Mutiply: ";

Run the program. Enter 1 from the text menu for Addition. When the program asks for the first number, type 7. Then for the second number, type 3. The total comes to 10, so we know the Addition code is working.

 

A closer look at the Addition block of code:

When the program reaches the Addition block of code, and you typed 1 from the menu, then the “if” statement is True. Then the block of code between the curly braces is executed.


//Addition

if(menu_num == 1) {

	cout << "\nEnter the first number to Add: ";

	cin >> first_num;

	cout << "\nEnter the second number to Add: ";

	cin >> second_num;

	cout << "\nTotal: " << first_num + second_num;

}

The first “cin” statement asks for a number. You typed in 7, so the value of 7 is put into the variable “first_num“.

The second “cin” statement asks for a number. You typed in 3, so the value of 3 is put into the variable “second_num“.

The final “cout” statement prints the total: (first_num + second_num = 10)

Add the Multiplication and Subtraction blocks of code after the Addition block.


//Addition

if(menu_num == 1) {

    cout << "\nEnter the first number to Add: ";

    cin >> first_num;

    cout << "\nEnter the second number to Add: ";

    cin >> second_num;

    cout << "\nTotal: " << first_num + second_num; 

}

//Multiplication

if(menu_num == 2) {

    cout << "\nEnter the first number to Mutiply: ";

    cin >> first_num;

    cout << "\nEnter the second number to Mutiply: ";

    cin >> second_num;

    cout << "\nTotal: " << first_num * second_num; 

}

//Subtraction

if(menu_num == 3) {

    cout << "\nEnter the first number to Subtract: ";

    cin >> first_num;

    cout << "\nEnter the second number to Subtract: ";

    cin >> second_num;

    cout << "\nTotal: " << first_num - second_num; 

}

cout << "\n\npause...press a key then Enter... or close console window";

cin >> input_pause;

return 0;

Run the program again. Type 2 from the menu to Multiply. Use 7 as the first number and 3 as the second number. Total = 21 (7×3=21), so the Multiplication block of code is working.

 

Run the program again. Type 3 from the menu to Subtract. Again use 7 as the first number and 3 as the second number. Total = 4 (7-3=4), so the Subtraction block of code is working.

 

You might also want to read:

Part 1 – Installing Visual C++ Express

Part 2 – The Basics of C++

Part 3 – Conditional “if” Statement

Part 4 – else if Statement in C++

Part 5 – “switch and loops”

Part 6 – Arrays & Strings

Part 7 – Pointers

Part 8 – Functions in C++

 

More C / C++ and Dev Programming Tutorials:

 

If you like this page please share with your friends:

Leave a Reply

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