CamilleJoyVeniegas
11 views
25 slides
Mar 09, 2025
Slide 1 of 25
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
About This Presentation
Computer and Selection Control Programming
Size: 444.55 KB
Language: en
Added: Mar 09, 2025
Slides: 25 pages
Slide Content
Selection Contol in JavaScript
In this lesson If-else statement If-then-else statement Nested if
If statement It is used to execute specific programming code if the evaluation of a conditional (Boolean) expression returns to a true value. Syntax: if(conditional expression) { statements; }
Using If statement (codes) <html><head> <title> If Statement </title></head> <body> <script> num=prompt("Enter the a number",""); if (num==10) { document.write("You guess the right number!"); } document.writeln("<br>","Click on the refresh button"); document.writeln("to run the program again"); </script> </body></html>
Using if statement (Output)
If-then-else statement When using an if statement, an else clause can be included to run alternate code if the conditional expression evaluated by the if statement returns a false value. Syntax: if(conditional expression) { statements; } else { statements; }
Using If then else statement (codes) <html> <head> <title> If then else statement </title></head> <body> <script> if (confirm("Do you want to continue?")) { document.bgColor ="blue"; document.fgColor="white"; document.writeln("< br >< br >< br >"); document.writeln("<h1>Thank you for entering my site</h1>"); } else { document.bgColor ="black"; document.fgColor="white"; document.writeln("< br >< br >< br >"); document.writeln("<h1>You dont know what you're missing!</h1>"); } </script></body></html>
Nested statement An if statement contained within an if or if else statement. You use nested if and if else statement to perform conditional evaluations in addition to the original conditional evaluation. Syntax: if(conditional expression) { if (conditional expression) { statements; } else { else { statements; }
Using nested if statement (codes) <html> <head> <title> Nested If </title></head> <body> <script> country=prompt(; number=prompt(“Enter a number”,””); if (number > 5); if (number > 10); numberRange =“The number is greater than 10.” else numberRange =“The number is less than 10.” else numberRange =“The number is less than 5.” </script></body></html>
switch statement Controls program flow by executing a specific set of statements, depending on the value of an expression. It compares the value of an expression to a label contained within the switch statement. If the value matches a particular label, then statements associated with the label execute.
switch statement Syntax: switch(expression) { case label_: Statements; Break; Case label_: Statements; Break; … Default: Statements; }
Using Loop in JavaScript
Loop statement It repeatedly executes a statement or a series of statements while a specific condition is TRUE or until a specific condition becomes true. A counter is used to keep of track how many repeated executions a loop statement performs. An accumulator collects and sums the value of a certain variable as the variable is manipulated inside the loop.
While statement The while statement is used for repeating a statement or series of statements as long as the given conditional statement is true. Syntax: while(conditional expression) { }
While statement (codes) <html> <head> <title>While Statement</title></head> <body> <script> alert("This script will display your name on the page 5 times."); name=prompt("Enter your name",""); count=1; while (count<6){ document.writeln("Your name is\t“ + "<U>"+name+"</U>"); document.writeln("< br >");count++;} </script></body></html>
Do..While statement The do…while statement executes a statement,and then repeat the execution as long as a given conditional expression evaluates true. Syntax: do { statements; } while (conditional expression);
Do…While statement (codes) <html> <head> <title>While Statement</title></head> <body> <script> count=1; alert("The value of count is"+ count); do {document.writeln("Number" + count); document.writeln("< br >"); count++; }while (count<11); document.writeln("< hr >"); do { document.writeln("Number" + count); document.writeln("< br >");count++; }while (count<11); </script></body></html>
For.. statement The for statement is used to repeating a statement or series of statements as long as a given conditional expression evaluates to TRUE. The for statement performs essentially the same function as the while statement. Syntax: for ( intialization expression; condition;update statement) { statements; }
FOR statement (codes) <html> <head> <title> For Statement </title></head> <body> <script> alert("This script will display your name on the page 5 times); name=prompt(“Enter your name,””); for(count=1; count<6; count++){ document.writeln (“Your name is” + “<U>” + name + “</U>”); document.writeln("< br >"); } </script></body></html>
WHAT IS AN ARRAY? An Array contains a set of data represented by a single variable name. To create an array, use the following format 1.) array_name = new Array (number of elements) Example: quiz=new Array (4) 2.) array_name = new Array (elements) Example: grades=new Array(85,82,90,89)
WHAT IS AN ARRAY? The numbering of elements within the array starts with the index number of 0. Example: quiz[0], quiz[1], quiz[2], To assign values to the elements: Example: grades[0]=90; grades[1]=83; grades[2]=85;
ARRAY (codes) <html> <head> <title> Array </title></head> <body> <script> nday =new Array(7); nday [0]=prompt("Enter the first day of the week",""); nday [1]=prompt("Enter the second day of the week",""); nday [2]=prompt("Enter the third day of the week",""); nday [3]=prompt("Enter the fourth day of the week",""); nday [4]=prompt("Enter the fifth day of the week",""); nday [5]=prompt("Enter the sixth day of the week",""); nday [6]=prompt("Enter the seventh day of the week",""); document.writeln ("The first day of the week is"+ nday [0]); document.writeln ("< br >"); document.writeln ("The last day of the week is"+ nday [6]); </script></body></html>
Using for statement in an array (codes) <html> <head> <title> Using For statement in an Array </title></head> <body> <script> grade=new Array(4); sum=0; for( i =0; i <4; i ++) { grade[ i ]=prompt("Enter the grade",""); sum+= parseInt (grade[ i ]); } average=sum/4; document.writeln (“List of grades: < br >"); for( i =0; i <4; i ++) { document.writeln (“Quarter"+ (i+1)+ “:”); document.writeln (grade[ i ]+ “< br >”); } document.writeln (“Average:”+ average); </script></body></html>
Break and Continue Break statement is used to exit a switch statement while Continue is used to restart the loop with new iteration
Break statement (codes) <html> <head> <title> Break Statement </title></head> <body> <script> alert("This script will display the number 1-10"); count=1;while(count<11){ if (count>5){ count++; break; } document.writeln ("Number:"+count+"< br >"); count++; } </script></body></html>