Case statements in C++ program

Asked By 10 points N/A Posted on -
qa-featured

How to use Case statements in C++ program and what is its advantages and disadvantages?

SHARE
Best Answer by bertoso
Answered By 5 points N/A #103119

Case statements in C++ program

qa-featured
Hi Jeffrey and generally the simplest language C++ is a language that is developed from C which is a programming language that is developed at AT & T's Bell Laboratories of USA in 1972 by Dennis Ritchie who Designed and written it completely and C became popular due to its ease of use and advantages and extra features are added to C++.
 
Case statements plays a major role in helping the controlling of conditional operations and then transfers the control to the statement within its body.
 
Syntax for case is as follows.
 
Case expression : Statement//Expression
 
Default : Statement//default value
 
The case statements mainly used because of the multiples choices like to display white color means we will use one case and other cases for others and by default a value is given if it is not matched with any case.
 
Thank you.
Best Answer
Best Answer
Answered By 0 points N/A #103120

Case statements in C++ program

qa-featured

Hi Jeffrey,

The case statements or the so-called switch-case statement is a good alternative to cascading if-else statements.

The syntax for the switch-case statement is as follows:

switch(<expression>)

{

case <label-1>: <statement-1>;

[break;] [optional]

case<label-2>: <statement-2>;

[break;] [optional]

case<label-3>: <statement-3>;

[break;] [optional]

case<label-n>: <statement-n>;

[default: <statement-d>;] [optional]

[break;]

}

  • The expression may be an integer or character literal or as the name suggests, an expression that evaluates to an integer or a character value.
  • The use of float and double data type values will result into an error.
  • The <expression> is evaluated and the value compared is within each of the case labels. The case labels must have the same type as the <expression> and they must be unique.
  • If the value of the <expression> matches with the case label, the statement(s) the follow will be executed until it reaches the [break]; statement (if present).
  • If none of the case labels matched, the statement(s) in the <default> label is executed (if one exists).
  • Without the break statement, the statements in following case labels are executed up to the last label.
  • Statements associated with a case label can be a single or a sequence of statements.
  • Curly brackets can also be added in each case label to make it more readable.

Disadvantages:

  • Declarations after the case label without the {} is ignored by the compiler. The switch statement transfers control directly to an executable statement within the body, bypassing the lines that contain initializations.
  • Can cause waterfall effect if you forgot to add break before end the case label block (if you intend to).

Advantages:

  • A switch statement CAN SOMETIMES be more efficient than an if / else statement, because it is easy for a compiler to generate jump tables from switch statements. It is possible to do the same thing for if / else statements, given appropriate constraints, but that is much more difficult.
  • With a large number of strings, there is a significant performance advantage to using a switch statement, because the compiler will use a hash table to implement the jump.
Answered By 10 points N/A #103121

Case statements in C++ program

qa-featured

Thank you Hoting Gracia and Bertoso.

Thank you for telling me the pros and cons of the C++ program. TechyV is the best!

Related Questions