0% found this document useful (0 votes)
22 views29 pages

JavaScript Control Structures Guide

Uploaded by

zinzin3511
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views29 pages

JavaScript Control Structures Guide

Uploaded by

zinzin3511
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Chapter (3)

Decisions, Loops, and


Functions

Faculty of Information Science 1


Comparison Operators

Faculty of Information Science 2


ch3_examp1
<html xmlns="[Link]
<body>
<script type="text/javascript“>
var degFahren = Number(prompt("Enter the degrees Fahrenheit",32));
var degCent;
degCent = 5/9 * (degFahren - 32);
[Link](degFahren + "\xB0 Fahrenheit is " + degCent +"\xB0 centigrade<BR>");
if (degCent < 0)
{
[Link]("That’s below the freezing point of water");
}
if (degCent == 100)
[Link]("That’s the boiling point of water");
</script>
</body>
</html>

Faculty of Information Science 3


Logical Operators

Faculty of Information Science 4


AND

a truth table of possible evaluations of left-hand sides and right-hand sides and
the result

Faculty of Information Science 5


OR

Faculty of Information Science 6


NOT

Faculty of Information Science 7


Multiple Conditions Inside an if
Statement
if (degCent < 100)
{
if (degCent > 0)
{
[Link](“degCent is between 0 and
100”);
}
}

Faculty of Information Science 8


ch3_examp2: Multiple Conditions
<html xmlns="[Link]
<body>
<script type="text/javascript“>
var myAge = Number(prompt("Enter your age",30));
if (myAge >= 0 && myAge <= 10)
{
[Link]("myAge is between 0 and 10<br>");
}
if ( !(myAge >= 0 && myAge <= 10) )
{
[Link]("myAge is NOT between 0 and 10<br>");
}
if ( myAge >= 80 || myAge <= 10 )
{
[Link]("myAge is 80 or above OR 10 or below<br>");
}
if ( (myAge >= 30 && myAge <= 39) || (myAge >= 80 && myAge <= 89) )
{
[Link]("myAge is between 30 and 39 or myAge is between 80 and 89");
}</script></body></html>
Faculty of Information Science 9
Control Structures
• Branching

• Conditions

 You can combine multiple conditions by and (&&), or (||).

Faculty of Information Science 10


Other Operators
• Ternary operator
a = (b == c)? d : e;
• Multiple-value Branching

Faculty of Information Science 11


ch3_examp3
<html xmlns="[Link]
<body>
<script type="text/javascript">
var secretNumber = prompt("Pick a number between 1 and 5:", "");
secretNumber = parseInt(secretNumber);
switch (secretNumber)
{
case 1: [Link]("Too low!"); break;
case 2: [Link]("Too low!"); break;
case 3: [Link]("You guessed the secret number!"); break;
case 4: [Link]("Too high!"); break;
case 5: [Link]("Too high!"); break;
default: [Link]("You did not enter a number between 1 and 5."); break;
}
[Link]("<BR>Execution continues here");
</script>
</body>
</html> Faculty of Information Science 12
Executing the Same Code for Different Cases
switch (secretNumber)
{
case 1:
case 2:[Link](“Too low!”);break;

case 3:[Link](“You guessed the secret number!”);break;

case 4:
case 5:[Link](“Too high!”);break;

default:[Link](“You did not enter a number between 1 and


5.”);break;
}

Faculty of Information Science 13


Looping
• for loop
for (expression1; expression2; expression3)
statement;

• expression1: Executed once before entering loop


• expression2: Evaluated at the beginning of each
loop iteration. If it evaluates to false, then the
loop will be finished.
• expression3: Executed at the end of each loop
iteration.
Faculty of Information Science 14
Looping
• “for”…”in” loop
for (index in object) {
statement1;
...
}

• while
Pre-evaluation loop
(condition) statement;

 Post-evaluation loop

do statement; while (condition);


Faculty of Information Science 15
For Loop

var myArray=new Array(“Paul”, “Paula”, “Pauline”);


var loopCounter;
for(loopCounter=0;loopCounter<3;loopCounter++)
{
[Link](myArray[loopCounter]);
}

