SlideShare a Scribd company logo
Course Code: CSE 202
Course Title: Computer Programming Lab
Lecture 4: Conditional Statement
Course Teacher: Saurav Barua (SB)
Assistant Professor, Dept. of CE, DIU
Phone: +88-01715334075
Email: saurav.ce@diu.edu.bd
Contents
2
parseInt() and
parseFloat()
methods
If statement Else statement
Else if
statement
Conditional
statement
.
Worked out
problems
Conditional statement
3
Conditional statements are used to perform different actions based on different
conditions.
In JavaScript we have the following conditional statements:
 Use if to specify a block of code to be executed, if a specified condition is
true
 Use else to specify a block of code to be executed, if the same condition
is false
 Use else if to specify a new condition to test, if the first condition is false
 Use switch to specify many alternative blocks of code to be executed
if statement
4
Syntax:
if (condition) {
// block of code to be executed
if the condition is true
}
Use the if statement to specify a block of JavaScript code to be
executed if a condition is true.
else statement
5
Syntax:
if (condition) {
// block of code to be executed if the
condition is true
} else {
// block of code to be executed if the
condition is false
}
Use the else statement to specify a block of code to be
executed if the condition is false.
else if statement
6
Syntax:
if (condition1) {
// block of code to be executed if condition1
is true
} else if (condition2) {
// block of code to be executed if the
condition1 is false and condition2 is true
} else {
// block of code to be executed if the
condition1 is false and condition2 is false
}
Use the else if statement to specify a new condition if the first
condition is false.
parseInt() and parseFloat()
7
 The parseInt() function parses a string argument and returns
an integer.
 The parseFloat() function parses a string argument and
