Presentation is loading. Please wait.

Presentation is loading. Please wait.

BIT116: Scripting Lecture 6 Part 1

Similar presentations


Presentation on theme: "BIT116: Scripting Lecture 6 Part 1"— Presentation transcript:

1 BIT116: Scripting Lecture 6 Part 1
Instructor: Craig Duckett Thursday, July 23rd, 2015 Conditional Statements & Loops

2 ASSIGNMENT ANNOUNCEMENTS
REMINDER: Submit via StudentTracker. If you haven't already done so, you will need to set up a StudentTracker account in order to upload your Assignment (ZIPPED up in a single file) Assignment 1 due on Lecture 7, NEXT Tuesday, July 28th , by midnight. Assignment 1 Revision due on Lecture 10, Thursday, August 6th , by midnight. Assignment 2 due on Lecture 11, Tuesday, August 11th , by midnight. Assignment 2 Revision due on Lecture 13, Tuesday, August 18th , by midnight. Assignment 3 due on Lecture 14, Thursday, August 20th , by midnight. Assignment 3 Revison due on Lecture 16, Thursday, August 27th , by midnight.

3 Conditional Statements

4 What is a Conditional Statement?
A conditional statement is a statement that you can use to execute a bit of code based on a condition or to do something else if that condition is not met. You can think of a conditional statement as being a little like cause and effect. Here's an example: "If a variable named myMoney is greater than 1000, send an alert that says my finances are OK. Otherwise, send an alert saying I need more money!"

5 The if , if/else , if/else if/else Statements
if statement: use this statement to execute some code once only if a specified condition is true if...else statement: use this statement to execute some code if the condition is true and another code if the condition is false if...else if....else statement: use this statement to select one of many blocks of code to be executed

6 The if Statement (Example)
if (hour < 18) {    show = "Good day"; }

7 The if…else Statement (Example)
if (hour < 18) { show = "Good day"; } else show = "Good evening";

8 The if…else if…else Statement (Example)
if (hour < 12) { show = "Good morning"; } else if ( hour < 18 ) show = "Good afternoon"; else if ( hour < 22 ) show = "Good evening"; else show = "Goodnight!";

9 The switch Statement The switch statement is used to perform different action based on different conditions. Use the switch statement to select one of many blocks of code to be executed.

10 var day = new Date().getDay(); // Note that Sunday = 0, Monday = 1, Tuesday = 2, etc.
switch (day) { case 0: currentDay = "Today is Sunday"; break; case 1: currentDay = "Today is Monday"; case 2: currentDay = "Today is Tuesday"; case 3: currentDay = "Today is Wednesday"; case 4: currentDay = "Today is Thursday"; case 5: currentDay = "TGIF!"; case 6: currentDay = "Today is Saturday"; } Example:

11 var day = new Date().getDay();
switch (day) { case 0: currentDay = "Today is Sunday"; break; case 6: currentDay = "Today is Saturday"; default: // Use default to specify what to do if there's no match    currentDay = "Looking forward to a spectacular Weekend!"; } Example:

12 Loops

13 What is a Loop? A loop is a block of code that allows you to repeat a section of code a certain number of times, perhaps changing certain variable values each time the code is executed. By doing this, you are often able to shorten certain tasks into a few lines of code, rather than writing the same line over and over again within the script and tiring your fingers. Loops are useful because they allow you to repeat lines of code without retyping them or using cut and paste in your text editor. This not only saves you the time and trouble of repeatedly typing the same lines of code, but also avoids typing errors in the repeated lines. You are also able to change one or more variable values each time the browser passes through the loop, which again saves you the time and trouble of typing a line that is only slightly different than the previous line.

14 The while Loop The while loop loops through a block of code as long as a specified condition is true. while(expression is true) {   Statement(s)to be executed } Example:

15 The 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. do{ Statement(s) to be executed; } while (expression is true); Example:

16 The for Loop The for loop is the most compact form of looping and includes the following three important parts: The loop initialization where we initialize our counter to a starting value. The initialization statement is executed before the loop begins. The test statement which will test if the given condition is true or false. If condition is true then code given inside the loop will be executed otherwise loop will come out. The iteration statement where you can increase (increment) or decrease (decrement) your counter. You can put all the three parts in a single line separated by a semicolon. for (initialization; test condition; iteration statement) { Statement(s) to be executed if test condition is true }

17 The break Statement You have already seen the break statement used in an earlier slide of this lecture. It was used to "jump out" of a switch() statement. The break statement can also be used to jump out of a loop. The break statement breaks the loop and continues executing the code after the loop (if any): for (i = 0; i < 10; i++) { if (i == 3) break; } x += "The number is " + i + "<br>";

18 The continue Statement
The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop. This example skips the value of 3: for (i = 0; i < 10; i++) { if (i == 3) continue; } x += "The number is " + i + "<br>";

19 ICE 06 Part 1 Please begin working on the LECTURE 6 In-Class Exercises. When you have completed your ICE, call the Instructor over to verify your work. If you have questions about your work, please call over the Instructor to clarify. Once you have completed your ICEs, you are free to go for the day. If you don't finish your ICEs by the end of class, you will still get full credit for actively doing the work, but it is greatly recommended you finish any outstanding ICEs outside of class.


Download ppt "BIT116: Scripting Lecture 6 Part 1"

Similar presentations


Ads by Google