SlideShare a Scribd company logo
Course Code: CSE 202
Course Title: Computer Programming Lab
Lecture 11: Switch, break and continue
statements
Course Teacher: Saurav Barua (SB)
Assistant Professor, Dept. of CE, DIU
Phone: +88-01715334075
Email: saurav.ce@diu.edu.bd
Contents
2
Alternatives
between switch
and if-else
Continue
statement
Break
statement
Comparison
continue and
break
Switch
statement
.
Alternatives among
for, while and do-
while loops
switch statement
3
 The switch statement is used to perform different actions based
on different conditions.
This is how it works:
 The switch expression is evaluated once.
 The value of the expression is compared with the values of
each case.
 If there is a match, the associated block of code is executed.
 If there is no match, the default code block is executed.
switch statement
4
Syntax:
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
continue statement
5
 The continue statement "jumps over" one iteration in the loop.
Syntax:
continue;
break statement
6
 The break statement "jumps out" of a loop.
Syntax:
break;
Comparison between continue and break statement
7
Program with continue statement
 Program to count 1 to 5, discontinue
at 4 and display in the console.
Program with break statement
 Program to count 1 to 5, skip 4 and
display in the console.
for(i=1; i<6; i++){
if(i== 4)
continue;
console.log(i);
}
/*Output:
1
2
3
5
*/
for(i=1; i<6; i++){
if(i== 4)
break;
console.log(i);
}
/*Output:
1
2
3
*/
If-else / switch alternatives
8
let score = 65;
function result(){
if(score>=90)
grade = "A";
else if(score>=80 && score<90)
grade = "B";
else if(score>=70 && score<80)
grade = "C";
else
grade = "F";
return grade;
}
outputResult = result();
console.log(outputResult);
Program with if- else if- else statement
 Program to calculate grade of a student from his
test score and display in the console.
Program with switch statement
let score = 73;
function result(){
switch(true){
case score>=90:
grade = "A";
break;
case score>=80:
grade = "B";
break;
case score>=70:
grade = "C";
break;
default:
grade = "F";
}
return grade;
}
outputResult = result();
console.log(outputResult);
for / while/ do-while alternatives
9
Program for loop
 Program to count 5 to 1 numbers and display in the console.
Program with while loop Program with do-while loop
for(let i = 5; i>0; i--){
console.log(i);
}
let i = 5;
while(i>0){
console.log(i);
i--
}
let i = 5;
do{
console.log(i)
i--;
}while(i>0);
Use of continue statement
10
Case study: The SPT N-value is the number of blows required to drive a drill rod
and soil sampler 12 inches into the ground during a standard penetration test
(SPT). The N-value is the sum of the number of blows required for the second and
third 6-inch intervals.
Example problem:
Program to calculate N-value of a SPT soil test where number of blows = [12, 18,
21] and display result in the browser.
const blows = [12, 18, 21];
let nVal = 0;
for (let i = 0; i<blows.length; i++){
if(blows[i] == blows[0])
continue;
else
nVal += blows[i];
}
console.log(nVal)
// output: 39
Worked out example
11
Example 11: Program to classify data based on specified range using switch
statement. (Classify water sample based on degree of hardness (mg/l as CaCO3)
and display result in the browser using switch, where <50 is soft, 50-150 is
moderately hard, 150-300 is hard and >300 is very hard.)
Google drive link:
https://drive.google.com/drive/folders/1DNqm-
2rFkuRJXg5Cb6PMa9fgd82mE-pD?usp=drive_link
Git-hub link:
https://github.com/sauravbarua02/waterHardnessSwitch
Interface
12
html codes
13
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, initial-
scale=1.0">
<title>Hardness of Water</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container" id="container">
</div>
<script src="app.js"></script>
</body>
</html>
css codes
14
body{
margin: 0;
padding: 0;
height: 100%;
background: url("images.jpg");
background-repeat: no repeat;
background-size: cover;
background-attachment: fixed;
background-position: center;
}
.container{
background-color: rgba(148, 60, 60, 0.1);
display: flex;
justify-content: center;
align-items: center;
height: 350px;
width: 350px;
margin: 30px auto;
padding: 20px;
border-radius: 10px;
border: 1px solid rgba(255, 255,255, 0.3);
box-shadow: 3px 3px 3px rgba(0,0,0,0.1);
font-size: larger;
backdrop-filter: blur(5px);
}
JS codes
15
const containerEl =
document.getElementById("container");
let hardnessValue = 160;
function check(){
switch(true){
case hardnessValue>=300:
result = "Very hard water";
break;
case hardnessValue>=150:
result = "Hard water";
break;
case hardnessValue>=50:
result = "Moderately hard water";
break;
case hardnessValue<50:
result = "Soft water";
break;
default:
result = "Not a number";
}
return result;
}
function display(){
let output = check();
containerEl.innerText = "The sample is
"+output;
}
display();
Class tasks
16
Task 11.1: Program to classify soil sample based on plasticity index (%) and display result in
the browser using switch, where 0 is non-plastic, <7 is low-plastic, 7-17 is medium-plastic and
>17 is highly-plastic.
Task 11.2: Program to classify aggregate sample based on aggregate impact value (%) and
display result in the browser using switch, where <10% is exceptionally strong, 10-20% is
strong, plastic, 20-30% is acceptable for road surfaces and >30% is weak for road surfaces.
Task 11.3: Program to classify column design based on D/C ratio obtained from ETABS analysis
and display result in the browser using switch, where <0.7 is not cost effective design, 0.7-1.0
is ideal design and >1.0 is not safe design.
Task 11.4: Program to classify open channel flow based on Raynold’s number and display
result in the browser using switch, where <2000 is laminar flow, 2000-4000 is unsteady flow
and >4000 is turbulent flow.
Task 11.5: Program to classify fresh natural water sample based on TDS (mg/l) and display
result in the browser using switch, where <20 is low, 20-120 is average and 120-1000 is high
End of the Lecture
17

