C++ execution control statement : if() else{ }

C++ execution control statement are used to channel the compiler on which code to be executed when a certain condition is fulfilled.They are use as a decision making statement.There are six execution control statement in C++,here we will discuss the ‘if() else‘ execution statement.

Syntax

The syntax of ‘if() else’ execution statement follows the form as shown below:

if( expression1)
{
//code 1
}
else 
{
//code 2
}

 

 

The ‘expression1’ inside if() determine which code among ‘code 1’ and ‘code 2’ is executed.If ‘expression1’ is true or 1 then the ‘code 1’ is executed and if ‘expression1’ is false or 0,then ‘code 2’ is executed.A code example is given below.

int ii=90;

if( ii>100 )
{
cout<< "ii is greater than 100";
}
else
{
cout<< "ii is smaller than 100" ;
}

The output is,

ii is smaller than 100

In the above example ii is 90 and inside ‘if statement’ we check if ii is greater than 100 but ii is smaller,so the expression ‘ii>100’ is false and so the code inside ‘else’ statement is executed.

The expression inside ‘if()’ bracket can be a single expression or a complex expression but the result it yield must always be true or false.

Here is another example.

int a=89 , b= 34 , c=3 , d=0 ;
string st="New";

/** when the expression is a simple expression **/
cout<<"When the expression is a simple expression!!\n";
if( a )
{
cout<< "a return true\n" ;
}
else
  cout<<"a return false\n";


/** when the expression is a complex one ***/
cout<<"\nWhen the expression is a complex expression!!\n";
if( (a-b)<(c+d) )
{
 cout<<"(a-b)<(c+d) return true\n";
}
else
{
 cout<<"(a-b)<(c+d) returns false";
}

/**When the expression does not return true or false **/
cout<<"\nWhen the expressiondoes not return true or false!!\n";
if( st ) //error!!
{
 cout<<"st is true";
}
else
{ 
 cout<< "st is false";
}

If you run the program output is,

When the expression is a simple expression!!
a return true

When the expression is a complex expression!!
(a-b)<(c+d) returns false

Any integer of real number or character which is not 0 will always return true hence in the first ‘if’ statement we get ‘a return true’ as the output.
 
In the second ‘if’ statement the expression ‘(a-b)<(c+d)' is same as '(89-34)<(3+0)' =50<3,which is false hence we get '(a-b)<(c+d) returns false'.
 
In the third ‘if’ statement the string ‘st’ is used as an expression,here nothing makes sense because string cannot be interpreted to either true or false value,so the third ‘if’ statement is an error.

**Note a single string cannot be used as an expression to determine true or false in ‘if’ statement but we can use two or more strings comparison expression in ‘if’ statement.The next section discuss this topic.


Using string as an expression in ‘if’ statement

In the previous code above we have seen that using a string as an expression in ‘if’ statement is just an error.However,if we use two strings and performs some operation between them to get the resulting value as true of false then the expression can be used in the ‘if’ statement.

string st="happy" , st1="hapy";

if( st==st1 )
{
 cout<<"st and st1 are equal";
}
else
 cout<<"st and st1 are not equal";

Output,

st and st1 are not equal

The expression ‘st==st1’ compares if the two strings have the same characters but they don’t,so the code inside ‘else’ is executed and hence we get ‘st and st1 are not equal’ as the output.

Note we can also use ‘>’ or ‘<' or '!=' operator to compare two strings.


Adding many executable cases with if() else statement

With the above normal ‘if() else’ statement form we can execute only two codes or a program for only two case can be made-one for true and the other for false.If we need more codes to be executed under many more cases then we will add extra ‘else if‘ statement before the ‘else’ statement.

int i=10 ;

if( i==1)
{
 cout<<"i is 1";
}
else if(i==5)
{
 cout<<"i is 5";
}
else
{
 cout<<"i value is not known";
}

Output,

i value is not known

Note you can add more ‘else if()’ statement and the number of times we can add the statement is limitless.

Here is another code example.

string st="New world" , st1="Kaidou" ;

if( st>st1 )
{
 cout<< st << " > " << st1 ;
}
else if( st==st1)
{
 cout<< st << " == " << st1 ;
}
else if(st<st1)
{
 cout<< st << " < " << st1 ;
}
else
 cout<< "Which is greater cannot be determined";

Output,

New world > Kaidou

Why do we get ‘New world > Kaidou’ as the output.When comparing two strings for which one is greater than the other each of their characters are compared and if any first characters is found greater than the other then true is returned no other characters are check.Now in “New world” and “Kaidou”,the first character ‘N’ is greater than the first character ‘K’ (check out the ASCII chart) so it return true(no other characters are check).




Leave a Reply

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