Home     Blog

Conditional Operators and Switch Statements

C++ has an operator that can be used as an alternative to an if-statement. This operator is called the conditional operator (?:). This operator can be used to replace an if-else statement of the following general form:

The above form of if-else can be alternatively written using ?: as follows,

It works in the same way an if-statement executes. The execution is in the following manner,

  • expression1 is evaluated
  • If the condition results into a true value in expression1 then expression2 is executed
  • If the condition results into a false value in expression1 then expression3 is executed

Example:

can be alternatively written as,

conditional switch statement1 Conditional Operators and Switch Statements

How does a Switch statement work?

C++ provides a multiple-branch selection statement know as switch. This selection statement successively tests the value of an expression against a list of integer or character constants. When a match is found, the statements associated with that constant are executed.

Syntax:

The expression is evaluated and its value is matched against the value of the constant specified in the case statements. When a match is found, the statement sequence associated with that case is executed until the break statement or the end of switch statement is reached. If a case statement does not include a break statement, then the control continues to the next case statement until either a break is encountered or end of switch is reached. When a break statement is encountered in a switch block, program execution jumps to the line of code immediately following the switch block i.e. outside the body of switch statement. The use of default statement is optional. It only executes when no case conditions match.

conditional switch statement2 Conditional Operators and Switch Statements

Example: Following code shows the implementation of switch statement

conditional switch statement3 Conditional Operators and Switch Statements

VN:F [1.9.17_1161]
Rating: 0.0/10 (0 votes cast)
Follow Will.Spencer on

Leave a Reply

Related Posts

  • break and continue Statements

    break Statement The break statement is used to alter the flow of control. When a break statement is executed in a while loop, for loop, do-while loop or switch statement, it causes immediate exit from that statement. Program execution continues with the next statement. Common uses of the break statement are to escape early from [...]...


  • if Statement

    An if/else statement tests a particular condition, if the condition evaluates to true, a course-of-action is followed i.e. a statement or a set of statements is executed. Otherwise another set of instructions are executed (the else block). Such statements are called program control statements that control the flow of execution in a program. Normally, the [...]...


  • Overloading Binary Operators

    Binary operators can be overloaded in a similar manner as unary operators. We should note the following features of an operator function for a binary operator: It receives only one class type argument explicitly, in case of a member function. For a friend function, two class types are received as arguments. It returns a class [...]...


  • Overloading Unary Operators

    Unary operators are the ones that operate on one operand, one such operator is the unary minus (-) operator which is used to change the sign of the operand it acts upon. This operator works well with basic data types such as int, float, etc.. In this section we will see how to overload this [...]...


  • Postfix

    The sum of X and Y is written as X+Y where + is the operator while X and Y are the operands. We have always learnt to write the sum of two numbers as X + Y; this notation is know as infix. Here, we’ll be talking about the postfix notation of representing arithmetic operations. [...]...