4 Control Statements: Part 1.

Slides:



Advertisements
Similar presentations
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.
Advertisements

Chapter Three Arithmetic Expressions and Assignment Statements
Types of selection structures
PSSA Preparation.
Chapter 04 (Part III) Control Statements: Part I.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
 Control structures  Algorithm & flowchart  If statements  While statements.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 4 – Introducing Algorithms, Pseudocode and.
 2002 Prentice Hall. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 The if Selection Structure.
Chapter 3 - Structured Program Development
Chapter 4 - Control Structures: Part 1 Outline 4.4Control Structures 4.5The if Selection Structure 4.6The if/else Selection Structure 4.7The while Repetition.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
Introduction to Computers and Programming Lecture 8: More Loops New York University.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection Statement 4.6 if else Selection Statement 4.7 while.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
Structured Program Development in C
Lecture 3 Structured Program Development in C
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 4 – Wage Calculator Application: Introducing.
Structured Program Development Outline 2.1Introduction 2.2Algorithms 2.3Pseudo code 2.4Control Structures 2.5The If Selection Structure 2.6The If/Else.
Nested Control Structures * Conditional Operator (?:) * Preincrement and Postincrement * Predecrement and Postdecrement * Counter-Controlled Repetition.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
C Lecture Notes 1 Structured Program Development.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
1 C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved. 4.
 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control.
 2007 Pearson Education, Inc. All rights reserved Structured Program Development in C.
Dale Roberts Program Control using Java - Repetition Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Control Structures: Part 1.
1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 The if Selection Structure 4.6 The if / else Selection Structure 4.7.
1 Control Statements: Part I Chapter 4 Control Statements: Part I Chapter 4.
 2003 Prentice Hall, Inc. All rights reserved. 1 Control Structures Outline -Introduction -Algorithms -Pseudocode -Control Structures -if Selection Structure.
