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

Slides:



Advertisements
Similar presentations
Chapter 9 Interactive Multimedia Authoring with Flash Introduction to Programming 1.
Advertisements

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
1 Lecture 8:Control Structures I (Selection) (cont.) Introduction to Computer Science Spring 2006.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
C++ for Engineers and Scientists Third Edition
Visual C++ Programming: Concepts and Projects
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Chapter 9 Interactive Multimedia Authoring with Flash - Introduction to Programming “Computers and Creativity” Richard D. Webster, COSC 109 Instructor.
Conditional Statements While writing a program, there may be a situation when you need to adopt one path out of the given two paths. So you need to make.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Structural Program Development: If, If-Else Outline.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Flow of Control Part 1: Selection
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Chapter 04 Control Statements: Part II. OBJECTIVES In this part you will learn: if…else Double-Selection Statement. while Repetition Statement.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Chapter 5: Structured Programming
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Week 4 Program Control Structure
C++ Programming Lecture 5 Control Structure I (Selection) – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
 2006 Pearson Education, Inc. All rights reserved if…else Double-Selection Statement if – Performs action if condition true if…else – Performs.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
CHAPTER 4 DECISIONS & LOOPS
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Chapter 10 Programming Fundamentals with JavaScript
Java Programming Fifth Edition
Chapter 4 – C Program Control
The if…else Selection Statement
Bill Tucker Austin Community College COSC 1315
EGR 2261 Unit 4 Control Structures I: Selection
DKT121: Fundamental of Computer Programming
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Programming Fundamentals
Chapter 6: Conditional Statements and Loops
JavaScript: Control Statements.
Control Structures.
Chapter 10 Programming Fundamentals with JavaScript
Selection (if-then-else)
Selection CSCE 121 J. Michael Moore.
Chapter 4 - Program Control
3 Control Statements:.
Chapter 4: Control Structures I (Selection)
2.6 The if/else Selection Structure
Week 3 – Program Control Structure
Control Statements Paritosh Srivastava.
Comparing Data & the ‘switch’ Statement
Comparing Data & the ‘switch’ Statement
CprE 185: Intro to Problem Solving (using C)
Structural Program Development: If, If-Else
Presentation transcript:

© 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.

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

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.

© 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.

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.

© 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.

© 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.

© 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.

© 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.

© 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.

© 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.

© 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.

© 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.

© 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.

© 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.

© 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.

© 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.

© 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.

© 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.

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.

© 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.

© 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.

© 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.

© 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.

© 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.

© 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.

© 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.

© 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.

© 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.

© 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.

© 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.

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

© 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.

© 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.

© 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.

© 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.

© 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.

© 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.

© 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.

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

© 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.

© 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.

© 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.

© 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.

© 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.

© 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.

© 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.

© 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.

© 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.

© 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.

© 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.

© 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.

© 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.

© 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.

© 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.

© 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.

© 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.

© 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.