Presentation is loading. Please wait.

Presentation is loading. Please wait.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Similar presentations


Presentation on theme: "© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved."— Presentation transcript:

1 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
"Digital Media Primer" Yue-Ling Wong, Copyright (c)2016 by Pearson Education, Inc. All rights reserved. © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

2 Chapter 10 Programming Fundamentals with JavaScript
Part 2 Programming Fundamentals - Part B © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

3 In this lecture, you will learn:
what are control structures to read and write: if statements and switch statements © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

4 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
How a Program Runs A program is written as a sequence of statements as instructions. The program executes the instructions sequentially--one instruction after the other, in the order in which they appear in the code. Use control structures to make nonsequential execution of the instructions. © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

5 Types of Control Structures
Loop A set of statements is executed repeatedly until a certain condition is reached Will be covered in Chapter 12 Conditional A set of statements is executed only if some conditions are met if statements and switch statements © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

6 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
if Statements if if...else if...else if Nested if statements © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

7 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
if General Syntax: if (logical expression(s)) { statement(s) } The statements grouped within the curly braces are called the block statements. © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

8 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
if Example: if (score > 60) { grade = "pass"; } © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

9 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
if If there is only one statement to be executed, the curly braces are optional. Examples: if (score > 60) grade = "pass"; The statement may be on a single line: if (score > 60) grade = "pass"; © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

10 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
if...else General Syntax: if (logical expression(s)) { statement(s) } else © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

11 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
if...else Example: if (score > 60) { grade = "pass"; } else grade = "fail"; © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

12 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
if...else if General Syntax: if (logical expression(s)) { statement(s) } else if (logical expression(s)) ... else © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

13 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
if...else if Example: if (score > 90) { grade = "A"; } else if (score > 80) grade = "B"; else if (score > 70) grade = "C"; else if (score > 60) grade = "D"; else grade = "F"; © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

14 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
if...else if The conditions are checked one at a time sequentially. Once a condition is found to be true, the statement(s) for that condition will be executed and the rest of the conditions in the if else if statements group will not be checked. © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

15 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
if...else if Suppose score = 85. Example: if (score > 90) { grade = "A"; } else if (score > 80) grade = "B"; else if (score > 70) grade = "C"; else if (score > 60) grade = "D"; else grade = "F"; © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

16 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
if...else if Suppose score = 85. First check: (score > 90). (85 > 90) is false Example: if (score > 90) { grade = "A"; } else if (score > 80) grade = "B"; else if (score > 70) grade = "C"; else if (score > 60) grade = "D"; else grade = "F"; © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

17 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
if...else if Suppose score = 85. First check: (score > 90) (85 > 90) is false So, check: (score > 80) (85 > 80) is true Example: if (score > 90) { grade = "A"; } else if (score > 80) grade = "B"; else if (score > 70) grade = "C"; else if (score > 60) grade = "D"; else grade = "F"; © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

18 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
if...else if Suppose score = 85. First check: (score > 90) (85 > 90) is false So, check: (score > 80) (85 > 80) is true So, execute: grade = "B" Example: if (score > 90) { grade = "A"; } else if (score > 80) grade = "B"; else if (score > 70) grade = "C"; else if (score > 60) grade = "D"; else grade = "F"; © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

19 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
if...else if Suppose score = 85. First check: (score > 90) (85 > 90) is false So, check: (score > 80) (85 > 80) is true So, execute: grade = "B" Skip the rest of the conditions in the group. Example: if (score > 90) { grade = "A"; } else if (score > 80) grade = "B"; else if (score > 70) grade = "C"; else if (score > 60) grade = "D"; else grade = "F"; © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

20 The order of conditions in if ...else if Statements
The order sometimes matters Example: Let's swap (score > 90) and (score > 60) © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

21 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
if...else if Suppose score = 85. First check: (score > 60). (85 > 60) is true Example: if (score > 60) { grade = "D"; } else if (score > 80) grade = "B"; else if (score > 70) grade = "C"; else if (score > 90) grade = "A"; else grade = "F"; © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

22 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
if...else if Suppose score = 85. First check: (score > 60). (85 > 60) is true So, execute grade = "D" Example: if (score > 60) { grade = "D"; } else if (score > 80) grade = "B"; else if (score > 70) grade = "C"; else if (score > 90) grade = "A"; else grade = "F"; © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

