2.4 Conditional and Control Statements in JavaScrript-1
2.4 Conditional and Control Statements in JavaScrript-1
in JavaScript
U23CS381 - ADD Session by
Dr. S. Sampath Kumar, AP/CSE
Agenda
Introduction
Condition Statement
Ternary Operator
Control Statement
Jump Statements
Course Outcomes:
CO. Outcome K Level
(Apply) Understand and apply JavaScript concepts for dynamic web page
CO2 K3
design
CO3 (Apply) Understand and apply shell commands and GIT workflow K2
Challenge time:
➢ Different types of Operators supported by JS
➢ Snake Case
➢ Ternary Operator
➢ Typeof()
Introduction:
➢ The program can decide which statements to execute based
on a condition.
➢ Conditional statements are used through the various
programming languages to instruct the computer on making
decisions when given some conditions.
➢ These decisions are made if the pre-stated conditions are
either true or false, depending on the functions the programmer
has in mind.
Control Statements:
➢ The control statements are used to control the order of execution
of the statements.
➢ The two significant types of control statements available in JS:
❖ the conditional or decision-making statements and
❖ the repetitive statements
➢ Decision-making Statements: if, if-else, switch
➢ Repetitive statements: for, while, do-while
Challenge time:
List
all relational operators.
Assuming that x = 1, guess the result of the following Boolean
expressions:
➢ (x > 0)
➢ (x < 0)
➢ (x != 0)
➢ (x >= 0)
➢ (x != 1)
Syntax:
//nested if-else statement
if(condition1){
if(condition){
//code
}
else{
//code
}
}
Conditional and Control Statements in JavaScript
15 09-11-2023
Syntax:
if(condition 1) {
statement 1; //executes when condition 1 is true
}
else if(condition 2) {
statement 2; //executes when condition 2 is true
}
else {
statement 3;
//executes when all the conditions are false
}
Conditional and Control Statements in JavaScript
17 09-11-2023
Ternary Operator:
➢ It is a special operator which has three operands.
➢ In JavaScript, there is only one such operator i.e. Conditional
Operator ( ?: ).
➢ This operator is an alternative for an if-else statement.
➢ It helps you to write any 'if-else' or 'conditional' blocks in a very
crisp way and also makes our code look clean and simpler.
➢ It is commonly referred to as the inline-if or ternary-if operator.
Ternary Operator:
Syntax:
switch(expression)
{
case value1:
//code to be executed;
break;
case value2:
//code to be executed;
break;
......
default:
//code to be executed if none of the cases matched
} Conditional and Control Statements in JavaScript
21 09-11-2023
Challenge time:
What is a conditional statement?
What happens if you omit the "break" statement within a case?
Syntax:
while(condition){
//looping statements
}
Syntax:
do
{
//statements
} while (condition);
Challenge time:
difference between "for," "while," and "do-while" loops
Entry Controlled Loop
Exit Controlled Loop
➢ Syntax:
jump-statement;
break;
➢ Syntax:
Label:
{ // code block
}
Conditional and Control Statements in JavaScript
37 09-11-2023
jump-statement;
continue;