More Related Content

PDF
L4, Conditional statement, CSE 202 JavaScript
PPTX
Switch case and looping
DOC
Course Breakup - C Porgramming Language
DOC
Course Break - C Language
DOC
Student copybca sem1-c
PPTX
Switch case and looping
PPTX
Final requirement for programming-Bonifacio, Mary Clemence
PPT
Project
L4, Conditional statement, CSE 202 JavaScript
Switch case and looping
Course Breakup - C Porgramming Language
Course Break - C Language
Student copybca sem1-c
Switch case and looping
Final requirement for programming-Bonifacio, Mary Clemence
Project

Similar to L11, Switch, break and continue statements, CSE 202, BN11.pdf (20)

PPTX
Mark asoi ppt
PPTX
Margareth lota
PPTX
Switch case and looping statement
PPTX
Yeahhhh the final requirement!!!
ODP
Interview questions slide deck
PPTX
Fundamentals of programming final
PPTX
Fundamentals of prog. by rubferd medina
PPTX
My final requirement
PDF
vtu data structures lab manual bcs304 pdf
PDF
Starting Out with C++: Early Objects 9th Edition by Tony Gaddis (eBook PDF)
PDF
Starting Out with C++: Early Objects 9th Edition by Tony Gaddis (eBook PDF)
PPT
My programming final proj. (1)
PDF
Fundamentals of programming)
PPTX
Switch case and looping new
PDF
Starting Out with C++: Early Objects 9th Edition by Tony Gaddis (eBook PDF)
PPTX
Final project powerpoint template (fndprg) (1)
DOC
Course Breakup Plan- C
PDF
(eBook PDF) Starting Out with C++: Early Objects 9th Edition
PPTX
Switch case looping
PPTX
Fundamentals of programming final santos
Mark asoi ppt
Margareth lota
Switch case and looping statement
Yeahhhh the final requirement!!!
Interview questions slide deck
Fundamentals of programming final
Fundamentals of prog. by rubferd medina
My final requirement
vtu data structures lab manual bcs304 pdf
Starting Out with C++: Early Objects 9th Edition by Tony Gaddis (eBook PDF)
Starting Out with C++: Early Objects 9th Edition by Tony Gaddis (eBook PDF)
My programming final proj. (1)
Fundamentals of programming)
Switch case and looping new
Starting Out with C++: Early Objects 9th Edition by Tony Gaddis (eBook PDF)
Final project powerpoint template (fndprg) (1)
Course Breakup Plan- C
(eBook PDF) Starting Out with C++: Early Objects 9th Edition
Switch case looping
Fundamentals of programming final santos
Ad

More from SauravBarua11 (20)

