 2002 Prentice Hall. All rights reserved. 1 Chapter 4 – Control Structures Part 1 Outline Counter-Controlled Repetition: Example Sentinel-Controlled Repetition:

Slides:



Advertisements
Similar presentations
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.11 Assignment Operators 2.12 Increment and Decrement Operators.
Advertisements

1 Outline Chapter 4 Introduction Control Structures if Single-Selection Statement if else Selection Statement while Repetition Statement Assignment Operators.
 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.
Introduction to Computers and Programming Lecture 7:
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.1 Introduction 2.2 Algorithms 2.3 Pseudocode 2.4 Control Structures.
LOOPING do { } while ( ) for (I = 0; I < 5; I++) { } while ( ) { }
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.1 Introduction 2.2 Algorithms 2.3 Pseudocode 2.4 Control Structures.
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.
Introduction to Computers and Programming Lecture 8: More Loops New York University.
Introduction to Computers and Programming More Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
 2003 Prentice Hall, Inc. All rights reserved. 1 Input / Output Input/output –cin Standard input stream Normally keyboard –cout Standard output stream.
 2003 Prentice Hall, Inc. All rights reserved. 1 Algorithms Computing problems –Solved by executing a series of actions in a specific order Algorithm.
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.
Control Structures in C++ while, do/while, for switch, break, continue.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
 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.
 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.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.1Introduction 2.2Algorithms 2.3Pseudocode 2.4Control Structures.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 4.
 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.
 2001 Prentice Hall, Inc. All rights reserved. 1 Introduction to C# Programming Part I.
Advanced Programming LOOP.
1 Lecture 4 for loops and switch statements Essentials of Counter-Controlled Repetition Counter-controlled repetition requires  Name of control.
 2003 Prentice Hall, Inc. All rights reserved. 1 Control Structures Outline Assignment Operators Increment and Decrement Operators Essentials of Counter-Controlled.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.1 Introduction 2.2 Algorithms 2.3 Pseudocode 2.4 Control Structures.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.1 Introduction 2.2 Algorithms 2.3 Pseudocode 2.4 Control Structures.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
EC-111 Algorithms & Computing Lecture #4 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
 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.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.1Introduction 2.2Algorithms 2.3Pseudocode 2.4Control Structures.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.1 Introduction 2.2 Algorithms 2.3 Pseudocode 2.4 Control Structures.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.1 Introduction 2.2 Algorithms 2.3 Pseudocode 2.4 Control Structures.
9/20: The while Repetition Structure last time’s program repetition structures: what they are the while repetition structure homework due on Thursday program.
 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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.1 Introduction 2.2 Algorithms 2.3 Pseudocode 2.4 Control Structures.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 17 - WMLScript: Control Structures I Outline 17.1 Introduction 17.2 Algorithms 17.3 Pseudocode.
Jozef Goetz,  2011 Pearson Education, Inc. All rights reserved.  2002 Prentice Hall. All rights reserved. expanded by J. Goetz, 2012.
 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.
1 JavaScript: Control Structures. 2 Control Structures Flowcharting JavaScript’s sequence structure.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
C++ Programming Lecture 7 Control Structure I (Selection) – Part II The Hashemite University Computer Engineering Department.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1 Introduction 5.2 Essentials of Counter-Controlled.
 2001 Prentice Hall, Inc. All rights reserved. 1 Introduction to C# Part II.
Advanced Programming LOOP. 2 while Repetition Structure Repetition Structure –An action is to be repeated Continues while statement is true Ends when.
2/18: Assignment Operators About Average2.java –while loop use –explicit casting –twoDigits object Assignment Operators Increment & Decrement Operators.
 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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.1 Introduction 2.2 Algorithms 2.3 Pseudocode 2.4 Control Structures.
5.1 Introduction Problem Solving –Requires understanding of: Building blocks Program-construction principles BZUPAGES.COM.
Introduction to Computers and Programming Lecture 7:
 2003 Prentice Hall, Inc. All rights reserved. 1 Will not cover 4.14, Thinking About Objects: Identifying Class Attributes Chapter 4 - Control Structures.
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
A DVANCED P ROGRAMMING C HAPTER 5 & 6: C ONTROL S TRUCTURES Dr Shahriar Bijani Spring 2016.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 6.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.1 Introduction 2.2 Algorithms 2.3 Pseudocode 2.4 Control Structures.
 2003 Prentice Hall, Inc. All rights reserved. ECE 2551 Dr. S. Kozaitis Fall Chapter 5 - Control Statements: Part 2 Outline 5.3 for Repetition.
