You are on page 1of 16

1

Dr. Navneet Kaur, Lovely Profession


al University

CSE326
Internet Programming Laboratory
Lecture #11 Part 2
Dr. Navneet Kaur
2
Dr. Navneet Kaur, Lovely
Professional University

Outline
 Control statements
 Looping statements
 Popup boxes
3
Dr. Navneet Kaur, Lovely
Professional University

Control statements
 JavaScript control statement is used to control the execution
of a program based on a specific condition.
 If the condition meets then a particular block of action will be
executed otherwise it will execute another block of action
that satisfies that particular condition.
 if statement - The if statement executes a statement if a
specified condition is true.
if (condition)
{
// block of code to be executed if the condition is true
}
4
Dr. Navneet Kaur, Lovely
Professional University

Control statements
 if-else Statement - The if...else statement executes a statement
if a specified condition is true. If the condition is false, another
statement in the optional else clause will be executed.

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
}
5
Dr. Navneet Kaur, Lovely
Professional University

Control statements
 if-else-if Statement - Note that there is no elseif syntax in
JavaScript. However, you can write it with a space between else
and if.
if (x > 50) {
/* do something */
} else if (x > 5) {
/* do something */
} else {
/* do something */
}
6
Dr. Navneet Kaur, Lovely
Professional University

Control statements
 Switch Statement - The switch statement evaluates an expression, matching
the expression's value against a series of case clauses, and executes
statements after the first case clause with a matching value, until a break
statement is encountered.
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
7
Dr. Navneet Kaur, Lovely
Professional University

Looping statements
 Loops offer a quick and easy way to do something
repeatedly.
 for statement - A for loop repeats until a specified condition
evaluates to false.
for (initialization; condition; afterthought)
statement
 while statement - A while statement executes its statements as
long as a specified condition evaluates to true.
while (condition)
statement
8
Dr. Navneet Kaur, Lovely
Professional University

Looping statements
 do...while statement - The do...while statement repeats until a
specified condition evaluates to false.
do
statement
while (condition);
9
Dr. Navneet Kaur, Lovely
Professional University

Control statements (contd.)


 continue- The continue statement terminates execution of the
statements in the current iteration of the current or labeled loop,
and continues execution of the loop with the next iteration.
for(init; condition; update) {
//code
if(condition to continue) {
continue;
}
//code
}
10
Dr. Navneet Kaur, Lovely
Professional University

Control statements (contd.)


while(condition){
//code
if(condition to continue){
continue;
}
//code
}
11
Dr. Navneet Kaur, Lovely
Professional University

Control statements (contd.)


 break - The break statement terminates the current loop or
switch statement and transfers program control to the statement
following the terminated statement.
for(init; condition; update) {
//code
if(condition to break) {
break;
}
//code
}
12
Dr. Navneet Kaur, Lovely
Professional University

Control statements (contd.)


while(condition){
//code
if(condition to break){
break;
}
//code
}
13
Dr. Navneet Kaur, Lovely
Professional University

Popup boxes
 JavaScript provides built-in global functions to display popup
message boxes for different purposes.
 alert():The alert() function displays a message to the user to
display some information to users. This alert box will have
the OK button to close the alert box.
 confirm(message): Use the confirm() function to take the user's
confirmation before starting some task.
 prompt(message, defaultValue): Use the prompt() function to
take the user's input to do further actions.
14
Dr. Navneet Kaur, Lovely
Professional University

References
 https://www.w3schools.com/js/js_if_else.asp
 https://www.w3schools.com/js/js_switch.asp
 https://www.w3schools.com/js/js_break.asp
 https://www.w3schools.com/js/js_loop_for.asp
 https://www.w3schools.com/js/js_loop_while.asp
 https://www.w3schools.com/js/js_popup.asp
15
Dr. Navneet Kaur, Lovely
Professional University

Program link
https://onlinegdb.com/3SnGoKwah
16
Dr. Navneet Kaur, Lovely Profession
al University

Thank you

You might also like