23 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
if...else if Suppose score = 85. First check: (score > 60). (85 > 60) is true So, execute grade = "D" Skip the rest of the conditions in the group. Example: if (score > 60) { grade = "D"; } else if (score > 80) grade = "B"; else if (score > 70) grade = "C"; else if (score > 90) grade = "A"; else grade = "F"; © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

24 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
if...else if Example: if (score > 60) { grade = "D"; } else if (score > 80) grade = "B"; else if (score > 70) grade = "C"; else if (score > 90) grade = "A"; else grade = "F"; With this group of if...else if statements, nobody will get a grade higher than "D"!! © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

25 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Review Question Suppose the variables a and b are both greater than 0. What are the resulting values of s from the following two code segments? Code Segment #1 Code Segment #2 s = 0; if (a > 0) s++; if (b > 0) s++; else if (b > 0) s++; Code segment #1: s = 2 Code segment #2: s = 1 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

26 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Review Question In which of the following if else if statements does the order of the tests matter? Code Segment #1 Code Segment #2 if (score == 1) x = n; else if (score == 2) x = n*n; else if (score == 3) x = n*n*n; else x = -1; if (score <= 1) x = n; else if (score <= 2) x = n*n; else if (score <= 3) x = n*n*n; The order in code segment #2 matters. © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

27 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Nested if Statements Nested if Statements means if statements are placed inside another if statement To create different combinations of conditions for different sets of instructions © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

28 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Nested if Statements Example 1 Example 2 if (age < 40) { if (weight < 150) group = 2; } else group = 3; Example 1: group = 2 Example 2: group = 2 What will the value of group if age = 38 and weight = 145? © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

29 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Nested if Statements Example 1 Example 2 if (age < 40) { if (weight < 150) group = 2; } else group = 3; Example 1: group = 3 Example 2: group is unchanged What will the value of group if age = 38 and weight = 157? © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

30 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Nested if Statements Example 1 Example 2 if (age < 40) { if (weight < 150) group = 2; } else group = 3; Example 1: group is unchanged Example 2: group = 3 What will the value of group if age = 46 and weight = 145? © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

31 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Nested if Statements Example 1 Example 2 if (age < 40) { if (weight < 150) group = 2; } else group = 3; Example 1: group is unchanged Example 2: group = 3 What will the value of group if age = 46 and weight = 157? © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

32 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Logical Operators && AND || OR ! NOT © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

33 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Logical AND: && logicalExpression1 && logicalExpression2 true : only when both logicalExpression1 and logicalExpression2 are true false : when either logicalExpression1 or logicalExpression2 is false © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

34 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Logical OR: || logicalExpression1 || logicalExpression2 true : when either logicalExpression1 or logicalExpression2 is true false : only when both logicalExpression1 and logicalExpression2 is false © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

35 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Logical NOT: ! !logicalExpression1 true : when logicalExpression1 is false false : when logicalExpression1 is true © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

36 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Examples Example 1 Example 2 if (age < 40 && weight < 150) { group = 2; } else group = 3; if (age < 40 || weight < 150) { group = 2; } else group = 3; Example 1: group = 2 Example 2: group = 2 Which statement will be executed in these examples when age = 38 and weight = 145? © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

37 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Examples Example 1 Example 2 if (age < 40 && weight < 150) { group = 2; } else group = 3; if (age < 40 || weight < 150) { group = 2; } else group = 3; Example 1: group = 3 Example 2: group = 2 Which statement will be executed in these examples when age = 38 and weight = 157? © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

38 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Examples Example 1 Example 2 if (age < 40 && weight < 150) { group = 2; } else group = 3; if (age < 40 || weight < 150) { group = 2; } else group = 3; Example 1: group = 3 Example 2: group = 2 Which statement will be executed in these examples when age = 46 and weight = 145? © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

39 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Examples Example 1 Example 2 if (age < 40 && weight < 150) { group = 2; } else group = 3; if (age < 40 || weight < 150) { group = 2; } else group = 3; Example 1: group = 3 Example 2: group = 3 Which statement will be executed in these examples when age = 46 and weight = 157? © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

40 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
switch Statement © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

41 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
switch Statement May be used as an alternative to if...else if statements in cases that: a single expression or variable is compared against several values comparison is equality only (not by greater than or less than) © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