returns a floating point number.
Worked out example
8
Example 4: Program to take input from prompt and classify the input data
through JS and display result in the browser. (Program to classify the input data
for turbidity (NTU) of drinking water, where <=10 NTU is low turbid, 11-50 NTU
moderately turbid, 51-100 is high turbid and >100 is very high turbid.
Google drive link:
https://drive.google.com/drive/folders/1e2L2EOjHrX7MSy4yPCsep4i2t0J1
FSkQ?usp=drive_link
Git-hub link: https://github.com/sauravbarua02/turbidityIfElse
Interface
9
html codes
10
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, initial-
scale=1.0">
<title>Water Quality for
turbidity</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container" id="container">
</div>
<script src="app.js"></script>
</body>
</html>
css codes
11
body{
margin: 0px;
background-color: rgba(40,184,83,0.6);
}
.container{
background-color: rgba(40,184,83,0.2);
height: 300px;
width: 300px;
display: flex;
flex-direction: column;
margin: 20px auto;
padding: 10px;
justify-content: center;
align-items: center;
font-size: larger;
box-shadow: 0 0 4px 1px rgba(0,0,0,0.6);
border-radius: 10px;
}
JS codes
12
const containerEl =
document.getElementById("container");
let turbidity = parseInt(prompt("Enter
turbidity of water sample in NTU:"));
function check(){
if(turbidity<=10)
result = "Low turbid";
else if(turbidity>10&&turbidity<=50)
result = "Moderate turbid";
else if(turbidity>50&&turbidity<=100)
result ="High turbid";
else
result = "Very high turbid";
return result;
}
function display(check){
val = check();
containerEl.innerText = " The water sample
is " +val;
}
display(check);
Class tasks
13
Task 4.1: Program to classify the input data for pH of drinking, where <6.5 is acidic
and alkaline, 6.5-8.5 normal range and safe for drinking and >8.5 hard and alkaline.
Task 4.2: Program to classify the input data for grain size (mm) of soil, where <0.002
is clay, 0.002-0.075 is silt, 0.075-0.42 is fine sand, 0.42-2.0 is medium sand, 2.0-4.75
is coarse sand and >4.75 is gravel.
Task 4.3: Program to classify the input data for average speed (mph) of traffic
stream, where >60 is LOS A, 57-60 is LOS B, 54-57 is LOS C, 46-54 is LOS D, 30-46 is
LOS E and <30 is LOS F.
Task 4.4: Program to classify the input data for column slenderness ratio, where <32
is short column, 32-120 is medium column and >120 is long column.
Task 4.5: Program to classify the input data for Froude number of hydraulic jump,
where <=1.0 is no jump, 1-1.7 is undulating jump, 1.7-2.5 is weak jump, 2.5-4.5 is
oscillating jump, 4.5-9.0 is stable jump and >9.0 is strong jump.
End of the Lecture
14

More Related Content

Similar to L4, Conditional statement, CSE 202 JavaScript (20)

PPS
CS101- Introduction to Computing- Lecture 23
Bilal Ahmed
 
PPTX
Introduction to JavaScript
Chris Huie
 
PDF
1 coding101 fewd_lesson1_coding_quickstart 20210105
John Picasso
 
PPT
Javascript
Vishwa Patel
 
DOCX
WD programs descriptions.docx
anjani pavan kumar
 
PDF
Download full ebook of (Ebook) learning javascript 3e by kan instant download...
fonseaderit
 
PDF
Week 5
EasyStudy3
 
PDF
Week 5
EasyStudy3
 
PDF
Processing an introduction to programming 1st Edition Nyhoff
dhjfqrlqur3831
 
PPTX
Introduction to Client-Side Javascript
Julie Iskander
 
PPTX
Learning JavaScript Programming
Hriday Ahmed
 
PPT
Javascript
Sunil Thakur
 
PPTX
JavaScript, VBScript, AJAX, CGI
Aashish Jain
 
PPTX
Web technologies-course 08.pptx
Stefan Oprea
 
PDF
Functional Programming for OO Programmers (part 2)
Calvin Cheng
 
PDF
data Integrative Programming untuk pemula.pdf
loliktry1
 
PPTX
Top Programming Languages to Learn in Coding Classes
SkyHi Tech Academy
 
PPT
Introduction To Programming
cwarren
 
PPSX
Javascript variables and datatypes
Varun C M
 
PDF
SNP STEAM Academy 2017 Class #12
Markus Van Kempen
 
CS101- Introduction to Computing- Lecture 23
Bilal Ahmed
 
Introduction to JavaScript
Chris Huie
 
1 coding101 fewd_lesson1_coding_quickstart 20210105
John Picasso
 
Javascript
Vishwa Patel
 
WD programs descriptions.docx
anjani pavan kumar
 
Download full ebook of (Ebook) learning javascript 3e by kan instant download...
fonseaderit
 
Week 5
EasyStudy3
 
Week 5
EasyStudy3
 
Processing an introduction to programming 1st Edition Nyhoff
dhjfqrlqur3831
 
Introduction to Client-Side Javascript
Julie Iskander
 
Learning JavaScript Programming
Hriday Ahmed
 
Javascript
Sunil Thakur
 
JavaScript, VBScript, AJAX, CGI
Aashish Jain
 
Web technologies-course 08.pptx
Stefan Oprea
 
Functional Programming for OO Programmers (part 2)
Calvin Cheng
 
data Integrative Programming untuk pemula.pdf
loliktry1
 
Top Programming Languages to Learn in Coding Classes
SkyHi Tech Academy
 
Introduction To Programming
cwarren
 
Javascript variables and datatypes
Varun C M
 
SNP STEAM Academy 2017 Class #12
Markus Van Kempen
 

More from SauravBarua11 (20)

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

Recently uploaded (20)

PPTX
Work at Height training for workers .pptx
cecos12
 
PPTX
FSE_LLM4SE1_A Tool for In-depth Analysis of Code Execution Reasoning of Large...
cl144
 
PDF
Python Mini Project: Command-Line Quiz Game for School/College Students
MPREETHI7
 
PPTX
CST413 KTU S7 CSE Machine Learning Clustering K Means Hierarchical Agglomerat...
resming1
 
PDF
Validating a Citizen Observatories enabling Platform by completing a Citizen ...
Diego López-de-Ipiña González-de-Artaza
 
PPTX
How to Un-Obsolete Your Legacy Keypad Design
Epec Engineered Technologies
 
PDF
Rapid Prototyping for XR: Lecture 1 Introduction to Prototyping
Mark Billinghurst
 
PDF
May 2025: Top 10 Read Articles in Data Mining & Knowledge Management Process
IJDKP
 
PDF
Rapid Prototyping for XR: Lecture 4 - High Level Prototyping.
Mark Billinghurst
 
PDF
lesson4-occupationalsafetyandhealthohsstandards-240812020130-1a7246d0.pdf
arvingallosa3
 
PPTX
Mobile database systems 20254545645.pptx
herosh1968
 
PPTX
Precooling and Refrigerated storage.pptx
ThongamSunita
 
PPTX
Tesla-Stock-Analysis-and-Forecast.pptx (1).pptx
moonsony54
 
PPTX
Kel.3_A_Review_on_Internet_of_Things_for_Defense_v3.pptx
Endang Saefullah
 
PDF
Generative AI & Scientific Research : Catalyst for Innovation, Ethics & Impact
AlqualsaDIResearchGr
 
PPSX
OOPS Concepts in Python and Exception Handling
Dr. A. B. Shinde
 
PDF
NFPA 10 - Estandar para extintores de incendios portatiles (ed.22 ENG).pdf
Oscar Orozco
 
PPTX
MATERIAL SCIENCE LECTURE NOTES FOR DIPLOMA STUDENTS
SAMEER VISHWAKARMA
 
PDF
How to Buy Verified CashApp Accounts IN 2025
Buy Verified CashApp Accounts
 
PPTX
Introduction to Python Programming Language
merlinjohnsy
 
Work at Height training for workers .pptx
cecos12
 
FSE_LLM4SE1_A Tool for In-depth Analysis of Code Execution Reasoning of Large...
cl144
 
Python Mini Project: Command-Line Quiz Game for School/College Students
MPREETHI7
 
CST413 KTU S7 CSE Machine Learning Clustering K Means Hierarchical Agglomerat...
resming1
 
Validating a Citizen Observatories enabling Platform by completing a Citizen ...
Diego López-de-Ipiña González-de-Artaza
 
How to Un-Obsolete Your Legacy Keypad Design
Epec Engineered Technologies
 
Rapid Prototyping for XR: Lecture 1 Introduction to Prototyping
Mark Billinghurst
 
May 2025: Top 10 Read Articles in Data Mining & Knowledge Management Process
IJDKP
 
Rapid Prototyping for XR: Lecture 4 - High Level Prototyping.
Mark Billinghurst
 
lesson4-occupationalsafetyandhealthohsstandards-240812020130-1a7246d0.pdf
arvingallosa3
 
Mobile database systems 20254545645.pptx
herosh1968
 
Precooling and Refrigerated storage.pptx
ThongamSunita
 
Tesla-Stock-Analysis-and-Forecast.pptx (1).pptx
moonsony54
 
Kel.3_A_Review_on_Internet_of_Things_for_Defense_v3.pptx
Endang Saefullah
 
Generative AI & Scientific Research : Catalyst for Innovation, Ethics & Impact
AlqualsaDIResearchGr
 
OOPS Concepts in Python and Exception Handling
Dr. A. B. Shinde
 
NFPA 10 - Estandar para extintores de incendios portatiles (ed.22 ENG).pdf
Oscar Orozco
 
MATERIAL SCIENCE LECTURE NOTES FOR DIPLOMA STUDENTS
SAMEER VISHWAKARMA
 
How to Buy Verified CashApp Accounts IN 2025
Buy Verified CashApp Accounts
 
Introduction to Python Programming Language
merlinjohnsy
 
Ad

L4, Conditional statement, CSE 202 JavaScript

  • 1. Course Code: CSE 202 Course Title: Computer Programming Lab Lecture 4: Conditional Statement Course Teacher: Saurav Barua (SB) Assistant Professor, Dept. of CE, DIU Phone: +88-01715334075 Email: [email protected]
  • 2. Contents 2 parseInt() and parseFloat() methods If statement Else statement Else if statement Conditional statement . Worked out problems
  • 3. Conditional statement 3 Conditional statements are used to perform different actions based on different conditions. In JavaScript we have the following conditional statements:  Use if to specify a block of code to be executed, if a specified condition is true  Use else to specify a block of code to be executed, if the same condition is false  Use else if to specify a new condition to test, if the first condition is false  Use switch to specify many alternative blocks of code to be executed
  • 4. if statement 4 Syntax: if (condition) { // block of code to be executed if the condition is true } Use the if statement to specify a block of JavaScript code to be executed if a condition is true.
  • 5. else statement 5 Syntax: if (condition) { // block of code to be executed if the condition is true } else { // block of code to be executed if the condition is false } Use the else statement to specify a block of code to be executed if the condition is false.
  • 6. else if statement 6 Syntax: if (condition1) { // block of code to be executed if condition1 is true } else if (condition2) { // block of code to be executed if the condition1 is false and condition2 is true } else { // block of code to be executed if the condition1 is false and condition2 is false } Use the else if statement to specify a new condition if the first condition is false.
  • 7. parseInt() and parseFloat() 7  The parseInt() function parses a string argument and returns an integer.  The parseFloat() function parses a string argument and returns a floating point number.
  • 8. Worked out example 8 Example 4: Program to take input from prompt and classify the input data through JS and display result in the browser. (Program to classify the input data for turbidity (NTU) of drinking water, where <=10 NTU is low turbid, 11-50 NTU moderately turbid, 51-100 is high turbid and >100 is very high turbid. Google drive link: https://drive.google.com/drive/folders/1e2L2EOjHrX7MSy4yPCsep4i2t0J1 FSkQ?usp=drive_link Git-hub link: https://github.com/sauravbarua02/turbidityIfElse
  • 10. html codes 10 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial- scale=1.0"> <title>Water Quality for turbidity</title> <link rel="stylesheet" href="style.css"> </head> <body> <div class="container" id="container"> </div> <script src="app.js"></script> </body> </html>
  • 11. css codes 11 body{ margin: 0px; background-color: rgba(40,184,83,0.6); } .container{ background-color: rgba(40,184,83,0.2); height: 300px; width: 300px; display: flex; flex-direction: column; margin: 20px auto; padding: 10px; justify-content: center; align-items: center; font-size: larger; box-shadow: 0 0 4px 1px rgba(0,0,0,0.6); border-radius: 10px; }
  • 12. JS codes 12 const containerEl = document.getElementById("container"); let turbidity = parseInt(prompt("Enter turbidity of water sample in NTU:")); function check(){ if(turbidity<=10) result = "Low turbid"; else if(turbidity>10&&turbidity<=50) result = "Moderate turbid"; else if(turbidity>50&&turbidity<=100) result ="High turbid"; else result = "Very high turbid"; return result; } function display(check){ val = check(); containerEl.innerText = " The water sample is " +val; } display(check);
  • 13. Class tasks 13 Task 4.1: Program to classify the input data for pH of drinking, where <6.5 is acidic and alkaline, 6.5-8.5 normal range and safe for drinking and >8.5 hard and alkaline. Task 4.2: Program to classify the input data for grain size (mm) of soil, where <0.002 is clay, 0.002-0.075 is silt, 0.075-0.42 is fine sand, 0.42-2.0 is medium sand, 2.0-4.75 is coarse sand and >4.75 is gravel. Task 4.3: Program to classify the input data for average speed (mph) of traffic stream, where >60 is LOS A, 57-60 is LOS B, 54-57 is LOS C, 46-54 is LOS D, 30-46 is LOS E and <30 is LOS F. Task 4.4: Program to classify the input data for column slenderness ratio, where <32 is short column, 32-120 is medium column and >120 is long column. Task 4.5: Program to classify the input data for Froude number of hydraulic jump, where <=1.0 is no jump, 1-1.7 is undulating jump, 1.7-2.5 is weak jump, 2.5-4.5 is oscillating jump, 4.5-9.0 is stable jump and >9.0 is strong jump.
  • 14. End of the Lecture 14