PDF
L11, Project Survey , Spring 24, lecture notes, SB.pdf
PDF
L5, Computation of Area, Spring 24, SB.pdf
PDF
L4, Contour, Spring 24, lecture notes, SB.pdf
PDF
L3, Traverse Survey, Spring 24, lecture notes, SB.pdf
PDF
L8, Tacheometry survey, Spring 24, SB.pdf
PDF
L10, Astronomical surveying, Spring 24, SB.pdf
PDF
L6, Computation of Volume, Spring 24, SB.pdf
PDF
L9, photogrammetric survey, Spring 24, SB.pdf
PDF
L2, Level surveying, Spring 24,class notes, SB.pdf
PDF
Confusion matrix in Transportation Engineering.pdf
PDF
Ordinary least square (OLS) and MLE in Transportation Engineering.pdf
PDF
L5, Loop and iteration, CSE 202, BN11.pdf
PDF
L2. Function in JS, CSE 202, BN11.p1df documents
PDF
L6, Array in JS, CSE 202, BN11.pdf JavaScript
PDF
L3. Operators in JS, CSE 202, BN11.pdf JavaScript
PDF
L1. Introduction, CSE 202, BN11.pdf JavaScript
PDF
L7. Object in JS, CSE 202, BN11.pdf JavaScript
PDF
L9. Math object in JS, CSE 202, BN11.pdf
PDF
L10. Math.random method in JS, CSE 202, BN11.pdf
PDF
L8. Constructor and method in JS, CSE 202, BN11.pdf
L11, Project Survey , Spring 24, lecture notes, SB.pdf
L5, Computation of Area, Spring 24, SB.pdf
L4, Contour, Spring 24, lecture notes, SB.pdf
L3, Traverse Survey, Spring 24, lecture notes, SB.pdf
L8, Tacheometry survey, Spring 24, SB.pdf
L10, Astronomical surveying, Spring 24, SB.pdf
L6, Computation of Volume, Spring 24, SB.pdf
L9, photogrammetric survey, Spring 24, SB.pdf
L2, Level surveying, Spring 24,class notes, SB.pdf
Confusion matrix in Transportation Engineering.pdf
Ordinary least square (OLS) and MLE in Transportation Engineering.pdf
L5, Loop and iteration, CSE 202, BN11.pdf
L2. Function in JS, CSE 202, BN11.p1df documents
L6, Array in JS, CSE 202, BN11.pdf JavaScript
L3. Operators in JS, CSE 202, BN11.pdf JavaScript
L1. Introduction, CSE 202, BN11.pdf JavaScript
L7. Object in JS, CSE 202, BN11.pdf JavaScript
L9. Math object in JS, CSE 202, BN11.pdf
L10. Math.random method in JS, CSE 202, BN11.pdf
L8. Constructor and method in JS, CSE 202, BN11.pdf
Ad

Recently uploaded (20)

PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PPTX
Essential Infomation Tech presentation.pptx
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
medical staffing services at VALiNTRY
PDF
Understanding Forklifts - TECH EHS Solution
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
System and Network Administration Chapter 2
PPTX
history of c programming in notes for students .pptx
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
How to Migrate SBCGlobal Email to Yahoo Easily
Essential Infomation Tech presentation.pptx
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
medical staffing services at VALiNTRY
Understanding Forklifts - TECH EHS Solution
Upgrade and Innovation Strategies for SAP ERP Customers
wealthsignaloriginal-com-DS-text-... (1).pdf
Odoo POS Development Services by CandidRoot Solutions
Softaken Excel to vCard Converter Software.pdf
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
How Creative Agencies Leverage Project Management Software.pdf
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
System and Network Administration Chapter 2
history of c programming in notes for students .pptx
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PTS Company Brochure 2025 (1).pdf.......
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf

