alt
Advertisement
Online Training
Career Series
Exforsys
Exforsys arrow Tutorials arrow C Plus Plus arrow C++ Decision Making Statements
Site Search


C++ Decision Making Statements
Article Index
C++ Decision Making Statements
switch statement

C++ Decision Making Statements

In this C++ tutorial, you will learn about decision making statements if statement, if…else statement, switch statement, conditional operator along with syntax and examples.

Decision-making is an important concept in any programming language and to accomplish this, C++ uses the following decision making statements:

  • if statement
  • if…else statement
  • switch statement
  • conditional operator

Explanation of decision-making statements in C++ programming language.

if statement:


if (condition returns true)
{
   statement 1;
   statement 2;
   ……………
}

If the condition returns true value then all the statements inside the braces of if block are executed. Otherwise, if the condition returns false, then the statement outside the if block is executed. If there is only one statement to be executed if the condition returns true in an if statement then it can be written without the braces.

For Example:

The programmer desires to retrieve the input from the user, check the number value and wishes to print the output only if it is greater than 50. Thus, this can be performed as follows:


#include <iostream.h>
void main()
{
   int a;
   cout << “Input the number:”;
   cin >> a;
   if (a>50)
   cout << a;
}

There is only one statement to execute if the if statement returns true value, therefore, the braces are not placed.

Suppose, in the above program, if the condition returns true value. The programmer wants to print the value of a first and then add 10 to a and print. Braces are then used for the if block as follows:


#include <iostream.h>
void main()
{
   int a;
   cout << “Input the number:”;
   cin >> a;
   if (a>50)
   {
      cout << a;
      a=a+10;
      cout << a;
   }
}

if…else statement:

If has several levels of condition checking. When the if statement condition evaluates to false, then another condition in the false block must be placed by using an else if statement. If the if statement evaluates to true, then all the statements inside the if block are executed and the else if will be ignored. When the if statement is false, it will then check the condition in the else if statement. If this statement evaluates to true, then the statements in that block are executed else the following else if condition is checked and continued. If all statements are false when the programmer places an else statement, the statements in this block are executed. This is referred to as a nested if…else statement.


if (condition returns true)
{
   statement 1;
   statement 2;
   ……………
}
else if (condition returns true)
{
   statement 1;
   statement 2;
   ……………
}
   ………………
   ……………………
else
{
   statement 1;
   statement 2;
   ……………
}

 for example:


include <iostream.h>
void main()
{
   int grade;
   cout<<"Input the Training Grade Level:"
   cin>> grade;
   if ( grade > 100 )
   {
      cout<<"Best Level";
      cout<< grade;
   }
   else if ( grade == 100 )
   {
      cout<<"Good Level";
      cout << grade;
   }
   else
   {
      cout<<"Needs Improvement";
   }
}

 The output of the above program is


Input the Training Grade Level: 150
Best Level 150

Since the input was greater than 100 the, if block got executed.

Suppose the input is 90 the output would be because here the if and the else if condition both failed and therefore statement in else block got executed
The output of the above program is


Input the Training Grade Level: 90
Needs Improvement



 
< Prev   Next >
Sponsored Links
© 2008 Exforsys.com
Joomla! is Free Software released under the GNU/GPL License.
Page copy protected against web site content infringement by Copyscape