.Open 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. .Table Operator Condition When True .Row == if and only if left operand is equal to right operand .Row != if and only if left operand is not equal to right operand .Row > if and only if left operand is greater than right operand .Row < if and only if left operand is less than right operand .Row >= if and only if left operand is greater than or equal than right operand .Row <= if and only if left operand is less than or equal than right operand .Close.Table A simple condition for testing if a variable named number is less than zero, would be as follows: .As_is int number = 0; .As_is 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++. .Table Logical Operator What It Does .Row && Logical AND .Row || Logical OR .Row ! Logical NOT (or negation) .Close.Table The following table contains the truth table for the above logical operators. .Table p q p && q (AND) p || q (OR) !p (NOT) .Row True True True True False .Row True False False True False .Row False True False True True .Row False False False False True .Close.Table In C++, suppose we have the following: .As_is int x = 5; // declare a variable x as integer, and initialize to 5 .As_is int y = 4; // declare a variable y as integer, and initialize to 4 .As_is bool test; // declare a variable test as boolean .As_is 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: .As_is if (condition) .As_is { .As_is statement(s); .As_is } 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: .As_is if ( number < 0 ) .As_is { .As_is cout << "The number is less than zero!" << endl; .As_is } 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: .As_is if ( number < 0 ) .As_is 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: .As_is if (condition) .As_is { .As_is statement(s)_to_execute_when_condition_is_true; .As_is } .As_is else .As_is { .As_is statement(s)_to_execute_when_condition_is_false; .As_is } 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: .As_is int numerator = 120; .As_is int denominator = 6; .As_is int result; .As_is ..... // some C++ code doing something else here .... .As_is if (denominator == 0) // if denominator is zero, cannot divide by zero! .As_is { .As_is cout << "Cannot divide by zero!" << endl; .As_is result = 0; // **Could we use 0 in the result to tell a division by zero has occurred? .As_is } .As_is else .As_is { .As_is result = numerator / denominator; .As_is } ** - 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: .As_is int grade = 88; // student grade .As_is char letter_grade; .As_is if (grade >= 90) .As_is { .As_is letter_grade = 'A'; .As_is } .As_is else if (grade >= 80) .As_is { .As_is letter_grade = 'B'; .As_is } .As_is else if (grade >= 70) .As_is { .As_is letter_grade = 'C'; .As_is } .As_is else if (grade >= 60) .As_is { .As_is letter_grade = 'D'; .As_is } .As_is else .As_is { .As_is letter_grade = 'F'; .As_is } . 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: .As_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: .As_is if ( condition ) .As_is { .As_is expression_when_condition_true; .As_is } .As_is else .As_is { .As_is expression_when_condition_false; .As_is } . Code Snippets Any code snippets, or code we do in class or lab can be found at: .See rcc.csc5.code.html . Riverside City College Courses Go back to Riverside City College Courses page: .See rcc.courses.html .Close CSC 5 - C++ Conditionals Notes