C Programming 2002 Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
Structured Program Development Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved. 1.
1 Lecture 3 Control Structures else/if and while.
Chapter 3 Structured Program Development Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Will not cover 4.14, Thinking About Objects: Identifying Class Attributes Chapter 4 - Control Structures.
Lecture 5: Layers of Control. Nested while Loops Problem Multiplying two numbers and outputting the result only if they are both less than 5. (i.e. Start.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (while) Outline 3.7The While Repetition.
1 Chapter 4 - Control Statements: Part 1 Outline 4.1 Introduction 4.4 Control Structures 4.5 if Selection Structure 4.6 if/else Selection Structure 4.7.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
Algorithm: procedure in terms of
Control Statements: Part 1
Chapter 4 – Control Structures Part 1
4 Control Statements: Part 1.
Adopted from Pearson Education slides
Chapter 4 Control Statements: Loops 1
MSIS 655 Advanced Business Applications Programming
Structured Program
Chapter 3 - Structured Program Development
3 Control Statements:.
Chapter 3 - Structured Program Development
Chapter 4 - Control Structures: Part 1
EPSII 59:006 Spring 2004.
Presentation transcript:

4 Control Statements: Part 1

4.1 Introduction Before writing a program While writing a program Have a thorough understanding of problem Carefully plan your approach for solving it While writing a program Know what “building blocks” are available Use good programming principles

4.2 Algorithms Algorithms Control Statements The actions to execute The order in which these actions execute Control Statements Specify the order in which actions execute in a program

4.3 Pseudocode Pseudocode Artificial, informal language used to develop algorithms Used to think out program before coding Easy to convert into program (eg. C++) Executable statements in English For the programmer, not executed on computers

Fig. 4.1 | Pseudocode for the addition program of Fig. 2.5. Problem: Read two integers from the user and display their sum. Fig. 4.1 | Pseudocode for the addition program of Fig. 2.5.

4.4 Control Structures Only three types of structured constructs needed (one-in one-out) Sequence statements executed sequentially by default Conditional if, if…else, switch Repetition while, do…while, for No goto statements => structured programming Demonstrated by Böhm and Jacopini

4.4 Control Structures (Cont.) UML activity diagram Models the workflow Action state symbols Rectangles with curved sides Small circles Solid circle is the initial state Solid circle in a hollow circle is the final state Transition arrows Represent the flow of activity Comment notes Connected to diagram by dotted lines

Fig. 4.2 | Sequence-structure activity diagram.

Good Programming Practice 4.1 Consistently applying reasonable indentation conventions throughout your programs greatly improves program readability.

Fig. 4.4 | if single-selection statement activity diagram.

Fig. 4.5 | if...else double-selection statement activity diagram.

4.7 while Repetition Statement (Cont.) UML merge symbol Joins two or more flows of activity into one flow of activity Represented as a diamond Unlike the decision symbol a merge symbol has Multiple incoming transition arrows Only one outgoing transition arrows No guard conditions on outgoing transition arrows Has no counterpart in C++ code

Fig. 4.6 | while repetition statement UML activity diagram.

4.9 Formulating Algorithms: Repetition Problem statement Develop a class average program that processes grades for an arbitrary number of students each time it is run. Top-down, stepwise refinement Development technique for well-structured programs Top step Single statement conveying overall function of the program Example Determine the class average for the quiz First refinement Multiple statements using only the sequence structure Initialize variables Input, sum and count the quiz grades Calculate and print the total of all student grades and the class average

Determine the class average for the quiz. Initialize variables Input, sum and count the quiz grades Calculate and print the total of all student grades and the class average Fig. 4.11 | Class average problem pseudocode algorithm with sentinel-controlled repetition.

Outline fig04_12.cpp (1 of 1)

Outline fixed forces output to print in fixed point format (not scientific notation) and forces trailing zeros and decimal point to print fig04_13.cpp (1 of 4) steprecision stream manipulator (in header <iomanip>) sets numeric output precision

Outline fig04_13.cpp (2 of 4)

Outline Function determineClassAverage implements the class average algorithm described by the pseudocode fig04_13.cpp (3 of 4) Declare local int variables total, gradeCounter and grade and double variable average while loop iterates as long as grade does not equal the sentinel value -1

Outline fig04_13.cpp (4 of 4) Calculate average grade using static_cast< double > to perform explicit conversion

Outline fig04_14.cpp (1 of 1)

4.10 Formulating Algorithms: Nested Control Statement Problem statement A college offers a course that prepares students for the state licensing exam for real estate brokers. Last year, ten of the students who completed this course took the exam. The college wants to know how well its students did on the exam. You have been asked to write a program to summarize the results. You have been given a list of these 10 students. Next to each name is written a 1 if the student passed the exam or a 2 if the student failed. Your program should analyze the results of the exam as follows: 1. Input each test result (i.e., a 1 or a 2). Display the prompting message “Enter result” each time the program requests another test result. 2. Count the number of test results of each type. 3. Display a summary of the test results indicating the number of students who passed and the number who failed. 4. If more than eight students passed the exam, print the message “Raise tuition.”

Outline fig04_18.cpp (2 of 2) More than eight students passed the exam

4.10 Formulating Algorithms: Nested Control Statement (Cont.) Notice that Program processes 10 results Fixed number, use counter-controlled loop Each test result is 1 or 2 If not 1, assume 2 Two counters can be used One counts number that passed Another counts number that failed Must decide whether more than eight students passed

4.10 Formulating Algorithms: Nested Control Statement (Cont.) Top level outline Analyze exam results and decide whether tuition should be raised First refinement Initialize variables Input the ten exam results and count passes and failures Print a summary of the exam results and decide whether tuition should be raised Second Refinement Initialize variables to Initialize passes to zero Initialize failures to zero Initialize student counter to one

4.10 Formulating Algorithms: Nested Control Statement (Cont.) Second Refinement (Cont.) Input the ten exam results and count passes and failures to While student counter is less than or equal to ten Prompt the user to enter the next exam result If the student passed Add one to passes Else Add one to failures Add one to student counter

4.10 Formulating Algorithms: Nested Control Statement (Cont.) Second Refinement (Cont.) Print a summary of the exam results and decide whether tuition should be raised to Print the number of passes Print the number of failures If more than eight students passed Print “Raise tuition”

Fig. 4.15 | Pseudocode for examination-results problem.

Outline fig04_16.cpp (1 of 1)

Outline (1 of 2) Declare function processExamResults’s local variables fig04_17.cpp (1 of 2) Declare function processExamResults’s local variables

Outline fig04_17.cpp (2 of 2) Determine whether this student passed or failed, and increment the appropriate variable Determine whether more than eight students passed

Outline fig04_18.cpp (1 of 2)

4.11 Assignment Operators Assignment expression abbreviations Addition assignment operator Example c = c + 3; abbreviates to c += 3; Statements of the form variable = variable operator expression; can be rewritten as variable operator= expression; Other assignment operators d -= 4 (d = d - 4) e *= 5 (e = e * 5) f /= 3 (f = f / 3) g %= 9 (g = g % 9)

Fig. 4.19 | Arithmetic assignment operators.

4.12 Increment and Decrement Operators Increment operator ++ Increments variable by one Example c++ Decrement operator -- Decrement variable by one c--

4.12 Increment and Decrement Operators (Cont.) Preincrement When the operator is used before the variable (++c or --c) Variable is changed, then the expression it is in is evaluated using the new value Postincrement When the operator is used after the variable (c++ or c--) Expression the variable is in executes using the old value, then the variable is changed

Fig. 4.20 | Increment and decrement operators.

Good Programming Practice 4.8 Unlike binary operators, the unary increment and decrement operators should be placed next to their operands, with no intervening spaces.

Outline (1 of 1) Postincrementing the c variable fig04_21.cpp (1 of 1) Postincrementing the c variable Preincrementing the c variable

4.12 Increment and Decrement Operators (Cont.) If c = 5, then cout << ++c; c is changed to 6 Then prints out 6 cout << c++; Prints out 5 (cout is executed before the increment) c then becomes 6

4.12 Increment and Decrement Operators (Cont.) When variable is not in an expression Preincrementing and postincrementing have same effect Example ++c; cout << c; and c++; cout << c; are the same

Common Programming Error 4.14 4Attempting to use the increment or decrement operator on an expression other than a modifiable variable name or reference, e.g., writing ++(x + 1), is a syntax error.

Fig. 4.22 | Operator precedence for the operators encountered so far in the text.