 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1 Introduction 5.2 Essentials of Counter-Controlled.

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

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Chapter 5 - Control Structures - Part 2 Outline 5.1Introduction 5.2Essentials of Counter-Controlled Repetition 5.3The for Repetition Structure 5.4Examples.
Control Statements (II) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
Control Structures: Part 2. Introduction Essentials of Counter-Controlled Repetition For / Next Repetition Structure Examples Using the For / Next Structure.
 2003 Prentice Hall, Inc. All rights reserved. 1 Algorithms Computing problems –Solved by executing a series of actions in a specific order Algorithm.
Control Structures in C++ while, do/while, for switch, break, continue.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
 2002 Prentice Hall. All rights reserved. 1 Chapter 3 – Control Structures Outline 3.1 Introduction 3.2 Algorithms 3.3 Pseudocode 3.4Control Structures.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.1Introduction 2.2Algorithms 2.3Pseudocode 2.4Control Structures.
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1 Introduction 5.2 Essentials of Counter-Controlled Repetition.
 2002 Prentice Hall. All rights reserved. 1 Lecture 1 Consolidation of first year work (part 1) Read all the chapters on your own Slides highlight important.
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.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 - Control Structures - Part 2 Outline 5.1Introduction 5.2Essentials of Counter-Controlled Repetition.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 9 - JavaScript: Control Statements II Outline 9.1 Introduction 9.2 Essentials of Counter-Controlled.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
1 4.8The do/while Repetition Structure The do/while repetition structure –Similar to the while structure –Condition for repetition tested after the body.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1 Introduction 5.2 Essentials of Counter-Controlled.
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (Switch, do-while, break) Outline 4.7The.
Chapter 4 C Program Control. Objectives In this chapter, you will learn: –To be able to use the for and do … while repetition statements. –To understand.
 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.
Spring 2005, Gülcihan Özdemir Dağ Lecture 5, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 5 Outline 5.0 Revisiting.
 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.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
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 Chapter 5 – Control Structures: Part 2 Outline 5.1 Introduction 5.2 Essentials of Counter-Controlled.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
C++ Programming Lecture 7 Control Structure I (Selection) – Part II The Hashemite University Computer Engineering Department.
 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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1 Introduction 5.2 Essentials of Counter-Controlled.
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1 Introduction 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.
5.1 Introduction Problem Solving –Requires understanding of: Building blocks Program-construction principles BZUPAGES.COM.
 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 Counter-Controlled Repetition: Example Sentinel-Controlled Repetition:
CSCI/CMPE 4341 Topic: Programming in Python Chapter 4: Control Structures (Part 2) Xiang Lian The University of Texas – Pan American Edinburg, TX
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
A DVANCED P ROGRAMMING C HAPTER 5 & 6: C ONTROL S TRUCTURES Dr Shahriar Bijani Spring 2016.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - JavaScript: Control Structures II Outline 9.1 Introduction 9.2 Essentials of Counter-Controlled.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 6.
 2003 Prentice Hall, Inc. All rights reserved. ECE 2551 Dr. S. Kozaitis Fall Chapter 5 - Control Statements: Part 2 Outline 5.3 for Repetition.
