The if statement



1.1.  
The if statement

The if statement enables you to conditionally branching the flow of the program and this flow depends on whether or not the condition, behind the keyword if, is satisfied or not. The simplest form of the if statement is:
If ( logical_expression )
    // the command block

If the value of the expression after or behind the word if is logical truth, then the program executes the block of statements or commands following this expression.  If the value of the expression after or behind the word if is logical untruth (false) then this block of commands is skipped and the execution of the program is continuing form the first command after this block. For example:
if ( a < 0 ) {
      cout << “ The number a is negative!” << endl;
}
In case that the command block contains only one command, that the curly braces are not  required and they can be neglected, so we can write the previous example in this form:
if ( a < 0 )
      cout << “ The number a is negative!” << endl;
, or:
if ( a < 0 )  cout << “ The number a is negative!” << endl;

VERY IMPORTANT Because of the clarity of the code and concerns that may arise from alterations I strongly recommend that beginners use curly braces and write commands on a new line. Otherwise it can happen that you add a new command but forget to add curly brace to close the command block.

1.2. The if else statement

The syntax of this expression is:
if (logical_expression)
      statement1
else
      statement2
If the logical expression in if condition gives as a result the logical truth (is true) then the program will execute the statement1. After the program executes the statement1, the program will continue to execute the first command line after the if/else command block. If the logical_expression in if condition gives as a result logical untruth (flase) then the first statement or statement1 is skipped and the program will execute the statement2. When the program executes the statement2 it will continue with execution from the first command line after the if/else command block. Here is an example of If/else command block.
Example – The if/else statement
Here’s a simple example of a program which calculates the intersections of the line with the axes of the Descartes coordinate system. First of all start a new Win32 Console Application Project in Microsoft Visual Studio, name this project and in the Win32 Console Application Wizard click Next and then Finish. After you’ve created a new project write the following code
// IF Statement.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
            cout << "Enter the coefficients of a line:" << endl;
            float a, b, c;                                                                           cout << "a=";                                                                         cin >> a;
            cout << "b=";                                                            
            cin >> b;
            cout << "c="; 
           cin >> c;                                                         
            cout << "Coefficients are: " << a << ", "
                         << b << ", " << c << endl;
            cout << "Intersection with the abscissa: ";
            if (a != 0)
                        cout << -c / a << ", ";
            else
                        cout << "none, ";                                           
            cout << "Intersection with the ordinate: ";
            if (b != 0)
                        cout << -c / b << endl;
            else
                        cout << "none " << endl;                  
           return 0;
}











//Coefficients of a line
//Enter a value of a variable a

//Enter a value of a varialbe b

//Enter a value of a variable c





//Line is horizontal


//Line is vertical







After entering this code start debugging this program code. If you have entered this program code correctly than the program should run with no problems. If you have syntax errors than correct them and try debug the program again.  When you have finished with debugging press Ctrl+F5 and start your program.


This program is very simple. First of all we have included two standard libraries iostream and stdafx.h. The library stdfax.h is automatically generated with Microsoft Visual Studio. Then we have entered the namespace std which is necessary for cout, cin and endl commands. After the function int_tmain we have declared variables a, b, and c and use cout and cin commands to enter their values. After entering their values we have used the if/else statements to calculate the intersections of the line with axes of the coordinate system. If the value of the variable a is not equal to the 0 than the program will calculate –c/a and if the a is equal to zero the program will execute the first else statement. This means that the line is horizontal. In second if/else statement if the value of the variable b is not equal to zero than the program will calculate the value of –c/b. This means that the program will numerically determine the value of the point on the vertical axes on which the line intersects with the vertical axes. If the value of the variable b is equal to zero than the program will skip the if statement and execute the else statement. This means that the line doesn’t intersects with the vertical axes of the coordinate system.

1.3.  If and else if statements

The syntax of this statement is:
If (logical_expression1)
    Statement1
Else if (logical _expression2)
    Statement2
Else if (logical_expression3)
    Statement3
Else
    Statement_n

If the logical_expression1 is equal to logical truth, then the program will execute the statement1. After the program executes the statement1 the program will continue executing the first command line after the command block. If the logical_expression1 is false then the program will skip statement1 and if the logical_expression2 is equal to the logical truth than program will execute the statement2. After program executes the statement2 it will skip execution of other block commands and will start execution of the first line after the command block. If the logical_expression3 is equal to the logical truth than program will execute the statement3.After the execution of the statement3 the program will start executing the first line after the command block. This if/else if will be illustrated with the example in which we will calculate the value of the discriminant.
// IF3.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
            cout << "Discriminant example:"<< endl;
            cout << endl;
            cout << endl;
            double a, b, c;
            cout << "Enter coefficients of the quadratic equation:" << endl;
            cout << endl;
            cout << "Enter the value of variable a: " << endl;
            cin >> a;
            cout << endl;
            cout << "Enter the value of variable b: " << endl;
            cin >> b;
            cout << endl;
            cout << "Enter the value of variable c: " << endl;
            cin >> c;
            cout << endl;
            double D = b * b - 4. * a * c;

            cout << " The equation has: ";
            if (D == 0)
                        cout << "double real root." << endl;
            else if (D > 0)
                        cout << "two real roots." << endl;
            else
                        cout << "two complex roots." << endl;
            return 0;
}

The program looks like this. 

Share on Google Plus