rcc.csc5.cpp.conditionals || [Main Index] || [ MATHS ] || [Notation] || [Copyright] || [Contents] || [Source Text] || [Dictionary]
Last updated: Wed Nov 18 16:42:37 PST 2015

Contents


    CSC 5 - C++ Conditionals Notes

    Introduction

    This page serves as a quick access to notes on conditional constructs. In programming, there are often times in a C++ program the need to control the execution flow of a program. If there is a decision to be made to have a statement or block of statements to be executed depending on a condition, there needs to be conditional constructs that allow the program to achieve this need.

    Conditions

    Conditions are expressions that are evaluated to a boolean value, true or false, Simple conditions typically involve two operands, but we can have more than two operands. Each operand can be a variable or a literal value, and an operator. The operator is a comparison operator.

    OperatorCondition When True
    ==if and only if left operand is equal to right operand
    !=if and only if left operand is not equal to right operand
    >if and only if left operand is greater than right operand
    <if and only if left operand is less than right operand
    >=if and only if left operand is greater than or equal than right operand
    <=if and only if left operand is less than or equal than right operand

    A simple condition for testing if a variable named number is less than zero, would be as follows:

     int number = 0;
     bool test = number < 0; // test evaluates to false since number is not less than zero.

    We can also have more complex conditions called composite conditions, which are multiple conditions joined by logical operators. The table below contains the three logical operators that are commonly used in C++.

    Logical OperatorWhat It Does
    &&Logical AND
    ||Logical OR
    !Logical NOT (or negation)

    The following table contains the truth table for the above logical operators.

    pqp && q (AND)p || q (OR)!p (NOT)
    TrueTrueTrueTrueFalse
    TrueFalseFalseTrueFalse
    FalseTrueFalseTrueTrue
    FalseFalseFalseFalseTrue

    In C++, suppose we have the following:

     int x = 5; // declare a variable x as integer, and initialize to 5
     int y = 4; // declare a variable y as integer, and initialize to 4
     bool test; // declare a variable test as boolean
     test = ( x > 0 ) && ( y > 0 ); // test should be true since both conditions x > 0 and y > 0 are true.

    If Statements

    The most simple and fundamental statement that can be found with conditional constructs is the if-statement. The syntax of the if-statement is as follows:

     if (condition)
     {
         statement(s);
     }

    In which, condition is the boolean condition being tested, and statement(s) contains the code that we wish to execute when condition is true. Note: the curly braces { and } are only needed when there are more than one C++ statement to execute inside the if-statement's code block.

    Suppose in a program, we want to write a message to the console, telling the user the value of some variable named number is less than zero. We could write the following C++ code:

     if ( number < 0 )
     {
         cout << "The number is less than zero!" << endl;
     }

    Since there is only one statement following the if-statement in the block to execute when the condition is true, we could also write the C++ code as:

     if ( number < 0 )
         cout << "The number is less than zero!" << endl;

    Both of these C++ code segments will do the same thing when executed.

    If/Else Statements

    Consider having a programming decision where we have two possible actions, in which we want to execute one action or the other action, based on a given condition. We can use the if/else statement to achieve this.

    The syntax for the if/else statement is:

     if (condition)
     {
         statement(s)_to_execute_when_condition_is_true;
     }
     else
     {
         statement(s)_to_execute_when_condition_is_false;
     }

    Suppose in a program, we are tasked with the following: if the value of a variable named denominator equal to zero, we output to the terminal screen the text: "Cannot divide by zero!", otherwise we output the result of numerator variable divided by the denominator variable. How could we implement this? A simple example of how is:

     int numerator = 120;
     int denominator = 6;
     int result;
     ..... // some C++ code doing something else here ....
     if (denominator == 0) // if denominator is zero, cannot divide by zero!
     {
         cout << "Cannot divide by zero!" << endl;
         result = 0; // **Could we use 0 in the result to tell a division by zero has occurred?
     }
     else
     {
         result = numerator / denominator;
     }

    ** - Answer to the question in the comment is "maybe", if we say yes, then how could we tell if it was division by zero, or something else like 0 / 10, which also yields zero? Perhaps having a boolean flag checking if division by zero would be a better approach?

    If/Else If/Else Statements

    Consider having a programming decision where we have more than two possible actions, in which we want to execute one action or any of the other actions, based on a set given conditions. We can use the if/else if/else statements to achieve this. For example, consider the grading scale we have for class:

     int grade = 88; // student grade
     char letter_grade;
     if (grade >= 90)
     {
         letter_grade = 'A';
     }
     else if (grade >= 80)
     {
         letter_grade = 'B';
     }
     else if (grade >= 70)
     {
         letter_grade = 'C';
     }
     else if (grade >= 60)
     {
         letter_grade = 'D';
     }
     else
     {
         letter_grade = 'F';
     }

    Ternary Operator ?:

    The ternary operator is a quick convenient way to write an expression that has two possible values true or false, based on a condition. The general syntax for this operator is:

     condition ? expression_when_condition_true : expression_when_condition_false;

    In the above expression, the program will evaluate expression_when_condition_true when the evaluation of the condition yields true, and if the condition is evaluated as false, then expression_when_condition_false is evaluated instead. The ternary operator is very similar to:

     if ( condition )
     {
          expression_when_condition_true;
     }
     else
     {
          expression_when_condition_false;
     }

    Code Snippets

    Any code snippets, or code we do in class or lab can be found at: rcc.csc5.code.html

    Riverside City College Courses

    Go back to Riverside City College Courses page: rcc.courses.html

    . . . . . . . . . ( end of section CSC 5 - C++ Conditionals Notes) <<Contents | Index>>