TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
Control Structure in JavaScript (1).pptx
1. Control Structure in
JavaScript
By - Manish Kumar, Bansal Shrivastava, Abhinav Yadav
UID - 22MSM40208, 22MSM40112, 22MSM40138
Section - MSD-3
2. What is JavaScript
JavaScript (JS) is a light-weight object-oriented programming language
which is used by several websites for scripting the webpages. It is an
interpreted, full-fledged programming language that enables dynamic
interactivity on websites when applied to an HTML document.
3. Control Structure
Control structure actually controls the flow of execution of a program. Following
are the several control structure supported by javascript.
if … else
switch case
do while loop
while loop
for loop
4. If … else
The if statement is the fundamental control statement that
allows JavaScript to make decisions and execute statements
conditionally.
Syntax
if (expression){
Statement(s) to be executed if expression is true
}
5. Switch case
The basic syntax of the switch statement is to give an expression
to evaluate and several different statements to execute based on
the value of the expression.
Syntax
switch (expression) {
case condition 1: statement(s)
break;
case condition 2: statement(s)
break;
...
case condition n: statement(s)
break;
default: statement(s) }
6. Do while Loop
The do...while loop is similar to the while loop except that the
condition check happens at the end of the loop. This means
that the loop will always be executed at least once, even if the
condition is false.
Syntax
do{
Statement(s) to be executed;
} while (expression);
7. While Loop
The purpose of a while loop is to execute a
statement or code block repeatedly as long as
expression is true. Once expression becomes false,
the loop will be exited.
Syntax
while (expression) {
Statement(s) to be executed if expression is true
}
8. For Loop
The for loop is the most compact form of looping and
includes the following three important parts −
Expression 1 is executed (one time) before the execution of the code block.
Expression 2 defines the condition for executing the code block.
Expression 3 is executed (every time) after the code block has been executed.
Syntax
for (initialization; test condition; iteration statement){
Statement(s) to be executed if test condition is true
}
9. switch (expression) { case condition 1: statement(s) break; case condition 2: statement(s) break; ... case condition n: statement(s) break; default: statement(s) }
Syntax
do{
Statement(s) to be executed;
} while (expression);
while (expression){ Statement(s) to be executed if expression is true }
Syntax
for (initialization; test condition; iteration statement){ Statement(s) to be executed if test condition is true }