L11, Switch, break and continue statements, CSE 202, BN11.pdf

  • 1. Course Code: CSE 202 Course Title: Computer Programming Lab Lecture 11: Switch, break and continue statements Course Teacher: Saurav Barua (SB) Assistant Professor, Dept. of CE, DIU Phone: +88-01715334075 Email: saurav.ce@diu.edu.bd
  • 2. Contents 2 Alternatives between switch and if-else Continue statement Break statement Comparison continue and break Switch statement . Alternatives among for, while and do- while loops
  • 3. switch statement 3  The switch statement is used to perform different actions based on different conditions. This is how it works:  The switch expression is evaluated once.  The value of the expression is compared with the values of each case.  If there is a match, the associated block of code is executed.  If there is no match, the default code block is executed.
  • 4. switch statement 4 Syntax: switch(expression) { case x: // code block break; case y: // code block break; default: // code block }
  • 5. continue statement 5  The continue statement "jumps over" one iteration in the loop. Syntax: continue;
  • 6. break statement 6  The break statement "jumps out" of a loop. Syntax: break;
  • 7. Comparison between continue and break statement 7 Program with continue statement  Program to count 1 to 5, discontinue at 4 and display in the console. Program with break statement  Program to count 1 to 5, skip 4 and display in the console. for(i=1; i<6; i++){ if(i== 4) continue; console.log(i); } /*Output: 1 2 3 5 */ for(i=1; i<6; i++){ if(i== 4) break; console.log(i); } /*Output: 1 2 3 */
  • 8. If-else / switch alternatives 8 let score = 65; function result(){ if(score>=90) grade = "A"; else if(score>=80 && score<90) grade = "B"; else if(score>=70 && score<80) grade = "C"; else grade = "F"; return grade; } outputResult = result(); console.log(outputResult); Program with if- else if- else statement  Program to calculate grade of a student from his test score and display in the console. Program with switch statement let score = 73; function result(){ switch(true){ case score>=90: grade = "A"; break; case score>=80: grade = "B"; break; case score>=70: grade = "C"; break; default: grade = "F"; } return grade; } outputResult = result(); console.log(outputResult);
  • 9. for / while/ do-while alternatives 9 Program for loop  Program to count 5 to 1 numbers and display in the console. Program with while loop Program with do-while loop for(let i = 5; i>0; i--){ console.log(i); } let i = 5; while(i>0){ console.log(i); i-- } let i = 5; do{ console.log(i) i--; }while(i>0);
  • 10. Use of continue statement 10 Case study: The SPT N-value is the number of blows required to drive a drill rod and soil sampler 12 inches into the ground during a standard penetration test (SPT). The N-value is the sum of the number of blows required for the second and third 6-inch intervals. Example problem: Program to calculate N-value of a SPT soil test where number of blows = [12, 18, 21] and display result in the browser. const blows = [12, 18, 21]; let nVal = 0; for (let i = 0; i<blows.length; i++){ if(blows[i] == blows[0]) continue; else nVal += blows[i]; } console.log(nVal) // output: 39
  • 11. Worked out example 11 Example 11: Program to classify data based on specified range using switch statement. (Classify water sample based on degree of hardness (mg/l as CaCO3) and display result in the browser using switch, where <50 is soft, 50-150 is moderately hard, 150-300 is hard and >300 is very hard.) Google drive link: https://drive.google.com/drive/folders/1DNqm- 2rFkuRJXg5Cb6PMa9fgd82mE-pD?usp=drive_link Git-hub link: https://github.com/sauravbarua02/waterHardnessSwitch
  • 13. html codes 13 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial- scale=1.0"> <title>Hardness of Water</title> <link rel="stylesheet" href="style.css"> </head> <body> <div class="container" id="container"> </div> <script src="app.js"></script> </body> </html>
  • 14. css codes 14 body{ margin: 0; padding: 0; height: 100%; background: url("images.jpg"); background-repeat: no repeat; background-size: cover; background-attachment: fixed; background-position: center; } .container{ background-color: rgba(148, 60, 60, 0.1); display: flex; justify-content: center; align-items: center; height: 350px; width: 350px; margin: 30px auto; padding: 20px; border-radius: 10px; border: 1px solid rgba(255, 255,255, 0.3); box-shadow: 3px 3px 3px rgba(0,0,0,0.1); font-size: larger; backdrop-filter: blur(5px); }
  • 15. JS codes 15 const containerEl = document.getElementById("container"); let hardnessValue = 160; function check(){ switch(true){ case hardnessValue>=300: result = "Very hard water"; break; case hardnessValue>=150: result = "Hard water"; break; case hardnessValue>=50: result = "Moderately hard water"; break; case hardnessValue<50: result = "Soft water"; break; default: result = "Not a number"; } return result; } function display(){ let output = check(); containerEl.innerText = "The sample is "+output; } display();
  • 16. Class tasks 16 Task 11.1: Program to classify soil sample based on plasticity index (%) and display result in the browser using switch, where 0 is non-plastic, <7 is low-plastic, 7-17 is medium-plastic and >17 is highly-plastic. Task 11.2: Program to classify aggregate sample based on aggregate impact value (%) and display result in the browser using switch, where <10% is exceptionally strong, 10-20% is strong, plastic, 20-30% is acceptable for road surfaces and >30% is weak for road surfaces. Task 11.3: Program to classify column design based on D/C ratio obtained from ETABS analysis and display result in the browser using switch, where <0.7 is not cost effective design, 0.7-1.0 is ideal design and >1.0 is not safe design. Task 11.4: Program to classify open channel flow based on Raynold’s number and display result in the browser using switch, where <2000 is laminar flow, 2000-4000 is unsteady flow and >4000 is turbulent flow. Task 11.5: Program to classify fresh natural water sample based on TDS (mg/l) and display result in the browser using switch, where <20 is low, 20-120 is average and 120-1000 is high
  • 17. End of the Lecture 17