Contents  1-5  6-11  12-16  17-21  22-27   28-33  34 - 38  39-46  Projects   MFC

   Contact
   Search
   C
   C++
   Visual Basic
   Java
   JavaScript
   DHTML
   Style Sheets
   About
   Active X
   TDC Binding
   PHP
   Perl and CGI
   Flash
   XML
   SQL
   Messages
   Chat
   MCSE
   Linux
   Cabling   
   ActionScript
   Downloads
   E-Cards   
 
    
    

Note: Updated!  See top of introduction page c1.html for using a C++ .Net compiler for these standard C++ tutorials.

Decision structures allow your programs to make decisions based upon user input or the results of a process.  They
follow the general principles of logic, and in addition to employing relational and mathematical operators, they also
employ logical operators.  Logical operators allow more than one relational expression to be evaluated at a time,
allowing for a greater complexity of decisions as bits can be "AND'd" and "OR'd" together :)

"As the island of our knowledge grows, so does the shore of our ignorance." - John Wheeler

 

Operator Symbol Example
   AND    &&    expression1 && expression2
   OR     ||    expression1 || expression2 
   NOT         !       !expression

logical AND - Evaluates two expressions.  If both expressions are true the AND statement is true.  If one or both expressions are false the AND statement is false.  Example:  if ( (x == 5)  &&  (y == 5))  would evaluate true if both x and y are 5.

logical OR - If either expression is true, the OR statement is true.  Example:
if ( (x == 5)  ||  (y == 5)) would evaluate true if either x OR y is equal to 5.

logical NOT - Evaluates true if the expression being tested is FALSE.  If the expression being tested is false, the value of the test is true:
if ( !(x == 5) ) is true only if x is not equal to 5.  You could also write:    if (x  != 5).

if statement - enable testing for a condition and branching to different parts of code depending on the result.   The syntax is as following:

if(expression)
     statement;

If the expression has the value 0 it is considered false and the statement is skipped.   Example:

if(bigNumber > smallNumber)
   bigNumber = smallNumber;    //if bigNumber is larger it sets its value to smallNumber.

else clause - makes code more easily readible, program follows one branch of code if the condition is true and another branch of code if it is false.  Syntax is as follows:

if(expression)
    statement;
else
    statement;

Example:

#include <iostream.h>
int main()
{
    int firstNumber, secondNumber;
 
    cout << "Please enter a big number: ";
    cin >> firstNumber;
    cout << "\nPlease enter a smaller number: ";
    cin >> secondNumber;
 
    if(firstNumber > secondNumber)
       cout << "\nThanks!\n";
    else
        cout << "\nOops. The second is bigger!";

  return 0;
}

Output =
Please enter a big number:  10
Please enter a smaller number: 12
Oops.  The second is bigger!

Other if statements can be nested in if statements, to process a range of complex and connected decisions.  Example:

#include <iostream.h>
int main()
{
    int firstNumber, secondNumber;
    cout << "Enter two numbers.\nFirst: ";
    cin >> firstNumber;
    cout << "\nSecond: ";
    cin >> secondNumber;
    cout << "\n\n";

    if(firstNumber >= secondNumber)
    {
       if( (firstNumber % secondNumber) == 0)  
//evenly divisible?
       {
           if(firstNumber == secondNumber)
                { cout << "They are the same!\n"; }
           else { cout << "They are evenly divisible!\n"; }
       }
       else
           cout << "They are not evenly divisible!\n";
       }
    else
      cout << "Hey! The second one is larger!\n";

    return 0;
}

Output:

Output =
Enter 2 numbers.
First:    10
Second:    2
They are evenly divisible!

Watch your placement of braces and brackets. The example below illustrates a logic error due to lack of braces:  

#include <iostream.h>
int main()

{
    int x;
    cout << "Enter a number less than 5 or greater than 50: ";
    cin >> x;
    cout << "\n";
    if(x > 5) 
       if(x > 50)
           cout << "Greater than 50, Thanks!\n";
       else                           
//no braces - not the else intended!
           cout << "Less than 5, Thanks!\n";
 
    return 0;
 }

In the example above, if the user entered 30, the program would still display "Less than 5, Thanks!".  This is because without braces, the "else" corresponds to the "if" directly above it, testing whether x > 50, rather than the first "if" which is testing whether x > 5.  The next program uses braces to accomplish the task without the previous bug:

#include <iostream.h>
int main()
{
    int x;
    cout << "Enter a number less than 10 or greater than 100: ";
    cin >> x;
    cout << "\n";
   
    if(x > 10)
    {
       if(x > 100)
           { cout << "More than 100, Thanks!\n"; }
       else
           { cout << "Less than 100 but more than 10."; }
    }
    else                           
//now in braces - it's fixed!
       { cout << "Less than 10, Thanks!\n"; }

    return 0;
 }

Relational Precedence - Relational operators and logical operators each return 1 (true) or 0 (false).  They have a precedence order that determines which relations are evaluated first.  Example:

if( x > 5  &&  y > 5  ||  z > 5)  is better rewritten with parentheses:    if( (x > 5)  &&  (y > 5  ||  z > 5)  )

More about truth and falsehood:
if (x)
     x=0
;

Can be read as "If x is true (has a non-zero value), set it to 0".  It would be clearer written as:
if (x != 0)
     x = 0;

So these two statements are also equivalent;
if (!x)     //if x is false (zero)
if (x == 0) 
   //if x is zero

switch(case) statement - A switch statement is a decision structure possessing a default and several cases.  Switch structures allow you to branch off on any of a number of different values.  "if" and "else...if" combinations can become confusing when nested too deeply.   The switch statement evaluates "expression" and compares the result to each of the case values.  The evaluation is ONLY for equality.  Relational operators (<>) and Boolean expressions can not be used here.  If one of the case values matches the expression, execution jumps to those statements and continues to the end of the switch block unless a break statement is encountered.  If nothing at all matches, execution branches to the "default" statement.  If there is no default statement and there is no matching value, execution falls through the switch statement and the statement ends.  You should always include a default case in switch statements, if only to render an error message.

Note: If there is no break statement at the end of a case statement, execution will fall through to the next case.  This is sometimes necessary, but is usually an error.  If you do decide to let execution fall through, you might consider placing a comment indicating your intention and that you simply didn't just forget the "break" statement.  Example:

#include <iostream.h>

void main() {

     cout << "\nEnter your choice(1-7):\t";
     cin >> choice;
     switch(choice) 
     {
             case 0 : RunProgram = 0;
                      break;
             case 1 : cout << "You chose 1";
	              break;
             case 2 : cout << "You chose 2";
	              break;
             case 3 : cout << "You chose 3";
	              break;
             case 4 : cout << "You chose 4";
	              break;
             case 5 : cout << "You chose 5";
	              break;
             case 6 : cout << "You chose 6";
	              break;
             case 7 : cout << "You chose 7";
	              break;
             default : cout << "Sorry, invalid choice.";
	               break;
     } //close switch statement
} //end main()

 


©2004 C. Germany