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

Slides:



Advertisements
Similar presentations
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Advertisements

 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Control Structures: Part 2. Introduction Essentials of Counter-Controlled Repetition For / Next Repetition Structure Examples Using the For / Next Structure.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
 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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 4.
 2002 Prentice Hall. All rights reserved. 1 Outline 3.2Simple Program: Printing a Line of Text 3.3Another Simple Program: Adding Integers 3.7Using a Dialog.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Interest Calculator Application Introducing the For...Next Repetition Statements.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
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 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
 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.
 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.
Problem Solving and Control Statements. Using Exit to Terminate Repetition Statements There are many forms of the Exit statement, designed to terminate.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (Switch, do-while, break) Outline 4.7The.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 8 Dental Payment Application Introducing CheckBox es and Message Dialogs.
 2009 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
CSCI 3327 Visual Basic Chapter 4: Control Statements in Visual Basic (Part 2) UTPA – Fall 2011 Part of the slides is from Dr. John Abraham’s previous.
 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.
University of Palestine software engineering department Introduction to data structures Control Statements: Part 1 instructor: Tasneem Darwish.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
IE 411/511: Visual Programming for Industrial Applications Lecture Notes #5 Control Statements: Part 2.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
Java™ How to Program, Early Objects Version, 8/e © by Pearson Education, Inc. All Rights Reserved.
Sections 5.1 – 5.4 © Copyright by Pearson Education, Inc. All Rights Reserved.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1 Introduction 5.2 Essentials of Counter-Controlled.
5.1 Introduction Problem Solving –Requires understanding of: Building blocks Program-construction principles BZUPAGES.COM.
 2002 Prentice Hall. All rights reserved. 1 Chapter 4 – Control Structures Part 1 Outline Counter-Controlled Repetition: Example Sentinel-Controlled Repetition:
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
CSCI 3327 Visual Basic Chapter 4: Control Statements in Visual Basic (Part 2B) UTPA – Fall 2011 Part of the slides is from Dr. John Abraham’s previous.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - JavaScript: Control Structures II Outline 9.1 Introduction 9.2 Essentials of Counter-Controlled.
Chapter 4 Select … Case Multiple-Selection Statement & Logical Operators 1 © by Pearson Education, Inc. All Rights Reserved. -Edited By Maysoon.
 2007 Pearson Education, Inc. All rights reserved Control Statements: Part2.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved. ECE 2551 Dr. S. Kozaitis Fall Chapter 5 - Control Statements: Part 2 Outline 5.3 for Repetition.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.-Edited By Maysoon Al-Duwais1.
