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.

Slides:



Advertisements
Similar presentations
4 Control Statements: Part 1.
Advertisements

Chapter 04 (Part III) Control Statements: Part I.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
 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.
Lec3: Structured Program Development
© 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.
 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
Control Structures in C++ while, do/while, for switch, break, continue.
 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.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 8 - JavaScript: Control Structures I Outline 8.1 Introduction 8.2 Algorithms 8.3 Pseudocode 8.4.
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.
C++ Programming Lecture 6 Control Structure II (Repetition) By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Chapter 3 Structured Program Development. Objectives To understand basic problem-solving techniques. To be able to develop algorithms through the process.
 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.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
 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.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Control Structures: Part 1 Outline 4.1Introduction 4.2Algorithms 4.3Pseudocode 4.4Control.
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.
 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.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 8 - JavaScript: Control Structures I Outline 8.1 Introduction 8.2 Algorithms 8.3 Pseudocode 8.4.
1 Lecture 3 Control Structures else/if and while.
Lecture 5: Stopping with a Sentinel. Using a Sentinel Problem Develop a class-averaging program that will process an arbitrary number of grades each time.
Computer Programming Control Structure
Chapter 3 Structured Program Development Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
 2002 Prentice Hall. All rights reserved. 1 Chapter 4 – Control Structures Part 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures.
 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.
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.
JavaScript: Control Structures I Outline 1 Introduction 2 Algorithms 3 Pseudocode 4 Control Structures 5 if Selection Structure 6 if/else Selection Structure.
 2007 Pearson Education, Inc. All rights reserved Structured Program Development in C.
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.
Structured Program Development in C
Algorithm: procedure in terms of
Lecture 7: Repeating a Known Number of Times
Control Statements Kingdom of Saudi Arabia
Chapter 4 – Control Structures Part 1
Chapter 8 - JavaScript: Control Structures I
Structured Program
Programming in Pseudocode
Chapter 3 - Structured Program Development
3 Control Statements:.
Chapter 3 - Structured Program Development
Structured Program Development in C
Chapter 4 - Control Structures: Part 1
EPSII 59:006 Spring 2004.
-2- Introduction to C Programming
Structured Program Development in C
Structured Program Development in C
Chapter 8 - JavaScript: Control Structures I
Presentation transcript:

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 at i=1, j=1 and go from there) Formulate pseudocode algorithm i = 1 j = 1: 1 * j = j = 2: 1 * j = j = 3: 1 * j = j = 4: 1 * j = i = 2 j = 1: 2 * j = j = 2: 2 * j = j = 3: 2 * j = j = 4: 2 * j = j = 1; While j is less than 5 print out the result of 1 * j; j = j + 1; j = 1; While j is less than 5 print out the result of 2 * j; j = j + 1;

Nested while Loops Formulate pseudocode algorithm i = 3 j = 1: 3 * j = j = 2: 3 * j = j = 3: 3 * j = j = 4: 3 * j = i = 4 j = 1: 4 * j = j = 2: 4 * j = j = 3: 4 * j = j = 4: 4 * j = j = 1; While j is less than 5 print out the result of 3 * j; j = j + 1; j = 1; While j is less than 5 print out the result of 4 * j; j = j + 1;

Formulate Pseudocode Algorithm i = 1 i = 2 i = 3 i = 4 j = 1; While j is less than 5 print out the result of 1 * j; j = j + 1; j = 1; While j is less than 5 print out the result of 2 * j; j = j + 1; j = 1; While j is less than 5 print out the result of 3 * j; j = j + 1; j = 1; While j is less than 5 print out the result of 4 * j; j = j + 1; i i i i i = 1; While i is less than 5 i = i + 1; j = 1; While j is less than 5 print out the result of 1 * j; j = j + 1; i

Pseudocode i = 1; While i is less than 5 i = i + 1; j = 1; While j is less than 5 print out the result of i * j; j = j + 1;

C Code

Nested Control Structures Problem A college has a list of test results (1 = pass, 2 = fail) for 10 students. Write a program that analyzes the results as follows: 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. Count the number of test results of each type Display a summary of the test results indicating the number of students who passed and the number who failed If more than 8 students passed the test, print the message “Raise tuition.”

Before Formulate the Algorithm The program must process 10 test results Counter-controlled loop will be used Two counter can be used One for number of passes, one for number of fails Each test result is a number -- either a 1 or a 2 If the number is not a 1, we assume that it is a 2 Decide if more than 8 students passed the test.

Formulate the Pseudocode Algorithm Top level outline Analyze exam results and decide if tuition should be raised First refinement Initialize variables Input the ten quiz grades and count passes and failures Print a summary of the exam results and decide if tuition should be raised Refine Initialize variables to Initialize passes to zero Initialize failures to zero Initialize student counter to one

Formulate the Pseudocode Algorithm Refine Input the ten quiz grades and count passes and failures to While student counter is less than or equal to ten Input the next exam result If the student passed Add one to passes else Add one to failures Add one to student counter Refine Print a summary of the exam results and decide if tuition should be raised to Print the number of passes Print the number of failures If more than eight students passed Print “Raise tuition”

Pseudocode for Test Results Analysis

C code for Test Results Analysis while loop continues until 10 students have been processed if and else statements are nested inside while loop Initializing variables when they are defined can help reduce a program’s execution time.

C code for Test Results Analysis

Assignment Operators Assignment operators abbreviate assignment expressions c = c + 3 can be abbreviated as c += 3; using the addition assignment operator “+=” Statements of the form variable = variable operator expression; can be rewritten as variable operator= expression; Examples of other assignment operators: d -= 4 (d = d - 4) e *= 5 (e = e * 5) f /= 3 (f = f / 3) g %= 9 (g = g % 9)

Arithmetic Assignment Operators

Increment and Decrement Operators Increment operator (++) Can be used instead of c += 1 Decrement operator (--) Can be used instead of c -= 1 Pre-increment/Pre-decrement Operator is used before the variable (++c or --c) Variable is changed before the expression it is in is evaluated Post-increment/Post-decrement Operator is used after the variable (c++ or c--) Expression executes before the variable is changed

Increment and Decrement Operators If c equals 5, then printf( "%d", ++c ); Prints 6 printf( "%d", c++ ); Prints 5 In either case, c now has the value of 6 When variable not in an expression Preincrementing and postincrementing have the same effect ++c; printf( “%d”, c ); Has the same effect as c++; printf( “%d”, c );

Increment and Decrement Operators

c is printed, then incremented c is incremented, then printed

Precedence of the Operators

Create a program that prints out a triangle with a user specified width. Example output: Enter the specified width:5 X XX XXX XXXX XXXXX XXXX XXX XX X In-Class Programming Exercise