Faculty of Information Science 16


“for”…”in” loop
var elementIndex;
for (elementIndex in myArray)
{
[Link](myArray[elementIndex]);
}
It enables you to loop through each element in the array
without having to know how many elements the array
actually contains.

Faculty of Information Science 17


While Loop

var testVariable = 0;
while (testVariable <= 10)
{
alert(“Test Variable is” + testVariable);
testVariable++;
if (testVariable = 10)
{
alert(“The last loop”);
}
}

Faculty of Information Science 18


do…while loop

var userAge;
do
{
userAge = prompt(“Please enter your age”,””)
}
while (isNaN(userAge) == true);

Faculty of Information Science 19


Break and Continue

Break (terminate a while, for loop or switch)

Continue (restart the while loop)

Faculty of Information Science 20


Using label
• the destination of continue statement by using
label

Faculty of Information Science 21


ch3_examp4
<html xmlns="[Link]
<body>
<script type="text/javascript">
var degFahren = new Array(212, 32, -459.15);
var degCent = new Array();
var loopCounter;
for (loopCounter = 0; loopCounter <= 2; loopCounter++)
{ degCent[loopCounter] = 5/9 * (degFahren[loopCounter] - 32);
}
for (loopCounter = 2; loopCounter >= 0; loopCounter--)
{
[Link]("Value " + loopCounter + " was " + degFahren[loopCounter] + " degrees
Fahrenheit");
[Link](" which is " + degCent[loopCounter] + " degrees centigrade<BR>");
} </script> </body> </html>

Faculty of Information Science 22


Basic Function & Object
• User-defined function
function function_name(arguments) {
statements;
return return_value;
}
• Dialog box function
 alert("message")
 alert("Hello, World!");
 confirm("message")
if (confirm("Are you sure?"))
alert("You said sure!");
 prompt("message", "default value")
var name = prompt("What's your name?", "FirstName LastName");

Faculty of Information Science 23


ch3_examp5
<html xmlns="[Link] <body>
<script type="text/javascript">
function convertToCentigrade(degFahren)
{ var degCent;
degCent = 5/9 * (degFahren - 32);
return degCent;
}
var degFahren = new Array(212, 32, -459.15);
var degCent = new Array();
var loopCounter;
for (loopCounter = 0; loopCounter <= 2; loopCounter++)
{
degCent[loopCounter] = convertToCentigrade(degFahren[loopCounter]);
}
for (loopCounter = 2; loopCounter >= 0; loopCounter--)
{
[Link]("Value " + loopCounter + " was " + degFahren[loopCounter] + " degrees
Fahrenheit");
[Link](" which is " + degCent[loopCounter] + " degrees centigrade<br>");
} </script> </body> </html> Faculty of Information Science 24
 Exercises 
1. A junior programmer comes to you with some code that appears not to work.
Can you spot where he went wrong? Give him a hand and correct the
mistakes.
var userAge = prompt(“Please enter your age”);
if (userAge = 0);
{
alert(“So you’re a baby!”);
}
else if ( userAge < 0 | userAge > 200)
alert(“I think you may be lying about your age”);
else
{
alert(“That’s a good age”);
}

Faculty of Information Science 25


Exercises
2. Using document .write(), write code that displays the results of
the 12 times table. Its output should be the results of the
calculations.
12 * 1 = 12
12 * 2 = 24
12 * 3 = 36
...
12 * 11 = 132
12 * 12 = 144

Faculty of Information Science 26


Exercises
3. Change the code of Question 2 so that it’s a function that takes
as parameters the times table required and the values at which
it should start and end. For example, you might try the four
times table displayed starting with 4 * 4 and ending at 4 * 9.

Faculty of Information Science 27


Exercises
4. Modify the code of Question 3 to request the times table to be
displayed from the user; the code should continue to request
and display times tables until the user enters -1. Additionally,
do a check to make sure that the user is entering a valid
number; if the number is not valid, ask the user to re-enter it.

Faculty of Information Science 28


Faculty of Information Science 29

You might also like