Switch Statement in C++

Switch statement is a substitute for long if statements that compare a variable to several integral values (integral values are simply values that can be expressed as an integer, such as the value of a char). The basic format for using switch case is outlined below. The value of the variable given into switch is compared to the value following each of the cases, and when one value matches the value of the variable, the computer continues executing the program from that point.

Switch Statement Syntax

switch (value) {

case this-value:

Code to execute if value == this-value
break;

case that-value:

Code to execute if value == that-value
break;

default:

Code to execute if value does not equal any of the cases
break;

}

Switch Statement Example

int age;
cout << “What is your age: “; cin >> age;

switch (age)
{

case 1:

cout << “You are 1 year old”;
break;

case 2:

cout << “You are 2 years old”;
break;

case 3:

cout << “You are 3 years old”;
break;

default:

cout << “You are older than 3 years”;

}

Break Statement

The break command can be used to exit a loop at any time. Here is an example that will print “Hello” 5 times and then break out of the loop.

int x;
for (x = 1; x <= 10; x++)
{

cout << “Hello\n”;
if(x == 5)
break;

}

Continue Statement

The continue command lets you start the next iteration of the loop. The following example will not print “Hello” on 5th iteration because the continue command goes back to the beginning of the loop.

int x;
for (x = 1; x <= 10; x++)
{

if(x == 5)
continue;
cout << “Hello\n”;

}

C Control Structure

C language possesses such decision making capabilities and supports the following  statements known as c control structure or decision-making statements.

  • if statement
  • switch statement
  • Conditional operator statement
  • goto statement

if Statement

The if statement is a powerful decision making statement and is used to control
the flow of execution of statements. It is basically a two-way decision statement
and is used in conjunction with an expression.

if Statement Syntax

if (conditional)

{

block of statements executed if conditional is true;

}
else

{

block of statements if condition false;

}

if Statement Example

main()
{

int x=5

if (x > 1)
{

x=x+10;

}

printf(“%d”, x);

}

if…else statement

The if….else statement is an extension of the simple if statement. The general form is

if (condition)
{

True-block statement(s)

}
else
{

False-block statement(s)

}

If the condition is true, then the true-block statement(s), immediately following the if
statement are executed; otherwise the false-block statement(s) are executed.

void main(void)
{

int a, b;
char ch;

printf(“Choice:\n”);
printf(“(A) Add, (S) Subtract, (M) Multiply, or (D) Divide?\n”);
ch = getchar();
printf(“\n”);

printf(“Enter a: “);
scanf(“%d”, &a);
printf(“Enter b: “);
scanf(“%d”, &b);

if(ch==’A’) printf(“%d”, a+b);
else if(ch==’S’) printf(“%d”, a-b);
else if(ch==’M’) printf(“%d”, a*b);
else if(ch==’D’ && b!=0) printf(“%d”, a/b);

}

if-else-if statement

void main(void)
{

int numb;

printf(“Type any Number : “);
scanf(“%d”, &numb);

if(numb > 0) {

printf(“%d is the positive number”, numb);

} else if(numb < 0) {

printf(“%d is the Negative number”, numb);

} else {

printf(“%d is zero”,numb);

}

}

Switch Statement

The switch and case statements help control complex conditional and branching operations.
The switch statement transfers control to a statement within its body.

Switch Statement Syntax

switch (expression) {

case item:

statements;
break;

case item:

statements;
break;

case item:

statements;
break;

default:

statement;
break;

}

Switch Statement Example

main(){

int numb;
printf(“Type any Number”);
scanf(“%d”,&numb);

switch(numb %2)
{

case 0 :

printf(“the number %d is even \n”, numb);
break;

case 1 :

printf(“the number %d is odd \n”, numb);
break;

}

}

Ternary condition

The ? (ternary condition) operator is a more efficient form for expressing simple if
statements. It has the following form:

expression1 ? expression2: expression3

Ternary condition Example

res = (a>b) ? a : b;

if a is greater than b than res has the value a else the res has value b.

break statement

break statement is used to exit from a loop or a switch, control passing to the first
statement beyond the loop or a switch.

With loops, break can be used to force an early exit from the loop, or to implement a
loop with a test to exit in the middle of the loop body. A break within a loop should
always be protected within an if statement which provides the test to control the exit condition.

break statement example

For(i=0;i<=10;i++)

{

if(i==5){

break;

}

printf(“\n%d”,i);

}

Output:

0
1
2
3
4

continue statement

continue is similar to the break statement but it only works within loops where its effect
is to force an immediate jump to the loop control statement.

Like a break, continue should be protected by an if statement.

continue statement example

for(i=0;i<10;i++)

{

if(i==5){

continue;

}

printf(“\n%d”,i);

}

Output:

0
1
2
3
4
6
7
8
9

The goto statement

The goto is a unconditional branching statement used to transfer control of the program from one statement to another.

One must ensure not to use too much of goto statement in their program because its
functionality is limited. It is only recommended as a last resort if structured solutions are
much more complicated.

Java Conditional Statements

Conditional statements are used to evaluate one or more logical conditions along with a block of statements to be executed if the condition return true and optionally another block of statements to be executed if the logical condition return false. There are following conditional statements in java.

If Statement

The if statement is used to execute a block of code only if the applied condition (Boolean expression) return true.

Syntax:

if (condition) {

statements to be executed

}

The default and break statements are optional. Also there can be any number of case blocks in switch statement.

If – Else statement

The if – else statement consist of one if statement with a block of code to be executed if the condition return true and another block of code under else clause which is executed if the condition return false.

Syntax:

if (condition) {

statements to be executed if condition return true

} else {

statements to be executed if condition return false

}

If- Else – If statement

This is a nested if – else statement. It consist of multiple if conditions and an else statement. Each if condition has a block of code which is execute when the condition return true.

Syntax:

if (condition 1) {

statements to be executed if condition 1 return true

} else if(condition 2){

statements to be executed if condition 2 return true

} else {

statements to be executed if all condition return false

}

Switch Statement

The switch statement is used to test a variable for equality against a list of values. The switch statement is enclosed in a switch block and one or more case and/or default labels. The switch statement evaluate each case and execute the appropriate case block.

Syntax:

switch(expression){

case value1 :

statements to be executed

break;

case value2 :

statements to be executed

break;

case value3 :

statements to be executed

break;

default :

statements to be executed

}

The default and break statements are optional also there can be any number of case blocks in switch statement.