Chapter 4 – C Program Control
Introduction to Visual Basic
Chapter 5- Control Structures: Part 2
Problem Solving and Control Statements: Part 2
Control Statements: Part 2
Control Structures: Part 2
Chapter 4 - Program Control
CSCI 3327 Visual Basic Chapter 4: Control Statements in Visual Basic (Part 2B) UTPA – Fall 2011 Part of the slides is from Dr. John Abraham’s previous.
Chapter 5 – Control Structures: Part 2
Chapter 8 JavaScript: Control Statements, Part 2
Advanced Programming Chapters 5 & 6: Control Structures
Chapter 6 Control Statements: Part 2
Problem Solving and Control Statements
2.6 The if/else Selection Structure
Chapter 5 – Control Structures: Part 2
Chapter 4 - Program Control
Presentation transcript:

 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition 5.3 For / Next Repetition Structure 5.4 Examples Using the For / Next Structure 5.5 Select Case Multiple-Selection Structure 5.6 Do / Loop While Repetition Structure 5.7 Do / Loop Until Repetition Structure 5.8Using the Exit Keyword in a Repetition Structure 5.9 Logical Operators

 2002 Prentice Hall. All rights reserved Essentials of Counter-Controlled Repetition Elements needed –Control variable Used to determine whether loop continues to iterate –Initial value of control variable –Increment (or decrement) Describes how control variable is modified during each iteration –Condition Tests for final value of control variable

 2002 Prentice Hall. All rights reserved. Outline 3 WhileCounter.vb 1 ' Fig. 5.1: WhileCounter.vb 2 ' Using the While structure to demonstrate counter-controlled 3 ' repetition. 4 5 Module modWhileCounter 6 7 Sub Main() 8 9 Dim counter As Integer = 2 ' initialization While (counter <= 10) ' repetition condition 12 Console.Write(counter & " ") 13 counter += 2 ' increment counter 14 End While End Sub ' Main End Module ' modWhileCounter While structure used for repetition Control variable defined and initialized to 2 Control variable incremented by 2 each iteration Condition tests if control variable is less than or equal to final value

 2002 Prentice Hall. All rights reserved For / Next Repetition Structure For / Next counter-controlled repetition –Structure header initializes control variable, specifies final value and increment For keyword begins structure –Followed by control variable initialization To keyword specifies final value Step keyword specifies increment –Optional –Increment defaults to 1 if omitted –May be positive or negative –Next keyword marks end of structure –Executes until control variable greater (or less) than final value

 2002 Prentice Hall. All rights reserved. Outline 5 ForCounter.vb Program Output 1 ' Fig. 5.2: ForCounter.vb 2 ' Using the For/Next structure to demonstrate counter-controlled 3 ' repetition. 4 5 Module modForCounter 6 7 Sub Main() 8 Dim counter As Integer 9 10 ' initialization, repetition condition and 11 ' incrementing are included in For structure 12 For counter = 2 To 10 Step 2 13 Console.Write(counter & " ") 14 Next End Sub ' Main End Module ' modForCounter Control variable initialized to 2 To specifies final value of 10 Step increments counter by 2 each iteration Next marks end of loop

 2002 Prentice Hall. All rights reserved For / Next Repetition Structure Fig. 5.3Components of a typical For/Next header. For counter = 2 To 10 Step 2 For keyword Initial value of control variable Final value of control variable Increment of control variable Control variable name To keyword Step keyword

 2002 Prentice Hall. All rights reserved Examples Using the For / Next Structure Examples –Vary the control variable from 1 to 100 in increments of 1 For i = 1 To 100 For i = 1 To 100 Step 1 –Vary the control variable from 100 to 1 in increments of –1 For i = 100 To 1 Step –1 –Vary the control variable from 7 to 77 in increments of 7 For i = 7 To 77 Step 7 –Vary the control variable from 20 to 2 in increments of –2 For i = 20 To 2 Step -2

 2002 Prentice Hall. All rights reserved Examples Using the For / Next Structure Fig. 5.4Flowcharting a typical For/Next repetition structure. counter = 1 counter < = 10 (implicit) false true Console.WriteLine( counter * 10) counter += 1 (implicit) Establish initial value of control variable Determine if final value of control variable has been reached Body of loop (this can be multiple statements) Increment the control variable

 2002 Prentice Hall. All rights reserved. Outline 9 Sum.vb Program Output 1 ' Fig. 5.5: Sum.vb 2 ' Using For/Next structure to demonstrate summation. 3 4 Imports System.Windows.Forms 5 6 Module modSum 7 8 Sub Main() 9 10 Dim sum = 0, number As Integer ' add even numbers from 2 to For number = 2 To 100 Step 2 14 sum += number 15 Next MessageBox.Show("The sum is " & sum, _ 18 "Sum even integers from 2 to 100", _ 19 MessageBoxButtons.OK, MessageBoxIcon.Information) 20 End Sub ' Main End Module ' modSum Control variable counts by 2, from 2 to 100 Value of number is added in each iteration to determine sum of even numbers Text displayed in dialog Display a MessageBox Indicate button to be OK button Indicate icon to be Information icon Text displayed in title bar

 2002 Prentice Hall. All rights reserved Examples Using the For / Next Structure Fig. 5.6Icons for message dialogs.

 2002 Prentice Hall. All rights reserved Examples Using the For / Next Structure Fig. 5.7Button constants for message dialogs.

 2002 Prentice Hall. All rights reserved. Outline 12 Interest.vb 1 ' Fig. 5.8: Interest.vb 2 ' Calculating compound interest. 3 4 Imports System.Windows.Forms 5 6 Module modInterest 7 8 Sub Main() 9 10 Dim amount, principal As Decimal ' dollar amounts 11 Dim rate As Double ' interest rate 12 Dim year As Integer ' year counter 13 Dim output As String ' amount after each year principal = rate = output = "Year" & vbTab & "Amount on deposit" & vbCrLf ' calculate amount after each year 21 For year = 1 To amount = principal * (1 + rate) ^ year 23 output &= year & vbTab & _ 24 String.Format("{0:C}", amount) & vbCrLf 25 Next ' display output 28 MessageBox.Show(output, "Compound Interest", _ 29 MessageBoxButtons.Ok, MessageBoxIcon.Information) End Sub ' Main End Module ' modInterest Perform calculation to determine amount in account Append year followed by the formatted calculation result and newline character to end of String output Specify C (for “currency”) as formatting code Type Decimal used for precise monetary calculations

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

 2002 Prentice Hall. All rights reserved Examples Using the For / Next Structure Fig. 5.9String formatting codes.

 2002 Prentice Hall. All rights reserved Select Case Multiple-Selection Structure Multiple-Selection Structure –Tests expression separately for each value expression may assume –Select Case keywords begin structure Followed by controlling expression –Compared sequentially with each case –Code in case executes if match is found –Program control proceeds to first statement after structure Case keyword –Specifies each value to test for –Followed by code to execute if test is true –Case Else Optional Executes if no match is found Must be last case in sequence

 2002 Prentice Hall. All rights reserved. Outline 16 SelectTest.vb 1 ' Fig. 5.10: SelectTest.vb 2 ' Using the Select Case structure. 3 4 Module modEnterGrades 5 6 Sub Main() 7 Dim grade As Integer = 0 ' one grade 8 Dim aCount As Integer = 0 ' number of As 9 Dim bCount As Integer = 0 ' number of Bs 10 Dim cCount As Integer = 0 ' number of Cs 11 Dim dCount As Integer = 0 ' number of Ds 12 Dim fCount As Integer = 0 ' number of Fs Console.Write( "Enter a grade, -1 to quit: " ) 15 grade = Console.ReadLine() ' input and process grades 18 While grade <> Select Case grade ' check which grade was input Case 100 ' student scored Console.WriteLine( "Perfect Score!" & vbCrLf & _ 24 "Letter grade: A" & vbCrLf ) 25 aCount += Case 90 To 99 ' student scored Console.WriteLine( "Letter Grade: A" & vbCrLf ) 29 aCount += Case 80 To 89 ' student scored Console.WriteLine( "Letter Grade: B" & vbCrLf ) 33 bCount += 1 34 Select Case begins multiple-selection structure Controlling expression First Case executes if grade is exactly 100 Next Case executes if grade is between 90 and 99, the range being specified with the To keyword

 2002 Prentice Hall. All rights reserved. Outline 17 SelectTest.vb 35 Case 70 To 79 ' student scored Console.WriteLine("Letter Grade: C" & vbCrLf) 37 cCount += Case 60 To 69 ' student scored Console.WriteLine("Letter Grade: D" & vbCrLf) 41 dCount += ' student scored 0 or (10 points for attendance) 44 Case 0, 10 To Console.WriteLine("Letter Grade: F" & vbCrLf) 46 fCount += Case Else ' alert user that invalid grade was entered 51 Console.WriteLine("Invalid Input. " & _ 52 "Please enter a valid grade." & vbCrLf) 53 End Select Console.Write("Enter a grade, -1 to quit: ") 56 grade = Console.ReadLine() 57 End While ' display count of each letter grade 60 Console.WriteLine(vbCrLf & _ 61 "Totals for each letter grade are: " & vbCrLf & _ 62 "A: " & aCount & vbCrLf & "B: " & bCount _ 63 & vbCrLf & "C: " & cCount & vbCrLf & "D: " & _ 64 dCount & vbCrLf & "F: " & fCount) End Sub ' Main End Module ' modEnterGrades Optional Case Else executes if no match occurs with previous Case s End Select marks end of structure

 2002 Prentice Hall. All rights reserved. Outline 18 Program Output Enter a grade: 84 Letter Grade: B Enter a grade: 100 Perfect Score! Letter grade : A+ Enter a grade: 7 Invalid Input. Please enter a valid grade. Enter a grade: 95 Letter Grade: A Enter a grade: 78 Letter Grade: C Totals for each letter grade are: A: 2 B: 1 C: 1 D: 0 F: 0

 2002 Prentice Hall. All rights reserved Select Case Multiple Fig. 5.11Flowcharting the Select Case multiple-selection structure. Case a Case b Case z Case Else action(s) false Case a action(s) Case b action(s) Case z action(s) true

 2002 Prentice Hall. All rights reserved Do / Loop While Repetition Structure Do / Loop While Repetition Structure –Similar to While and Do / While –Loop-continuation condition tested after body executes Loop body always executed at least once –Begins with keyword Do –Ends with keywords Loop While followed by condition

 2002 Prentice Hall. All rights reserved. Outline 21 DoWhile.vb Program Output 1 ' Fig. 5.12: DoWhile.vb 2 ' Demonstrating the Do/Loop While repetition structure. 3 4 Module modDoWhile 5 6 Sub Main() 7 8 Dim counter As Integer = ' print values 1 to 5 11 Do 12 Console.Write(counter & " ") 13 counter += 1 14 Loop While (counter <= 5) End Sub ' Main End Module ' modDoWhile Do keyword begins structure Loop While ends structure Condition tested after body executes

 2002 Prentice Hall. All rights reserved Do / Loop Until Repetition Structure Fig. 5.13Flowcharting the Do/Loop While repetition structure. true false condition action(s)

 2002 Prentice Hall. All rights reserved Do / Loop Until Repetition Structure Do / Loop Until Repetition Structure –Similar to Do Until / Loop structure –Loop-continuation condition tested after body executes Loop body always executed at least once

 2002 Prentice Hall. All rights reserved. Outline 24 LoopUntil.vb Program Output 1 ' Fig. 5.14: LoopUntil.vb 2 ' Using Do/Loop Until repetition structure 3 4 Module modLoopUntil 5 6 Sub Main() 7 8 Dim counter As Integer = ' print values 1 to 5 11 Do 12 Console.Write(counter & " ") 13 counter += 1 14 Loop Until counter > End Sub ' Main End Module ' modLoopUntil Condition tested after body executes

 2002 Prentice Hall. All rights reserved Do / Loop Until Repetition Structure Fig. 5.15Flowcharting the Do/Loop Until repetition structure. true false condition action(s)

 2002 Prentice Hall. All rights reserved Using the Exit Keyword in a Repetition Structure Exit Statements –Alter the flow of control Cause immediate exit from a repetition structure –Exit Do Executed in Do structures –Exit For Executed in For structures –Exit While Executed in While structures

 2002 Prentice Hall. All rights reserved. Outline 27 ExitTest.vb 1 ' Fig. 5.16: ExitTest.vb 2 ' Using the Exit keyword in repetition structures. 3 4 Imports System.Windows.Forms 5 6 Module modExitTest 7 8 Sub Main() 9 Dim output As String 10 Dim counter As Integer For counter = 1 To ' skip remaining code in loop only if counter = 3 15 If counter = 3 Then 16 Exit For 17 End If Next output = "counter = " & counter & _ 22 " after exiting For/Next structure" & vbCrLf Do Until counter > ' skip remaining code in loop only if counter = 5 27 If counter = 5 Then 28 Exit Do 29 End If counter += 1 32 Loop 33 Loop specified to execute 10 times Exit For statement executes when condition is met, causing loop to exit Program control proceeds to first statement after the structure counter is 3 when loop starts, specified to execute until it is greater than 10 Exit Do executes when counter is 5, causing loop to exit

 2002 Prentice Hall. All rights reserved. Outline 28 Program Output 34 output &= "counter = " & counter & _ 35 " after exiting Do Until/Loop structure" & vbCrLf While counter <= ' skip remaining code in loop only if counter = 7 40 If counter = 7 Then 41 Exit While 42 End If counter += 1 45 End While output &= "counter = " & counter & _ 48 " after exiting While structure" MessageBox.Show(output, "Exit Test", _ 51 MessageBoxButtons.OK, MessageBoxIcon.Information) 52 End Sub ' Main End Module ' modExitTest counter is 5 when loop starts, specified to execute while less than or equal to 10 Exit While executes when counter is 7, causing loop to exit

 2002 Prentice Hall. All rights reserved Logical Operators Used to form complex conditions by combining simple ones –Short-circuit evaluation Execute only until truth or falsity is known –AndAlso operator Returns true if and only if both conditions are true –OrElse operator Returns true if either or both of two conditions are true

 2002 Prentice Hall. All rights reserved Logical Operators (II) Logical Operators without short-circuit evaluation –And and Or Similar to AndAlso and OrElse respectively Always execute both of their operands Used when an operand has a side effect –Condition makes a modification to a variable –Should be avoided to reduce subtle errors –Xor Returns true if and only if one operand is true and the other false

 2002 Prentice Hall. All rights reserved Logical Operators (III) Logical Negation –Not Used to reverse the meaning of a condition Unary operator –Requires one operand Can usually be avoided by expressing a condition differently

 2002 Prentice Hall. All rights reserved Logical Operators Fig. 5.17Truth table for the AndAlso (logical AND) operator.

 2002 Prentice Hall. All rights reserved Logical Operators Fig. 5.18Truth table for the OrElse (logical OR) operator.

 2002 Prentice Hall. All rights reserved Logical Operators Fig. 5.19Truth table for the boolean logical exclusive OR (Xor) operator. Fig. 5.20Truth table for operator Not (logical NOT).

 2002 Prentice Hall. All rights reserved. Outline 35 Program Output

 2002 Prentice Hall. All rights reserved Logical Operators Fig. 5.22Precedence and associativity of the operators discussed so far.