0% found this document useful (0 votes)
102 views

Switch Statement IN C/C++: by Quratulain Naqvi (Paki Tech)

The document discusses the switch statement in C/C++. It provides the syntax of a switch statement and important points about its usage. A switch statement allows a program to evaluate an expression and branch to different parts of code based on the expression's value. It provides an alternative to long if/else chains. The syntax includes a switch variable, cases for possible values, and an optional default. Break statements determine where control flow jumps after each case. Nesting switch statements is possible but discouraged. An example program demonstrates a simple switch statement.

Uploaded by

Quratulain Naqvi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
102 views

Switch Statement IN C/C++: by Quratulain Naqvi (Paki Tech)

The document discusses the switch statement in C/C++. It provides the syntax of a switch statement and important points about its usage. A switch statement allows a program to evaluate an expression and branch to different parts of code based on the expression's value. It provides an alternative to long if/else chains. The syntax includes a switch variable, cases for possible values, and an optional default. Break statements determine where control flow jumps after each case. Nesting switch statements is possible but discouraged. An example program demonstrates a simple switch statement.

Uploaded by

Quratulain Naqvi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

SWITCH STATEMENT

IN C/C++
By Quratulain Naqvi (Paki Tech)
Switch Statement
(by Paki Tech)

■ Switch case statements are a substitute for long if statements that compare a
variable to several integral values.
■ The switch statement is a multiway branch statement. It provides an easy way to
dispatch execution to different parts of code based on the value of the expression.
■ Switch is a control statement that allows a value to change control of execution.
Switch Statement
(by Paki Tech)

■ Syntax:
switch (n)
{
case 1: // code to be executed if n = 1;
break;
case 2: // code to be executed if n = 2;
break;
default: // code to be executed if n doesn't match any cases
}
Important points about Switch Statement
(by Paki Tech)

a. The expression provided in the switch should result in a constant value otherwise it
would not be valid.
■ Valid expressions for switch:
// Constant expressions allowed switch(1+2+23) switch(1*2+3%4)

■ Invalid switch expressions for switch:


// Variable expression not allowed
switch(ab+cd)
switch(a+b+c)
Important points about Switch Statement
(by Paki Tech)

1. Duplicate case values are not allowed.


2. The default statement is optional. Even if the switch case statement do not have a
default statement,it would run without any problem.
3. The break statement is used inside the switch to terminate a statement sequence.
When a break statement is reached, the switch terminates, and the flow of control
jumps to the next line following the switch statement.
4. The break statement is optional. If omitted, execution will continue on into the next
case. The flow of control will fall through to subsequent cases until a break is reached.
5. Nesting of switch statements are allowed, which means you can have switch statements
inside another switch. However nested switch statements should be avoided as it
makes program more complex and less readable.
Switch Statement
(by Paki Tech)

■ Flowchart:
Switch Statement
(by Paki Tech)

// Following is a simple program to demonstrate


// syntax of switch.
#include <stdio.h>
int main()
{
int x = 2;
switch (x)
{
case 1: printf("Choice is 1");
break;
case 2: printf("Choice is 2");
break;
case 3: printf("Choice is 3");
break;
default: printf("Choice other than 1, 2 and 3");
break;
} Output:
return 0;
} Choice is 2
THANK YOU FOR
WATCHING
Please subscribe to my channel (Paki Tech), Hit the like
button, turn the notification bell on and comment down what
you want to learn next.. 
Please share my channel too…

You might also like