Exforsys

Home arrow Technical Training arrow C++ Tutorials

C++ Decision Making Statements Page - 2

Page 2 of 2
Author: Sripriya R     Published on: 1st Sep 2007    |   Last Updated on: 25th Jul 2011

C++ Decision Making Statements

switch statement:

If there are a number of decision making conditions instead of making an if..else construct, the programmer can use switch statement.

Ads

The general syntax is:

Sample Code
  1. switch (expression)
  2. {
  3.    case constant1:
  4.    group of statements 1;
  5.    break;
  6.    case constant2:
  7.    group of statements 2;
  8.    break;
  9.    .
  10.    .
  11.    .
  12.    default:
  13.    default group of statements
  14. }
Copyright exforsys.com


In the above example, the expression is first evaluated and the value id checked with the constant1 in the case statement. If this is evaluated with the same value, then the group of statements is executed. When a break statement is encountered, the control is switched to the end of the switch statement. If the value of the expression is not equal to constant1, it is checked with constant2. If it evaluates the same value, then the group of statements (in the case of constant2) is executed. When a break statement is encountered, the control is then switched to the end of the switch statement. This proceeds until the value of expressions equal one of the constant values given. If the value of the expression is not equal to any of the constant values given, then, by default, the statements present are executed.

For example:

Sample Code
  1. #include <iostream>
  2. using namespace std;
  3. void main()
  4. {
  5.    int x;
  6.    cout<<"Enter Input Value: ";
  7.    cin>>x;
  8.    switch(x)
  9.    {
  10.       case 10:
  11.       cout<< "Value is 10";
  12.       break;
  13.       case 20:
  14.       cout<<"Value is 20";
  15.       break;
  16.       default:
  17.       cout<<"Invalid Input";
  18.    }
  19. }
Copyright exforsys.com


The output of the above program is

In the above example, the output produces:

  • Enter Input Value:10
  • Value is 10

Since the entered input is 10, which is equal to the first case value, the statement in the first case block is executed.

  • Enter Input Value:50
  • Invalid Input

Since the entered input is 50, which is not equal to any of the case values, the default statement is executed.

Suppose the programmer wants the same block of statements to be executed for a number of case values. This is performed by removing the break statement. If a break statement is removed, then the control inside the end of a switch statement will move to the next case statement and proceed until it finds a break statement.

Important care must be taken not to place variables in case values. Only constants can be placed and expressions of the switch statement can be compared only with constants in case statements.

conditional operator:

As we have seen before in the earlier chapter covering operators in C++, the conditional operator actually is a form of if…else statement.

Sample Code
  1. if(a < b)
  2.    m=a;
  3. else
  4.    m=b;
Copyright exforsys.com


could be written using conditional operators as

Sample Code
  1. m=(a < b)? a:b;
Copyright exforsys.com


Read Next: C++ Looping


 
 

Comments