Chapter 4 – C Program Control
Chapter 5- Control Structures: Part 2
Control Statements: Part 2
Chapter 4 - Program Control
Chapter 2.1 Control Structures (Selection)
Chapter 4 – Control Structures Part 1
Chapter 5 – Control Structures: Part 2
- Additional C Statements
Advanced Programming Chapters 5 & 6: Control Structures
Program Control Topics While loop For loop Switch statement
Chapter 6 Control Statements: Part 2
2.6 The if/else Selection Structure
Chapter 5 – Control Structures: Part 2
Dale Roberts, Lecturer IUPUI
Chapter 4 - Program Control
C++ Programming Lecture 7 Control Structure I (Selection) – Part II
Presentation transcript:

 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1 Introduction 5.2 Essentials of Counter-Controlled Repetition 5.3 for Repetition Structure 5.4 Examples Using the for Structure 5.5 switch Multiple-Selection Structure 5.6 do/while Repetition Structure 5.7 Statements break and continue 5.8 Logical and Conditional Operators 5.9 Structured-Programming Summary

 2001 Prentice Hall, Inc. All rights reserved Essentials of Counter Controlled Repetition Counter controlled repetition –Control variable The variable used to determine if the loop continues –Initial value of the control variable –Incrementing/decrementing of the variable –The condition When the looping should continue

 2001 Prentice Hall, Inc. All rights reserved. Outline 3 WhileCounter.cs Program Output 1 // Fig. 5.1: WhileCounter.cs 2 // Counter-controlled repetition. 3 4 using System; 5 6 class WhileCounter 7 { 8 static void Main( string[] args ) 9 { 10 int counter = 1; // initialization while ( counter <= 5 ) // repetition condition 13 { 14 Console.WriteLine( counter ); 15 counter++; // increment } // end while } // end method Main } // end class WhileCounter This is where the counter variable is initialized. It is set to 1. The loop will continue until counter is greater than five (it will stop once it gets to six) The counter is incremented and 1 is added to it

 2001 Prentice Hall, Inc. All rights reserved for Repetition Structure The for repetition structure –Syntax: for (Expression1, Expression2, Expression3) Expression1 = names the control variable –Can contain several variables Expression2 = loop-continuation condition Expression3 = incrementing/decrementing –If Expression1 has several variables, Expression3 must have several variables accordingly –++counter and counter++ are equivalent –Variable scope Expression1 can only be used in the body of the for loop When the loop ends the variable expires

 2001 Prentice Hall, Inc. All rights reserved for Repetition Structure for (int counter = 1; counter <= 5; counter++ ) Initial value of control variableIncrement of control variable Control variablenameFinal value of control variable for keyword Loop-continuation condition Fig. 5.3Components of a typical for header.

 2001 Prentice Hall, Inc. All rights reserved for Repetition Structure counter++ Establish initial value of control variable. Determine if final value of control variable has been reached. counter <= 10 Console.WriteLine ( counter * 10 ); true false int counter = 1 Body of loop (this may be multiple statements) Increment the control variable. Fig. 5.4Flowcharting a typical for repetition structure.

 2001 Prentice Hall, Inc. All rights reserved. Outline 7 ForCounter.cs Program Output 1 // Fig. 5.2: ForCounter.cs 2 // Counter-controlled repetition with the for structure. 3 4 using System; 5 6 class ForCounter 7 { 8 static void Main( string[] args ) 9 { 10 // initialization, repetition condition and incrementing 11 // are all included in the for structure 12 for ( int counter = 1; counter <= 5; counter++ ) 13 Console.WriteLine( counter ); 14 } 15 } This is where the counter variable is initialized. It is set to 1. The loop will continue until counter is greater than five (it will stop once it gets to six) The counter is incremented (1 is added to it)

 2001 Prentice Hall, Inc. All rights reserved Examples Using the for Structure Increment/Decrement –When incrementing In most cases < or <= is used –When decrementing In most cases > or >= is used Message boxes –Buttons OK OKCancel YesNo AbortRetryIgnore YesNoCancel RetryCancel

 2001 Prentice Hall, Inc. All rights reserved Examples Using the for Structure Massages boxes –Icons Exclamation Question Error Information Formatting –(variable : format) Table 5.9 lists some formatting codes

 2001 Prentice Hall, Inc. All rights reserved. Outline 10 Sum.cs Program Output 1 // Fig. 5.5: Sum.cs 2 // Summation with the for structure. 3 4 using System; 5 using System.Windows.Forms; 6 7 class Sum 8 { 9 static void Main( string[] args ) 10 { 11 int sum = 0; for ( int number = 2; number <= 100; number += 2 ) 14 sum += number; MessageBox.Show( "The sum is " + sum, 17 "Sum Even Integers from 2 to 100", 18 MessageBoxButtons.OK, 19 MessageBoxIcon.Information ); } // end method Main } // end class Sum Argument 4: MessageBox Icon (Optional) Argument 3: OK dialog button. (Optional) Argument 2: Title bar string (Optional) Argument 1: Message to display Displays a message box with an OK button Has the message box contain an information icon The counter. It is initialized to 2Once the number is greater than 100 the loop breaks Increments number by 2 every time the loop starts over The caption of the message boxThe title of the message box

 2001 Prentice Hall, Inc. All rights reserved Examples Using the for Structure

 2001 Prentice Hall, Inc. All rights reserved Examples Using the for Structure

 2001 Prentice Hall, Inc. All rights reserved. Outline 13 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 Creates a message box that displays the output with a title of “Compound Interest” has an OK button and an information icon Loops through 10 times starting at 1 and ending at 10, adding 1 to the counter (year) each time Insert a TabFormats amount to have a currency formatting ($0.00)

 2001 Prentice Hall, Inc. All rights reserved. Outline 14 Interest.cs Program Output

 2001 Prentice Hall, Inc. All rights reserved Examples Using the for Structure

 2001 Prentice Hall, Inc. All rights reserved switch Multiple-Selection Structure The switch statement –Constant expressions String Integral –Cases Case ‘x’ : –Use of constant variable cases Empty cases The default case –The break statement Exit the switch statement

 2001 Prentice Hall, Inc. 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 Each of these variables acts as a counter so they are initialized to zero The start of the switch statement. The grade variable is used as the data to be tested for each case. Prompt the user for a grade and store it into the grade variable case ‘A’ is empty so it is the same as case ‘a’ Both cases add one to aCountThe break statement is used to exit the switch statement and not perform the rest of the operations Both case ‘B’ and case ‘b’ add one to the bCount variable A for loop that initializes i to 1, loops 10 times and increments i by one each time

 2001 Prentice Hall, Inc. 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 Both cases add 1 to cCountIf grade equals D or d add one to dCount Add one to fCount if grade equals F or fIf non of the cases are equal to the value of grade then the default case is executed Display the results

 2001 Prentice Hall, Inc. 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

 2001 Prentice Hall, Inc. All rights reserved switch Multiple-Selection Structure Fig. 5.11Flowcharting the switch multiple-selection structure.

 2001 Prentice Hall, Inc. All rights reserved do/while Repetition Structure The while loops vs. the do/while loops –Using a while loop Condition is tested The the action is performed Loop could be skipped altogether –Using a do/while loop Action is performed Then the loop condition is tested Loop must be run though once Always uses brackets ({) to prevent confusion

 2001 Prentice Hall, Inc. All rights reserved. Outline 22 DoWhileLoop.cs Program Output 1 // Fig. 5.12: DoWhileLoop.cs 2 // The do/while repetition structure. 3 4 using System; 5 6 class DoWhileLoop 7 { 8 static void Main( string[] args ) 9 { 10 int counter = 1; do 13 { 14 Console.WriteLine( counter ); 15 counter++; 16 } while ( counter <= 5 ); } // end method Main } // end class DoWhileLoop The counter is initialized to oneThese actions are performed at least one Continue looping as long as counter is less than 6 The incrementing task

 2001 Prentice Hall, Inc. All rights reserved do/while Repetition Structure true false action(s) condition Fig. 5.13Flowcharting the do / while repetition structure.

 2001 Prentice Hall, Inc. All rights reserved Statements break and continue Use –Used to alter the flow of control –The break statement Used to exit a loop early –The continue statement Used to skip the rest of the statements and begin the loop at the first statement in the loop –Programs can be completed without their usage

 2001 Prentice Hall, Inc. All rights reserved. Outline 25 BreakTest.cs 1 // Fig. 5.14: BreakTest.cs 2 // Using the break statement in a for structure. 3 4 using System; 5 using System.Windows.Forms; 6 7 class BreakTest 8 { 9 static void Main( string[] args ) 10 { 11 string output = ""; 12 int count; for ( count = 1; count <= 10; count++ ) 15 { 16 if ( count == 5 ) 17 break; // skip remaining code in loop 18 // if count == output += count + " "; } // end for loop output += "\nBroke out of loop at count = " + count; MessageBox.Show( output, "Demonstrating the break statement", 27 MessageBoxButtons.OK, MessageBoxIcon.Information ); } // end method Main } // end class BreakTest Displays a message box the displays the output, has a title of “demonstrating the break statement,” uses an OK button, and displays an information icon A loop that starts at one, goes to ten, and increments by one If count = 5 then break out of the loopDisplay the last value that the counter was at before it broke

 2001 Prentice Hall, Inc. All rights reserved. Outline 26 BreakTest.cs Program Output

 2001 Prentice Hall, Inc. All rights reserved. Outline 27 ContinueTest.cs 1 // Fig. 5.15: ContinueTest.cs 2 // Using the continue statement in a for structure. 3 4 using System; 5 using System.Windows.Forms; 6 7 class ContinueTest 8 { 9 static void Main( string[] args ) 10 { 11 string output = ""; for ( int count = 1; count <= 10; count++ ) 14 { 15 if ( count == 5 ) 16 continue; // skip remaining code in loop 17 // only if count == output += count + " "; 20 } output += "\nUsed continue to skip printing 5"; MessageBox.Show( output, "Using the continue statement", 25 MessageBoxButtons.OK, MessageBoxIcon.Information ); } // end method Main } // end class ContinueTest A loop that starts at 1, goes to 10, and increments by 1 If count = 5 then continue looping causing the program to skip the rest of the loop Create a message box that displays the output, has the title “using the continue statement,” uses an OK button, and displays an information icon.

 2001 Prentice Hall, Inc. All rights reserved. Outline 28 ContinueTest.cs Program Output

 2001 Prentice Hall, Inc. All rights reserved Logical and Conditional Operators Operators –Logical AND (&) –Conditional AND (&&) –Logical OR (|) –Conditional OR (||) –Logical exclusive OR or XOR (^) –Logical NOT (!) Can be avoided if desired by using other conditional operators Used to add multiple conditions to a statement

 2001 Prentice Hall, Inc. All rights reserved Logical and Conditional Operators

 2001 Prentice Hall, Inc. All rights reserved Logical and Conditional Operators

 2001 Prentice Hall, Inc. All rights reserved. Outline 32 LogicalOperators.cs 1 // Fig. 5.20: LogicalOperators.cs 2 // Demonstrating the logical operators. 3 using System; 4 5 class LogicalOperators 6 { 7 // main entry point for application 8 static void Main( string[] args ) 9 { 10 // testing the conditional AND operator (&&) 11 Console.WriteLine( "Conditional AND (&&)" + 12 "\nfalse && false: " + ( false && false ) + 13 "\nfalse && true: " + ( false && true ) + 14 "\ntrue && false: " + ( true && false ) + 15 "\ntrue && true: " + ( true && true ) ); // testing the conditional OR operator (||) 18 Console.WriteLine( "\n\nConditional OR (||)" + 19 "\nfalse || false: " + ( false || false ) + 20 "\nfalse || true: " + ( false || true ) + 21 "\ntrue || false: " + ( true || false ) + 22 "\ntrue || true: " + ( true || true ) ); // testing the logical AND operator (&) 25 Console.WriteLine( "\n\nLogical AND (&)" + 26 "\nfalse & false: " + ( false & false ) + 27 "\nfalse & true: " + ( false & true ) + 28 "\ntrue & false: " + ( true & false ) + 29 "\ntrue & true: " + ( true & true ) ); 30 Only true if both inputs are trueOnly false if both inputs are false The result is only true if both are true Outputs a truth table for the conditional AND operator (&&) Outputs a truth table for the conditional OR operator (||) Outputs a truth table for the logical AND operator (&)

 2001 Prentice Hall, Inc. All rights reserved. Outline 33 LogicalOperators.cs Program Output 31 // testing the logical OR operator (|) 32 Console.WriteLine( "\n\nLogical OR (|)" + 33 "\nfalse | false: " + ( false | false ) + 34 "\nfalse | true: " + ( false | true ) + 35 "\ntrue | false: " + ( true | false ) + 36 "\ntrue | true: " + ( true | true ) ); // testing the logical exclusive OR operator (^) 39 Console.WriteLine( "\n\nLogical exclusive OR (^)" + 40 "\nfalse ^ false: " + ( false ^ false ) + 41 "\nfalse ^ true: " + ( false ^ true ) + 42 "\ntrue ^ false: " + ( true ^ false ) + 43 "\ntrue ^ true: " + ( true ^ true ) ); // testing the logical NOT operator (!) 46 Console.WriteLine( "\n\nLogical NOT (!)" + 47 "\n!false: " + ( !false ) + 48 "\n!true: " + ( !true ) ); 49 } 50 } Conditional AND (&&) false && false: False false && true: False true && false: False true && true: True Conditional OR (||) false || false: False false || true: True true || false: True true || true: True Returns the opposite as the inputReturns false when the two conditionals are the same If one is true the result is true Outputs a truth table for the logical OR operator (||) Outputs a truth table for the logical exclusive OR operator (||) Outputs a truth table for the logical NOT operator (!)

 2001 Prentice Hall, Inc. All rights reserved. Outline 34 LogicalOperators.cs Program Output Logical AND (&) false & false: False false & true: False true & false: False true & true: True Logical OR (|) false | false: False false | true: True true | false: True true | true: True Logical exclusive OR (^) false ^ false: False false ^ true: True true ^ false: True true ^ true: False Logical NOT (!) !false: True !true: False

 2001 Prentice Hall, Inc. All rights reserved Structured Programming Summary Control Structures –Only one entrance –Only one exit –Building blocks to programming –Allow nesting –Makes code neater and easier to follow –No overlapping structures The goto keyword

 2001 Prentice Hall, Inc. All rights reserved Structured Programming Summary 3 forms of control necessary –Many ways to implement these controls –Sequential (only 1 way) Straight forward programming –Selection (3 ways) if selection (one choice) if/else selection (two choices) switch statement (multiple choices) –Repetition (4 ways) while structure do/while structure for structure foreach structure (chapter 7)

 2001 Prentice Hall, Inc. All rights reserved Structured Programming Summary

 2001 Prentice Hall, Inc. All rights reserved Structured Programming Summary Fig. 5.22C#’s single-entry/single-exit sequence, selection and repetition structures. (part 1) Sequence.

 2001 Prentice Hall, Inc. All rights reserved Structured Programming Summary Fig. 5.22C#’s single-entry/single-exit sequence, selection and repetition structures. (part 2) Selection T F if structure (single selection) else/if structure (double selection) TF switch structure (multiple selections). break T T T F F F.

 2001 Prentice Hall, Inc. All rights reserved Structured Programming Summary Fig. 5.22C#’s single-entry/single-exit sequence, selection and repetition structures. (part 3) Repetition T F while structure T F do/while structure F T for structure/ foreach structure

 2001 Prentice Hall, Inc. All rights reserved Structured Programming Summary

 2001 Prentice Hall, Inc. All rights reserved Structured Programming Summary Fig. 5.24Simplest flowchart.

 2001 Prentice Hall, Inc. All rights reserved Structured Programming Summary Rule 2 Fig. 5.25Repeatedly applying rule 2 of Fig to the simplest flowchart.

 2001 Prentice Hall, Inc. All rights reserved Structured Programming Summary Rule 3 Fig. 5.26Applying rule 3 of Fig to the simplest flowchart.

 2001 Prentice Hall, Inc. All rights reserved Structured Programming Summary Fig. 5.27Stacked, nested and overlapped building blocks. Stacked building blocks Overlapping building blocks (illegal in structured programs) Nested building blocks

 2001 Prentice Hall, Inc. All rights reserved Structured Programming Summary Fig. 5.28Unstructured flowchart.