C++ part 3 – If, If else, else statements

(UPDATED 26.6.2013)

I am sorry for the previous master piece of chaos namely Boolean operations. From now on I will try to be as clear as possible. In this post we will go through some basic if, if else statements.

Now you might think what is if statement? If statement is part of almost any programming language and important part that is to create algorithms to achieve something. If statements basically are statements to check if some value we input or output will be the value we want (or do not want) or if we are not sure about the value coming in our program.

For example in the below I have tried to demonstrate in pseudo code

// Example 1
if( 2 > 1 )
// Conditional expression
{
 cout << ” The value 2 is bigger than 1″ << endl;
}

Another example

// Example 1
if( 2 == 2 )
// Conditional expression
{
 cout << ” The value 2 equals 2 ” << endl;
}

As a side note, the == is not the same as = and here is why.
The == (equal) operator is a Boolean meaning that if you are creating the program logic and you want to compare the values, you will use this operator. On the other hand if you want to assign a value to another variable you use the = (assignment) operator.

If statements can basically be though as crossroads where if some value equals for example “John” or the value 1, then output “Hi John” or do some arithmetic operations with the numerical value. Below is the code example of if else statement

#include <iostream>
using namespace std;
int main ()
{
    int userInput;
    
    cout << “Enter your age: ” << endl;
    cin >> userInput;
    
    if(userInput < 0)
    {
      cout << “Are you from other dimension?” << endl;
    }else if(userInput >= 0 && userInput <= 20)
    {
      cout << “You are very young!” << endl;
    }else if(userInput > 20 && userInput <= 30)
    {
      cout << “You are not so young anymore” << endl;
    }else if(userInput > 30 && userInput <= 40)
    {
      cout << “You are in the middle age crisis!” << endl;
    }else if(userInput > 40 && userInput <= 70)
    {
      cout << “You are old!” << endl;
    }else if(userInput > 70 && userInput <= 130)
    {
      cout << “I think you are lying” << endl;
    }else{
      cout << “You have either entered invalid input or you really are a fossil” << endl;
    }
}

To be able to understand what is happening in the above example, you should look my previous post more closely. What happened in the above statement is that the user is asked for the input and the if statement will check what input the user entered and will output messages on the command line according to the user value input. For example in the below line of code

    if(userInput < 0)
    {
      cout << “Are you from other dimension?” << endl;
    }

we are checking that if the user input in the variable userInput is below the numerical value zero/0, console out/output the message on the command line “Are you from other dimension?”. If the user entered value above or equal zero/0 but under or equal 20, then output “You are very young!” in the below line of code.

    else if(userInput >= 0 && userInput <= 20)
    {
      cout << “You are very young!” << endl;
    }

Please note that there is “else if” instead of if. This basically means that if the value was not below zero, jump on the next if statement i.e. “else if” and you just continue using “else if” until you do not want any more dimensions on your program and end it with “else” where everything else will output something you have dictated for example error message etc.

Also note that I have used the && in the program meaning that if the value is over or equal 0 and value is below or equal 20 then output “You are very young!” and I end the statement with brackets where the next else if statement will come right after the bracket.

Below is example of the program in a picture

if statement

and here is some technical gibberish in the below link

http://msdn.microsoft.com/en-us/library/y34a3dk2.aspx

Below is another code example

#include <iostream>
using namespace std;
int main ()
{
    int userInput;
    
    cout << “Enter either 1 or 5: ” << endl;
    cin >> userInput;
    
    if(userInput == 1 || userInput == 5)
    {
      cout << “CORRECT!” << endl;
    }else
    {
      cout << “INVALID!” << endl;
    }
 
    system(“PAUSE”);
    return 0;
}

Where the

    if(userInput == 1 || userInput == 5)
    {

means that if the input is 1 OR the input is 5 output “CORRECT!” else output “INVALID!”.
Below is few pictures

if 2

if 3

Here is more example code related to if statement below

#include <iostream>
using namespace std;
int main ()
{
    int userInput1, userInput2, arithmetic;
    string choise;
    
    cout << “Enter the arithmetic operation(+, -, /, *): ” << endl;
    cin >> choise;
    
    cout << “Enter first whole number: ” << endl;
    cin >> userInput1;
    
    cout << “Enter second whole number: ” << endl;
    cin >> userInput2;
    
    if(choise == “+”)
    {
      arithmetic = userInput1 + userInput1;
      cout << “The value is ” << arithmetic << endl;
    }else if(choise == “-“)
    {
      arithmetic = userInput1 – userInput1;
      cout << “The value is ” << arithmetic << endl;
    }else if(choise == “/”)
    {
      arithmetic = userInput1 / userInput1;
      cout << “The value is ” << arithmetic << endl;
    }else if(choise == “*”)
    {
      arithmetic = userInput1 * userInput1;
      cout << “The value is ” << arithmetic << endl;
    }else{
          cout << “INVALID INPUT!” << endl;
    }
 
    system(“PAUSE”);
    return 0;
}

and below is picture of one possible output

if 4

——————————————————————————————————————————–

Age program version 2

Code below

#include <iostream>
using namespace std;
int main()
{
 int enteredAge;
 
 cout << “Enter your age: ” << endl;
 cin >> enteredAge;
 
 if(enteredAge < 0)
 {
  cout << “Entered age cannot be a negative” << endl;
 }else if(enteredAge >= 0 && enteredAge <= 12)
 {
  cout << “You are a child” << endl;              
 }else if(enteredAge >= 13 && enteredAge <= 19)
 {
  cout << “You are a teen” << endl;
 }else if(enteredAge >= 20 && enteredAge <= 40)
 {
  cout << “You not old!” << endl;              
 }else if(enteredAge >= 41 && enteredAge <= 90)
 {
  cout << “You are old…” << endl;
 }else{
  cout << “You are very…very old” << endl;
 }
    
}

below is a picture of what, happens if the user enters 0.

ss

and with paint…

if

I will publish this and add more content later.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s