Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.

Slides:



Advertisements
Similar presentations
Flow Control if, while, do-while Juan Marquez (03_flow_control.ppt)
Advertisements

Computer Science 1620 Loops.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
1 Chapter 2 C++ Basics. 2 Overview Variables, Constants and Assignments (2.1) Input and Output (2.2) Data Types and Expressions (2.3) Simple Flow of Control.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Thursday, December 14, 2006 “Would you tell me, please, which way I ought to go from here?” “That depends a good deal on where you want to get to,” said.
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.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 5: Control Structures II (Repetition)
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 2 C++ Basics.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
CONTROLLING PROGRAM FLOW
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
© 2006 Pearson Education 1 More Operators  To round out our knowledge of Java operators, let's examine a few more  In particular, we will examine the.
# ACS 168 Structured Programming Using the Computer Chapter 2 Spring 2002 Prepared by Shirley White.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 C++ Basics.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 2- 1 June 3, June 3, 2016June 3, 2016June 3, 2016 Azusa, CA.
6/3/2016 CSI Chapter 02 1 Introduction of Flow of Control There are times when you need to vary the way your program executes based on given input.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Flow of Control Chapter 3. Outline Branching Statements Java Loop Statements Programming with Loops The Type boolean.
CSCI 161 Lecture 7 Martin van Bommel. Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May.
COIT29222 Structured Programming 1 COIT29222-Structured Programming Lecture Week 02  Reading: Textbook(4 th Ed.), Chapter 2 Textbook (6 th Ed.), Chapters.
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 5: Control Structures II (Repetition)
Chapter 4 October 22, The If Statement Programs make decisions If(condition){ Statement(s); } Condition  boolean expression Evaluates to either.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Controlling Program Flow with Decision Structures.
COMP Loop Statements Yi Hong May 21, 2015.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 2 C++ Basics.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Branching statements.
Chapter 3 Control Statements
Data Types and Expressions
Chapter 2 C++ Basics. Chapter 2 C++ Basics Overview 2.1 Variables and Assignments 2.2 Input and Output 2.3 Data Types and Expressions 2.4 Simple Flow.
Chapter 2 C++ Basics.
Data Types and Expressions
REPETITION CONTROL STRUCTURE
Selection (also known as Branching) Jumail Bin Taliba by
Selections Java.
The switch Statement, and Introduction to Looping
EGR 2261 Unit 4 Control Structures I: Selection
Loop Structures.
The Selection Structure
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Programming Fundamentals
JavaScript: Control Statements.
Chapter 8 JavaScript: Control Statements, Part 2
CMSC 202 Java Primer 2.
3 Control Statements:.
Chapter 6: Repetition Statements
Computing Fundamentals
Chapter 4: Control Structures I (Selection)
© Copyright 2016 by Pearson Education, Inc. All Rights Reserved.
2.6 The if/else Selection Structure
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Chapter 5: Control Structures II (Repetition)
Chapter 3: Selection Structures: Making Decisions
Control Statements Paritosh Srivastava.
Using C++ Arithmetic Operators and Control Structures
Flow of Control Flow of control is the order in which a program performs actions. Up to this point, the order has been sequential. A branching statement.
Chapter 3 Flow of Control Loops in Java.
Presentation transcript:

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Simple Flow of Control Flow of control The order in which statements are executed Branch Lets program choose between two alternatives Slide 2- 3

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Branch Example To calculate hourly wages there are two choices Regular time ( up to 40 hours) gross_pay = rate * hours; Overtime ( over 40 hours) gross_pay = rate * * rate * (hours - 40); The program must choose which of these expressions to use Slide 2- 4

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Designing the Branch Decide if (hours >40) is true If it is true, then use gross_pay = rate * * rate * (hours - 40); If it is not true, then use gross_pay = rate * hours; Slide 2- 5

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Implementing the Branch if-else statement is used in C++ to perform a branch if (hours > 40) gross_pay = rate * * rate * (hours - 40); else gross_pay = rate * hours; Slide 2- 6 Display 2.8 Display 2.9

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Boolean Expressions Boolean expressions are expressions that are either true or false comparison operators such as '>' (greater than) are used to compare variables and/or numbers (hours > 40) Including the parentheses, is the boolean expression from the wages example A few of the comparison operators that use two symbols (No spaces allowed between the symbols!) >= greater than or equal to != not equal or inequality = = equal or equivalent Slide 2- 7 Display 2.10

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. if-else Flow Control (1) if (boolean expression) true statement else false statement When the boolean expression is true Only the true statement is executed When the boolean expression is false Only the false statement is executed Slide 2- 8

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. if-else Flow Control (2) if (boolean expression) { true statements } else { false statements } When the boolean expression is true Only the true statements enclosed in { } are executed When the boolean expression is false Only the false statements enclosed in { } are executed Slide 2- 9

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. AND Boolean expressions can be combined into more complex expressions with && -- The AND operator True if both expressions are true Syntax: (Comparison_1) && (Comparison_2) Example: if ( (2 < x) && (x < 7) ) True only if x is between 2 and 7 Inside parentheses are optional but enhance meaning Slide 2- 10

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. OR | | -- The OR operator (no space!) True if either or both expressions are true Syntax: (Comparison_1) | | (Comparison_2) Example: if ( ( x = = 1) | | ( x = = y) ) True if x contains 1 True if x contains the same value as y True if both comparisons are true Slide 2- 11

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. NOT ! -- negates any boolean expression !( x < y) True if x is NOT less than y !(x = = y) True if x is NOT equal to y ! Operator can make expressions difficult to understand…use only when appropriate Slide 2- 12

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Inequalities Be careful translating inequalities to C++ if x < y < z translates as if ( ( x < y ) && ( y < z ) ) NOT if ( x < y < z ) Slide 2- 13

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Pitfall: Using = or == ' = ' is the assignment operator Used to assign values to variables Example: x = 3; '= = ' is the equality operator Used to compare values Example: if ( x == 3) The compiler will accept this error: if (x = 3) but stores 3 in x instead of comparing x and 3 Since the result is 3 (non-zero), the expression is true Slide 2- 14

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Compound Statements A compound statement is more than one statement enclosed in { } Branches of if-else statements often need to execute more that one statement Example: if (boolean expression) { true statements } else { false statements } Slide Display 2.11

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Branches Conclusion Can you Write an if-else statement that outputs the word High if the value of the variable score is greater than 100 and Low if the value of score is at most 100? The variables are of type int. Write an if-else statement that outputs the word Warning provided that either the value of the variable temperature is greater than or equal to 100, or the of the variable pressure is greater than or equal to 200, or both. Otherwise, the if_else sttement outputs the word OK. The variables are of type int. Slide 2- 16

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Simple Loops When an action must be repeated, a loop is used C++ includes several ways to create loops We start with the while-loop Example: while (count_down > 0) { cout << "Hello "; count_down -= 1; } Output: Hello Hello Hello when count_down starts at 3 Slide Display 2.12

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. While Loop Operation First, the boolean expression is evaluated If false, the program skips to the line following the while loop If true, the body of the loop is executed During execution, some item from the boolean expression is changed After executing the loop body, the boolean expression is checked again repeating the process until the expression becomes false A while loop might not execute at all if the boolean expression is false on the first check Slide 2- 18

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. while Loop Syntax while (boolean expression is true) { statements to repeat } Semi-colons are used only to end the statements within the loop while (boolean expression is true) statement to repeat Slide Display 2.13

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. do-while loop A variation of the while loop. A do-while loop is always executed at least once The body of the loop is first executed The boolean expression is checked after the body has been executed Syntax: do { statements to repeat } while (boolean_expression); Slide Display 2.14 Display 2.15

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Increment/Decrement Unary operators require only one operand + in front of a number such as +5 - in front of a number such as increment operator Adds 1 to the value of a variable x ++; is equivalent to x = x + 1; -- decrement operator Subtracts 1 from the value of a variable x --; is equivalent to x = x – 1; Slide 2- 21

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Sample Program Bank charge card balance of $50 2% per month interest How many months without payments before your balance exceeds $100 After 1 month: $50 + 2% of $50 = $51 After 2 months: $51 + 2% of $51 = $52.02 After 3 months: $ % of $52.02 … Slide Display 2.16

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Infinite Loops Loops that never stop are infinite loops The loop body should contain a line that will eventually cause the boolean expression to become false Example: Print the odd numbers less than 12 x = 1; while (x != 12) { cout << x << endl; x = x + 2; } Better to use this comparison: while ( x < 12) Slide 2- 23

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Section 2.4 Conclusion Can you Show the output of this code if x is of type int? x = 10; while ( x > 0) { cout << x << endl; x = x – 3; } Show the output of the previous code using the comparison x 0? Slide 2- 24

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Program Style A program written with attention to style is easier to read easier to correct easier to change Slide 2- 25

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Program Style - Indenting Items considered a group should look like a group Skip lines between logical groups of statements Indent statements within statements if (x = = 0) statement; Braces {} create groups Indent within braces to make the group clear Braces placed on separate lines are easier to locate Slide 2- 26

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Program Style - Comments // is the symbol for a single line comment Comments are explanatory notes for the programmer All text on the line following // is ignored by the compiler Example: //calculate regular wages gross_pay = rate * hours; /* and */ enclose multiple line comments Example: /* This is a comment that spans multiple lines without a comment symbol on the middle line */ Slide 2- 27

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Program Style - Constants Number constants have no mnemonic value Number constants used throughout a program are difficult to find and change when needed Constants Allow us to name number constants so they have meaning Allow us to change all occurrences simply by changing the value of the constant Slide 2- 28

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Constants const is the keyword to declare a constant Example: const int WINDOW_COUNT = 10; declares a constant named WINDOW_COUNT Its value cannot be changed by the program like a variable It is common to name constants with all capitals Slide Display 2.17

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Section 2.5 Conclusion Can you Create a named constant of type double? Determine if a program can modify the value of a constant? Describe the benefits of comments? Explain why indenting is important in a program? Explain why blank lines are important in a program? Slide 2- 30