- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Conditionals: Simple Statements

Posted by Tayyab


The "if" Statement
It's time to make decisionsusing the relational operators.

The "if" expression is called a conditional statement (decision statement).  It tests a relationship by using the relational operators.  Based upon the results of these comparisons, decisions are made as to which statement(s) the program will execute next. 
if (test condition)
{
      block of one;
      or more C++ statements;
}
If you put a semicolon after the test condition, the "if" statement will STOP.  The block will not be executed.
The test condition can be any relational comparison (or logically TRUE) statement and must be enclosed in parentheses.  The block of statements is enclosed in French curly braces and are indented for readability.  The braces are NOT required if only ONE statement follows the "if", but it is a good idea to get in the habit of including them.
The block executes only if the test condition is TRUE.  If the test condition is FALSE, the block is ignored and execution continues to the next statement following the block.
Be careful!!!
   A careless mistake may go unnoticed.
if (a = 3)  is almost ALWAYS true.  The condition

a = 3 is actually an "assignment statement" (most likely NOT what you really intended to type), and assignments are consider to be true for any value other than 0.  If the assignment value is 0, such as if (a = 0), the condition is considered false.

whereas:

if (a = = 3)   is true ONLY if the value stored in a is the number 3.  This is most likely what you intended to type.
 

Examples:
// using a character literal
if (grade = = 'A')
{
     cout<<"Put it on the frig!";
}
// logically true test condition -
// when x is a non-zero value the
// test condition is considered true

if (x)
{
     cout<<"Way to go!";
     cout<<"The x is not zero!";
}
// using two variables
if (enemyCount < heroCount)
{
     cout<<"The good guys win!";
}
// using a calculation
if (cost * number = = paycheck)
{
     inventory = 0;
}

The "if ... else" Statements
Let's look at situations when the word"otherwise" can be used in your decision making!
if (test condition)
{
      block of one;
      or more C++ statements;
}
else
{
     block of one;
     or more C++ statements;
}
If the test condition is TRUE, the block of statements following the "if" executes.  If the test condition is FALSE, however, the block of statements following the "else" executes instead.  
Example:
//example of if ... else
cout<<"Enter the name of your city."
cin>>name;
cout<<"Enter the population";
cin>>population;
if (population >= 534187)
{
     cout<<"According to the census, " << city
            <<" is one of \n the 100 largest cities.";
}
else
{
     cout<<"According to the census, " << city
             <<"is not one of \n the 100 largest cities.";
}

The "if ... else if ... else" Statements
Let's look at situations where your program requires more than two situations as the result of a decision.
cout<<"Enter a value for a";
cin>>a;
cout<<"Enter a value for b";
cin>>b;
if(a= = b)
{
     cout<<"They are equal!\n";
}
else if( a < b)
{
     cout<<"The first number "
             << "is smaller.\n";
}
else
{
     cout<<"The second number"
             << is smaller.\n";
}

Notice that it was not necessary to check for a>b in the third situation.  Thetrichotomy principle tells us that numbers are related in one of three ways (a equals b, or a is less than b, or a is greater than b).  In order for the computer to get to the "else" condition in the problem above, it must have answered NO to the first two decisions.  Consequently, there is only ONE possibility left and we do not waste the computer's time checking it.

The "switch" Statement
If your program must select from one of many different actions, the "switch" structure will be more efficient than an "if ..else..." structure.

switch (expression)
{
     case (expression 1):    {
                                            one or more C++ statements;
                                            break;
                                            }
    case (expression 2):     {
                                           one or more C++ statements;
                                           break;
                                           }
   case (expression 3):     {
                                          one or more C++ statements;
                                          break;
                                          }
                    .
                    .
                    .

    default:                        {
                                        one or more C++ statements;
                                        break;
                                        }
}
  • You MUST use a break statement after each "case" block to keep execution from "falling through" to the remaining case statements.
  • Only integer or character types may be used as control expressions in "switch" statements.
  • It is best to place the most often used choices first to facilitate faster execution.
  • While the "default" is not required, it is recommended.

If you need to have several choices give the same response, you need to use the following coding style:
switch (value)
{
     case (1):
     case (2):
     case (3):   {
                       //The case code for 1, 2, 3
                       break;
                       }
     case (4):
     case (5):
     case (6):   {
                      //The case code for 4, 5, 6
                      break;
                      }
     default:    {
                     //The code for other values
                     break;
                      }
}



<< Previous                                                                                                                 Next >>    

0 comments:

Post a Comment