42 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
switch vs. if...else if if (score == 100) { bonus = 10; } else if (score == 200) bonus = 20; else if (score == 300) bonus = 30; else bonus = 0; switch (score) { case 100: bonus = 10; break; case 200: bonus = 20; case 300: bonus = 30; default: bonus = 0; } © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

43 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
switch Statement General Syntax: switch (expression) { case value1: statement(s) break; case value2: default: } © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

44 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
switch Statement Suppose score = 200. First check: (case 100). but score's value is not 100 switch (score) { case 100: bonus = 10; break; case 200: bonus = 20; case 300: bonus = 30; default: bonus = 0; } © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

45 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
switch Statement Suppose score = 200. First check: (case 100) but score's value is not 100 Check (case 200) score's value matches 200 switch (score) { case 100: bonus = 10; break; case 200: bonus = 20; case 300: bonus = 30; default: bonus = 0; } © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

46 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
switch Statement Suppose score = 200. First check: (case 100) but score's value is not 100 Check (case 200) score's value matches 200 Now start to execute statements until the break statement is encountered switch (score) { case 100: bonus = 10; break; case 200: bonus = 20; case 300: bonus = 30; default: bonus = 0; } © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

47 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
switch Statement Suppose score = 200. First check: (case 100) but score's value is not 100 Check (case 200) score's value matches 200 Now the break statement is encountered switch (score) { case 100: bonus = 10; break; case 200: bonus = 20; case 300: bonus = 30; default: bonus = 0; } So, now bonus = 20 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

48 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
switch Statement Suppose score = 200. First check: (case 100) but score's value is not 100 Check (case 200) score's value matches 200 Now the break statement is encountered switch (score) { case 100: bonus = 10; break; case 200: bonus = 20; case 300: bonus = 30; default: bonus = 0; } If we remove the break statement, the execution will fall through. You will see what it means in the next slide. © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

49 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
switch Statement Suppose score = 200. First check: (case 100) but score's value is not 100 Check (case 200) score's value matches 200 Keep executing statements until the break statement is encountered switch (score) { case 100: bonus = 10; break; case 200: bonus = 20; case 300: bonus = 30; default: bonus = 0; } © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

50 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
switch Statement Suppose score = 200. First check: (case 100) but score's value is not 100 Check (case 200) score's value matches 200 Keep executing statements until the break statement is encountered switch (score) { case 100: bonus = 10; break; case 200: bonus = 20; case 300: bonus = 30; default: bonus = 0; } © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

51 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
switch Statement Suppose score = 200. First check: (case 100) but score's value is not 100 Check (case 200) score's value matches 200 Keep executing statements until the break statement is encountered switch (score) { case 100: bonus = 10; break; case 200: bonus = 20; case 300: bonus = 30; default: bonus = 0; } © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

52 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
switch Statement Suppose score = 200. First check: (case 100) but score's value is not 100 Check (case 200) score's value matches 200 Now, the break statement is encountered switch (score) { case 100: bonus = 10; break; case 200: bonus = 20; case 300: bonus = 30; default: bonus = 0; } So, bonus = 0, NOT 20 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

53 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
About Fall Through Not a fault Can be used to implement the logical OR © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

54 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Fall Through Example if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) { numDays = 31; } else if (month == 4 || month == 6 || month == 9 || month == 11) numDays = 30; else if (month == 2) numDays = 28; else numDays = 0; switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: numDays = 31; break; case 6: case 9: case 11: numDays = 30; case 2: numDays = 28; default: bonus = 0; } © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

55 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Review Questions Note to instructor: Depending on your preference, you may want to go over the review questions at the end of this lecture as an instant review or at the beginning of next lecture to refresh students' memory of this lecture. © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

56 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Review Question ___ can be used to make the program execute nonsequentially. if statements Loops A and B © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

57 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Review Question if (a and b > 0) { c += 1; } The previous if statement is supposed to do the following: If both variables a and b are greater than 0, then increment the value of the variable c by 1. However the statement contains errors. Correct the errors. (Assume the variables have been declared.) if (a > 0 && b > 0) { c += 1; } © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

58 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Review Question if (a or b = 0) { c += 1; } The previous if statement is supposed to do the following: If either variable a or b is equal to 0, then increment the value of the variable c by 1. However the statement contains errors. Correct the errors. (Assume the variables have been declared.) if (a == 0 || b == 0) { c += 1; } © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.


Download ppt "© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved."

Similar presentations


Ads by Google