SlideShare a Scribd company logo
1 of 32
Download to read offline
Switch Statement
SYNTAX
switch(variable||constant||character)
{
case 1:
statement1;
statement2;
break;
case 2:
statements;
break;
.
.
default:
statements;
}
Example..
void main()
{
char ch=‘x’;
switch(ch)
{
case ’v’:
printf(“In case v ”);
break;
case ’a’:
printf(“In case a ”);
break;
case ’x’:
printf(“In case x ”);
break;
default:
printf(“I am in defaultn”);
}
}
•Switch statement can also contain Expressions
eg switch(i+j*k)
switch(45*j)
•Only constant expressions can be evaluated in
case
eg case 5+9 is correct.
case a+b is incorrect
Limitation of switch
• A float Expression cannot be tested using a switch.
• Multiple cases cannot use same expression values.
eg
switch(var)
{
case 6:
…………..
.
.
case 3*2:
}
It is an Illegal Expression
• case a>2: is an illegal expression
The Loop Control Structure
For Loop
While Loop
Do-While Loop
for Loop
for Loop
• Syntax
void main()
{
for(initialization; condition;increment/decrement)
{
statements;
}
}
for Loop
A Basic program:- Take 5 numbers from user and
find their sum.
void main()
{
int a,i,sum;
printf(“Enter Five number”);
for (i=0;i<5;i++)
{
scanf(“%d”,&a);
sum=sum+a; //
}
printf(“The sum is %d”,sum);
}
for Loop
Some more formats.
• void main()
{
int i;
for(i=1;i<=10;i=i+1)
printf(“%dn”,i);
}
• void main()
{
int i;
for(i=1;i<=10;)
{
printf(“%dn”,i);
i=i+1;
}
}
for Loop
Some more formats.
• void main()
{
int i=1;
for(;i<=10;i=i+1)
printf(“%dn”,i);
}
• void main()
{
int i=1;
for(;i<=10;)
{
printf(“%dn”,i);
i=i+1;
}
}
for Loop
Some more formats.
• void main()
{
int i;
for(i=0;i++<10;)
printf(“%dn”,i);
}
• void main()
{
int i;
for(i=0;++i<=10;)
{
printf(“%dn”,i);
}
}
Nested for loop
• 1*1
• 1*2
• 2*1
• 2*2
• 3*1
• 3*2
Nested for loop
void main()
{
int i,j;
for(i=1;i<4;i++)
{
for(j=1;j<=2;j++)
{
printf(“%dn”,i*j)
}
}
Multiple Initializations in for loop
• for(i=1,j=2;j<=10;j++,i++)
Break Statement
Break Statement
• main()
{
int num,i;
printf(“Enter a number”);
scanf(“%d”,&num);
i=2;
while(i<=num-1)
{
if(num%i==0)
{
printf(“Not a prime number”);
break;
}
i++;
}
if(i==num)
printf(“Prime number”);
}
Continue statement
void main()
{
int x;
for ( x = 1; x <= 10; x++ ) {
if ( x == 5 )
continue; /* skip remaining code in loop only if x == 5 */
printf( "%d ", x );
}
printf( "nUsed continue to skip printing the value 5n" );
}
Output
Predict the output:
void main()
{
float a=3;
switch(a)
{
case 1:
printf(“Value is 1”);
break;
case 2:
printf(“Value is 2”);
break;
case 3:
printf(“Value is 3”);
break;
default:
printf(“ERROR!!”);
}
}
Predict the output
void main(){
int x=0,y=0,i,j;
for(i=0;i<2;i++);
for(j=1;j<3;j++)
{
x++;
}
y++;
printf("x: %d, y: %d",x,y);
getch();
}
Predict the output..
• void main(){
• int ch='a'+'b';
• printf("%cn",ch);
• switch(ch)
• {
• case 'a':
• case 'b':
• printf("u entered bn");
• case 'A':
• printf("u entered An");
• case 'b'+'a':
• printf("u entered b and an");
• default:
• printf("Reached default");
• }
• }
Output???
• void main()
{
char ch='a';
printf("%d",ch);
}
Output????
• 97
void main()
{
int ch=3;
printf(“%c”,ch);
}
Ans ♥
Output????
void main()
{
char c=1;
switch(c)
{
case '1':
printf("In '1'");
break;
case 1||2:
printf(" In 1 or 2");
break;
default:
printf("Not found");
}
}
• ANS
• In 1 or 2
Output???
void main()
{
int suite=1;
switch(suite);
{
case 0;
printf("Clubn");
case 1;
printf("Diamondn");
}
}
Output
Output????
void main()
{
switch('A'+1)
{
case 66:
printf("In 66");
break;
case 1+'A':
printf("In same") ;
break;
case 'B':
printf("In B") ;
break;
default:
printf("In default");
break;
}
}

More Related Content

What's hot

C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptx
C-Programming  C LIBRARIES AND USER DEFINED LIBRARIES.pptxC-Programming  C LIBRARIES AND USER DEFINED LIBRARIES.pptx
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptxSKUP1
 
Understanding java streams
Understanding java streamsUnderstanding java streams
Understanding java streamsShahjahan Samoon
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in javaPratik Soares
 
Type casting in java
Type casting in javaType casting in java
Type casting in javaFarooq Baloch
 
Object Oriented Programming Concepts using Java
Object Oriented Programming Concepts using JavaObject Oriented Programming Concepts using Java
Object Oriented Programming Concepts using JavaGlenn Guden
 
Java ppt
Java pptJava ppt
Java ppt044249
 
Python 101: Python for Absolute Beginners (PyTexas 2014)
Python 101: Python for Absolute Beginners (PyTexas 2014)Python 101: Python for Absolute Beginners (PyTexas 2014)
Python 101: Python for Absolute Beginners (PyTexas 2014)Paige Bailey
 
Java Threads and Concurrency
Java Threads and ConcurrencyJava Threads and Concurrency
Java Threads and ConcurrencySunil OS
 
Java: Regular Expression
Java: Regular ExpressionJava: Regular Expression
Java: Regular ExpressionMasudul Haque
 
Friends function and_classes
Friends function and_classesFriends function and_classes
Friends function and_classesasadsardar
 
Java exception handling
Java exception handlingJava exception handling
Java exception handlingBHUVIJAYAVELU
 
C programming Lab 1
C programming Lab 1C programming Lab 1
C programming Lab 1Zaibi Gondal
 

What's hot (20)

Type Casting Operator
Type Casting OperatorType Casting Operator
Type Casting Operator
 
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptx
C-Programming  C LIBRARIES AND USER DEFINED LIBRARIES.pptxC-Programming  C LIBRARIES AND USER DEFINED LIBRARIES.pptx
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptx
 
Iostream in c++
Iostream in c++Iostream in c++
Iostream in c++
 
I/O Streams
I/O StreamsI/O Streams
I/O Streams
 
Understanding java streams
Understanding java streamsUnderstanding java streams
Understanding java streams
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Type casting in java
Type casting in javaType casting in java
Type casting in java
 
Object Oriented Programming Concepts using Java
Object Oriented Programming Concepts using JavaObject Oriented Programming Concepts using Java
Object Oriented Programming Concepts using Java
 
PPS Notes Unit 5.pdf
PPS Notes Unit 5.pdfPPS Notes Unit 5.pdf
PPS Notes Unit 5.pdf
 
Java ppt
Java pptJava ppt
Java ppt
 
Python 101: Python for Absolute Beginners (PyTexas 2014)
Python 101: Python for Absolute Beginners (PyTexas 2014)Python 101: Python for Absolute Beginners (PyTexas 2014)
Python 101: Python for Absolute Beginners (PyTexas 2014)
 
Java Threads and Concurrency
Java Threads and ConcurrencyJava Threads and Concurrency
Java Threads and Concurrency
 
Storage classes in C
Storage classes in CStorage classes in C
Storage classes in C
 
Java: Regular Expression
Java: Regular ExpressionJava: Regular Expression
Java: Regular Expression
 
Friends function and_classes
Friends function and_classesFriends function and_classes
Friends function and_classes
 
Java literals
Java literalsJava literals
Java literals
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
User defined data types.pptx
User defined data types.pptxUser defined data types.pptx
User defined data types.pptx
 
Recursive functions in C
Recursive functions in CRecursive functions in C
Recursive functions in C
 
C programming Lab 1
C programming Lab 1C programming Lab 1
C programming Lab 1
 

Viewers also liked (20)

Loops in C Programming
Loops in C ProgrammingLoops in C Programming
Loops in C Programming
 
For Loop
For LoopFor Loop
For Loop
 
PRESENTATION ON FOR LOOP
PRESENTATION  ON FOR LOOPPRESENTATION  ON FOR LOOP
PRESENTATION ON FOR LOOP
 
Loops in c
Loops in cLoops in c
Loops in c
 
Chapter 05 looping
Chapter 05   loopingChapter 05   looping
Chapter 05 looping
 
Loops in C
Loops in CLoops in C
Loops in C
 
Switch case and looping statement
Switch case and looping statementSwitch case and looping statement
Switch case and looping statement
 
Do...while loop structure
Do...while loop structureDo...while loop structure
Do...while loop structure
 
Looping statement
Looping statementLooping statement
Looping statement
 
Looping and switch cases
Looping and switch casesLooping and switch cases
Looping and switch cases
 
The Loops
The LoopsThe Loops
The Loops
 
10. switch case
10. switch case10. switch case
10. switch case
 
Switch Case in C Programming
Switch Case in C ProgrammingSwitch Case in C Programming
Switch Case in C Programming
 
Do While and While Loop
Do While and While LoopDo While and While Loop
Do While and While Loop
 
Switch case and looping
Switch case and loopingSwitch case and looping
Switch case and looping
 
While , For , Do-While Loop
While , For , Do-While LoopWhile , For , Do-While Loop
While , For , Do-While Loop
 
Presentation on nesting of loops
Presentation on nesting of loopsPresentation on nesting of loops
Presentation on nesting of loops
 
C++ loop
C++ loop C++ loop
C++ loop
 
Loops
LoopsLoops
Loops
 
Loops c++
Loops c++Loops c++
Loops c++
 

Similar to Switch Statement Guide

Similar to Switch Statement Guide (20)

C Unit-2.ppt
C Unit-2.pptC Unit-2.ppt
C Unit-2.ppt
 
if,loop,switch
if,loop,switchif,loop,switch
if,loop,switch
 
Control flow in c
Control flow in cControl flow in c
Control flow in c
 
12 lec 12 loop
12 lec 12 loop 12 lec 12 loop
12 lec 12 loop
 
Session 3
Session 3Session 3
Session 3
 
3. chapter ii
3. chapter ii3. chapter ii
3. chapter ii
 
6 c control statements branching &amp; jumping
6 c control statements branching &amp; jumping6 c control statements branching &amp; jumping
6 c control statements branching &amp; jumping
 
unit 2-Control Structures.pptx
unit 2-Control Structures.pptxunit 2-Control Structures.pptx
unit 2-Control Structures.pptx
 
C tutorial
C tutorialC tutorial
C tutorial
 
c++ Lecture 4
c++ Lecture 4c++ Lecture 4
c++ Lecture 4
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
1 introduction to c program
1 introduction to c program1 introduction to c program
1 introduction to c program
 
Looping
LoopingLooping
Looping
 
3 flow
3 flow3 flow
3 flow
 
3 flow
3 flow3 flow
3 flow
 
Lec 10
Lec 10Lec 10
Lec 10
 
Chap7_b Control Statement Looping (3).pptx
Chap7_b Control Statement Looping (3).pptxChap7_b Control Statement Looping (3).pptx
Chap7_b Control Statement Looping (3).pptx
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
 
Unit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in CUnit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in C
 
Dti2143 chap 4 control statement part 2
Dti2143 chap 4 control statement part 2Dti2143 chap 4 control statement part 2
Dti2143 chap 4 control statement part 2
 

More from Sukrit Gupta

The n Queen Problem
The n Queen ProblemThe n Queen Problem
The n Queen ProblemSukrit Gupta
 
Future Technologies - Integral Cord
Future Technologies - Integral CordFuture Technologies - Integral Cord
Future Technologies - Integral CordSukrit Gupta
 
Harmful Effect Of Computers On Environment - EWASTE
Harmful Effect Of Computers On Environment - EWASTE Harmful Effect Of Computers On Environment - EWASTE
Harmful Effect Of Computers On Environment - EWASTE Sukrit Gupta
 
Java Script basics and DOM
Java Script basics and DOMJava Script basics and DOM
Java Script basics and DOMSukrit Gupta
 
Cookies and sessions
Cookies and sessionsCookies and sessions
Cookies and sessionsSukrit Gupta
 

More from Sukrit Gupta (9)

The n Queen Problem
The n Queen ProblemThe n Queen Problem
The n Queen Problem
 
Future Technologies - Integral Cord
Future Technologies - Integral CordFuture Technologies - Integral Cord
Future Technologies - Integral Cord
 
Harmful Effect Of Computers On Environment - EWASTE
Harmful Effect Of Computers On Environment - EWASTE Harmful Effect Of Computers On Environment - EWASTE
Harmful Effect Of Computers On Environment - EWASTE
 
MySql
MySqlMySql
MySql
 
Html n CSS
Html n CSSHtml n CSS
Html n CSS
 
Html and css
Html and cssHtml and css
Html and css
 
Java script
Java scriptJava script
Java script
 
Java Script basics and DOM
Java Script basics and DOMJava Script basics and DOM
Java Script basics and DOM
 
Cookies and sessions
Cookies and sessionsCookies and sessions
Cookies and sessions
 

Recently uploaded

Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWQuiz Club NITW
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfPrerana Jadhav
 
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxCLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxAnupam32727
 
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvRicaMaeCastro1
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptshraddhaparab530
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 
ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6Vanessa Camilleri
 
Healthy Minds, Flourishing Lives: A Philosophical Approach to Mental Health a...
Healthy Minds, Flourishing Lives: A Philosophical Approach to Mental Health a...Healthy Minds, Flourishing Lives: A Philosophical Approach to Mental Health a...
Healthy Minds, Flourishing Lives: A Philosophical Approach to Mental Health a...Osopher
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
Sulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesSulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesVijayaLaxmi84
 
ARTERIAL BLOOD GAS ANALYSIS........pptx
ARTERIAL BLOOD  GAS ANALYSIS........pptxARTERIAL BLOOD  GAS ANALYSIS........pptx
ARTERIAL BLOOD GAS ANALYSIS........pptxAneriPatwari
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQuiz Club NITW
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Association for Project Management
 
Comparative Literature in India by Amiya dev.pptx
Comparative Literature in India by Amiya dev.pptxComparative Literature in India by Amiya dev.pptx
Comparative Literature in India by Amiya dev.pptxAvaniJani1
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmStan Meyer
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdfMr Bounab Samir
 
Objectives n learning outcoms - MD 20240404.pptx
Objectives n learning outcoms - MD 20240404.pptxObjectives n learning outcoms - MD 20240404.pptx
Objectives n learning outcoms - MD 20240404.pptxMadhavi Dharankar
 

Recently uploaded (20)

Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITW
 
Chi-Square Test Non Parametric Test Categorical Variable
Chi-Square Test Non Parametric Test Categorical VariableChi-Square Test Non Parametric Test Categorical Variable
Chi-Square Test Non Parametric Test Categorical Variable
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdf
 
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxCLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
 
Mattingly "AI & Prompt Design" - Introduction to Machine Learning"
Mattingly "AI & Prompt Design" - Introduction to Machine Learning"Mattingly "AI & Prompt Design" - Introduction to Machine Learning"
Mattingly "AI & Prompt Design" - Introduction to Machine Learning"
 
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.ppt
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 
ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6
 
Healthy Minds, Flourishing Lives: A Philosophical Approach to Mental Health a...
Healthy Minds, Flourishing Lives: A Philosophical Approach to Mental Health a...Healthy Minds, Flourishing Lives: A Philosophical Approach to Mental Health a...
Healthy Minds, Flourishing Lives: A Philosophical Approach to Mental Health a...
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
Sulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesSulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their uses
 
Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...
Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...
Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...
 
ARTERIAL BLOOD GAS ANALYSIS........pptx
ARTERIAL BLOOD  GAS ANALYSIS........pptxARTERIAL BLOOD  GAS ANALYSIS........pptx
ARTERIAL BLOOD GAS ANALYSIS........pptx
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
 
Comparative Literature in India by Amiya dev.pptx
Comparative Literature in India by Amiya dev.pptxComparative Literature in India by Amiya dev.pptx
Comparative Literature in India by Amiya dev.pptx
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and Film
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdf
 
Objectives n learning outcoms - MD 20240404.pptx
Objectives n learning outcoms - MD 20240404.pptxObjectives n learning outcoms - MD 20240404.pptx
Objectives n learning outcoms - MD 20240404.pptx
 

Switch Statement Guide