JavaScript: Control Structures I Outline 1 Introduction 2 Algorithms 3 Pseudocode 4 Control Structures 5 if Selection Structure 6 if/else Selection Structure.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.1 Introduction 2.2 Algorithms 2.3 Pseudocode 2.4 Control Structures.
Chapter 4 – Control Structures Part 1
مبانی برنامه‌سازی با C++ جلسه دوم
Chapter 8 - JavaScript: Control Structures I
Advanced Programming Chapters 5 & 6: Control Structures
switch Selection Structure
2.6 The if/else Selection Structure
Chapter 5 – Control Structures: Part 2
Chapter 8 - JavaScript: Control Structures I
C++ Programming Lecture 7 Control Structure I (Selection) – Part II
Presentation transcript:

 2002 Prentice Hall. All rights reserved. 1 Chapter 4 – Control Structures Part 1 Outline Counter-Controlled Repetition: Example Sentinel-Controlled Repetition: Example Assignment Operators Increment and Decrement Operators for Repetition Structure: Examples switch Multiple-Selection Structure

 2002 Prentice Hall. All rights reserved. Outline 2 Average1.cs 1 // Fig. 4.7: Average1.cs 2 // Class average with counter-controlled repetition. 3 4 using System; 5 6 class Average1 7 { 8 static void Main( string[] args ) 9 { 10 int total, // sum of grades 11 gradeCounter, // number of grades entered 12 gradeValue, // grade value 13 average; // average of all grades // initialization phase 16 total = 0; // clear total 17 gradeCounter = 1; // prepare to loop // processing phase 20 while ( gradeCounter <= 10 ) // loop 10 times 21 { 22 // prompt for input and read grade from user 23 Console.Write( "Enter integer grade: " ); // read input and convert to integer 26 gradeValue = Int32.Parse( Console.ReadLine() ); // add gradeValue to total 29 total = total + gradeValue; // add 1 to gradeCounter 32 gradeCounter = gradeCounter + 1; 33 }

 2002 Prentice Hall. All rights reserved. Outline 3 Average1.cs Program Output // termination phase 36 average = total / 10; // integer division // display average of exam grades 39 Console.WriteLine( "\nClass average is {0}", average ); } // end Main } // end class Average1 Enter integer grade: 100 Enter integer grade: 88 Enter integer grade: 93 Enter integer grade: 55 Enter integer grade: 68 Enter integer grade: 77 Enter integer grade: 83 Enter integer grade: 95 Enter integer grade: 73 Enter integer grade: 62 Class average is 79

 2002 Prentice Hall. All rights reserved. Outline 4 Average2.cs 1 // Fig. 4.9: Average2.cs 2 // Class average with sentinel-controlled repetition. 3 4 using System; 5 6 class Average2 7 { 8 static void Main( string[] args ) 9 { 10 int total, // sum of grades 11 gradeCounter, // number of grades entered 12 gradeValue; // grade value double average; // average of all grades // initialization phase 17 total = 0; // clear total 18 gradeCounter = 0; // prepare to loop // processing phase 21 // prompt for input and convert to integer 22 Console.Write( "Enter Integer Grade, -1 to Quit: " ); 23 gradeValue = Int32.Parse( Console.ReadLine() ); 24

 2002 Prentice Hall. All rights reserved. Outline 5 Average2.cs 25 // loop until a -1 is entered by user 26 while ( gradeValue != -1 ) 27 { 28 // add gradeValue to total 29 total = total + gradeValue; // add 1 to gradeCounter 32 gradeCounter = gradeCounter + 1; // prompt for input and read grade from user 35 // convert grade from string to integer 36 Console.Write( "Enter Integer Grade, -1 to Quit: " ); 37 gradeValue = Int32.Parse( Console.ReadLine() ); } // end while // termination phase 42 if ( gradeCounter != 0 ) 43 { 44 average = ( double ) total / gradeCounter; // display average of exam grades 47 Console.WriteLine( "\nClass average is {0}", average ); 48 } 49 else 50 { 51 Console.WriteLine( "\nNo grades were entered" ); 52 } } // end method Main } // end class Average2

 2002 Prentice Hall. All rights reserved. Outline 6 Average2.cs Program Output Enter Integer Grade, -1 to Quit: 97 Enter Integer Grade, -1 to Quit: 88 Enter Integer Grade, -1 to Quit: 72 Enter Integer Grade, -1 to Quit: -1 Class average is

 2002 Prentice Hall. All rights reserved Assignment Operators

 2002 Prentice Hall. All rights reserved Increment and Decrement Operators Increment operator –Used to add one to the variable –x++ –Same as x = x + 1 Decrement operator –Used to subtract 1 from the variable –y-- Pre-increment vs. post-increment –x++ or x-- Will perform an action and then add to or subtract one from the value –++x or --x Will add to or subtract one from the value and then perform an action

 2002 Prentice Hall. All rights reserved Increment and Decrement Operators

 2002 Prentice Hall. All rights reserved. Outline 10 Increment.cs Program Output 1 // Fig. 4.14: Increment.cs 2 // Preincrementing and postincrementing 3 4 using System; 5 6 class Increment 7 { 8 static void Main(string[] args) 9 { 10 int c; c = 5; 13 Console.WriteLine( c ); // print 5 14 Console.WriteLine( c++ ); // print 5 then postincrement 15 Console.WriteLine( c ); // print Console.WriteLine(); // skip a line c = 5; 20 Console.WriteLine( c ); // print 5 21 Console.WriteLine( ++c ); // preincrement then print 6 22 Console.WriteLine( c ); // print } // end of method Main } // end of class Increment

 2002 Prentice Hall. All rights reserved Increment and Decrement Operators

 2002 Prentice Hall. All rights reserved. Outline 12 Interest.cs 1 // Fig. 5.8: Interest.cs 2 // Calculating compound interest. 3 4 using System; 5 using System.Windows.Forms; 6 7 class Interest 8 { 9 static void Main( string[] args ) 10 { 11 decimal amount, principal = ( decimal ) ; 12 double rate =.05; 13 string output; output = "Year\tAmount on deposit\n"; for ( int year = 1; year <= 10; year++ ) 18 { 19 amount = principal * 20 ( decimal ) Math.Pow( rate, year ); output += year + "\t" + 23 String.Format( "{0:C}", amount ) + "\n"; 24 } MessageBox.Show( output, "Compound Interest", 27 MessageBoxButtons.OK, MessageBoxIcon.Information ); } // end method Main } // end class Interest

 2002 Prentice Hall. All rights reserved. Outline 13 Interest.cs Program Output

 2002 Prentice Hall. All rights reserved. 14

 2002 Prentice Hall. All rights reserved. 15

 2002 Prentice Hall. All rights reserved. 16

 2002 Prentice Hall. All rights reserved. Outline 17 SwitchTest.cs 1 // Fig. 5.10: SwitchTest.cs 2 // Counting letter grades. 3 4 using System; 5 6 class SwitchTest 7 { 8 static void Main( string[] args ) 9 { 10 char grade; // one grade 11 int aCount = 0, // number of As 12 bCount = 0, // number of Bs 13 cCount = 0, // number of Cs 14 dCount = 0, // number of Ds 15 fCount = 0; // number of Fs for ( int i = 1; i <= 10; i++ ) 18 { 19 Console.Write( "Enter a letter grade: " ); 20 grade = Char.Parse( Console.ReadLine() ); switch ( grade ) 23 { 24 case 'A': // grade is uppercase A 25 case 'a': // or lowercase a 26 ++aCount; 27 break; case 'B': // grade is uppercase B 30 case 'b': // or lowercase b 31 ++bCount; 32 break; 33

 2002 Prentice Hall. All rights reserved. Outline 18 SwitchTest.cs 34 case 'C': // grade is uppercase C 35 case 'c': // or lowercase c 36 ++cCount; 37 break; case 'D': // grade is uppercase D 40 case 'd': // or lowercase d 41 ++dCount; 42 break; case 'F': // grade is uppercase F 45 case 'f': // or lowercase f 46 ++fCount; 47 break; default: // processes all other characters 50 Console.WriteLine( 51 "Incorrect letter grade entered." + 52 "\nGrade not added to totals." ); 53 break; } // end switch } // end for Console.WriteLine( 60 "\nTotals for each letter grade are:\nA: {0}" + 61 "\nB: {1}\nC: {2}\nD: {3}\nF: {4}", aCount, bCount, 62 cCount, dCount, fCount ); } // end method Main } // end class SwitchTest

 2002 Prentice Hall. All rights reserved. Outline 19 SwitchTest.cs Program Output Enter a letter grade: a Enter a letter grade: A Enter a letter grade: c Enter a letter grade: F Enter a letter grade: z Incorrect letter grade entered. Grade not added to totals. Enter a letter grade: D Enter a letter grade: d Enter a letter grade: B Enter a letter grade: a Enter a letter grade: C Totals for each letter grade are: A: 3 B: 1 C: 2 D: 2 F: 1