SlideShare uma empresa Scribd logo
1 de 48
Chapter 5 – Control Structures: Part 2 Outline 5.1 Introduction 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.8 Using the  Exit  Keyword in a Repetition Structure 5.9   Logical Operators 5.10   Structured Programming Summary
5.1 Introduction ,[object Object],[object Object],[object Object],[object Object]
5.2 Essentials of Counter-Controlled Repetition ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
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 10  11  While  (counter <=  10 )  ' repetition condition 12  Console.Write(counter & &quot; &quot;) 13  counter +=  2  ' increment counter 14  End While 15  16  End Sub  ' Main 17 18  End Module  ' modWhileCounter 2 4 6 8 10 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
5.3  For / Next  Repetition Structure ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
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 &  &quot; &quot; ) 14  Next 15 16  End Sub  ' Main 17 18  End Module  ' modForCounter 2 4 6 8 10 Control variable initialized to 2 To  specifies final value of 10 Step  increments counter by 2 each iteration Next  marks end of loop
5.3  For / Next  Repetition Structure Fig. 5.3 Components 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
5.4  Examples Using the  For / Next  Structure   ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
5.4 Examples Using the  For / Next  Structure Fig. 5.4 Flowcharting 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
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 11  12  ' add even numbers from 2 to 100 13  For  number =  2   To   100  Step   2 14  sum += number 15  Next 16  17  MessageBox.Show( &quot;The sum is &quot;  & sum, _ 18  &quot;Sum even integers from 2 to 100&quot; , _ 19  MessageBoxButtons. OK , MessageBoxIcon. Information ) 20  End Sub   ' Main 21  22  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
5.4 Examples Using the  For / Next  Structure Fig. 5.6 Icons for message dialogs.
5.4 Examples Using the  For / Next  Structure Fig. 5.7 Button constants for message dialogs.
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 14  15  principal =  1000.00 16  rate =  0.05 17  18  output =  &quot;Year&quot;  &  vbTab  &  &quot;Amount on deposit&quot;  &  vbCrLf 19  20  ' calculate amount after each year 21  For  year =  1   To  10 22  amount = principal * ( 1  + rate) ^ year 23  output &= year &  vbTab  & _ 24  String.Format( &quot;{0:C}&quot; , amount) &  vbCrLf 25  Next 26  27  ' display output 28  MessageBox.Show(output,  &quot;Compound Interest&quot; , _ 29  MessageBoxButtons. Ok , MessageBoxIcon. Information ) 30  31  End Sub  ' Main 32  33  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
Program Output
5.4 Examples Using the  For / Next  Structure Fig. 5.9 String formatting codes.
5.5  Select   Case  Multiple-Selection Structure ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
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 13  14  Console.Write( &quot;Enter a grade, -1 to quit: &quot; ) 15  grade = Console.ReadLine() 16  17  ' input and process grades 18  While  grade <>  -1 19  20  Select   Case  grade  ' check which grade was input 21  22  Case   100   ' student scored 100 23  Console.WriteLine( &quot;Perfect Score!&quot;  &  vbCrLf  & _ 24  &quot;Letter grade: A&quot;  &  vbCrLf ) 25  aCount +=  1 26  27  Case   90   To   99   ' student scored 90-99 28  Console.WriteLine( &quot;Letter Grade: A&quot;  &  vbCrLf ) 29  aCount +=  1 30  31  Case   80   To   89   ' student scored 80-89 32  Console.WriteLine( &quot;Letter Grade: B&quot;  &  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
SelectTest.vb 35  Case   70   To   79   ' student scored 70-79 36  Console.WriteLine( &quot;Letter Grade: C&quot;  &  vbCrLf ) 37  cCount +=  1 38  39  Case   60   To   69   ' student scored 60-69 40  Console.WriteLine( &quot;Letter Grade: D&quot;  &  vbCrLf ) 41  dCount +=  1 42  43  ' student scored 0 or 10-59 (10 points for attendance) 44  Case   0 ,  10   To   59 45  Console.WriteLine( &quot;Letter Grade: F&quot;  &  vbCrLf ) 46  fCount +=  1 47  48  Case   Else 49  50  ' alert user that invalid grade was entered 51  Console.WriteLine( &quot;Invalid Input. &quot;  & _ 52  &quot;Please enter a valid grade.&quot;  &  vbCrLf ) 53  End   Select 54  55  Console.Write( &quot;Enter a grade, -1 to quit: &quot; ) 56  grade = Console.ReadLine() 57  End   While 58  59  ' display count of each letter grade 60  Console.WriteLine( vbCrLf  & _ 61  &quot;Totals for each letter grade are: &quot;  &  vbCrLf  & _ 62  &quot;A: &quot;  & aCount &  vbCrLf  &  &quot;B: &quot;  & bCount _ 63  &  vbCrLf  &  &quot;C: &quot;  & cCount &  vbCrLf  &  &quot;D: &quot;  & _ 64  dCount &  vbCrLf  &  &quot;F: &quot;  & fCount) 65  66  End   Sub  ' Main 67  68  End   Module  ' modEnterGrades Optional  Case Else  executes if no match occurs with previous  Case s End Select  marks end of structure
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
5.5  Select   Case  Multiple Fig. 5.11 Flowcharting the Select Case multiple-selection structure. Case   a Case   b Case   z . . . Case Else   action(s) false false false Case  a action(s) Case  b action(s) Case  z action(s) true true true
5.6  Do / Loop   While  Repetition Structure ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
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  =  1 9  10  ' print values 1 to 5 11  Do  12  Console.Write(counter &  &quot; &quot; ) 13  counter +=  1 14  Loop While  (counter <=  5 ) 15  16  End Sub  ' Main 17 18  End Module  ' modDoWhile 1 2 3 4 5 Do  keyword begins structure Loop While  ends structure Condition tested after body executes
5.7  Do / Loop  Until Repetition Structure Fig. 5.13 Flowcharting the Do/Loop While repetition structure. true false action(s) condition
5.7  Do / Loop   Until  Repetition Structure ,[object Object],[object Object],[object Object],[object Object]
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  =  1 9  10  ' print values 1 to 5 11  Do 12  Console.Write(counter &  &quot; &quot; ) 13  counter +=  1 14  Loop   Until  counter >  5 15  16  End   Sub  ' Main 17 18  End Module   ' modLoopUntil 1 2 3 4 5 6 7 8 9 Condition tested after body executes
5.7  Do / Loop   Until  Repetition Structure Fig. 5.15 Flowcharting the Do/Loop Until repetition structure. true false action(s) condition
5.8  Using the  Exit  Keyword in a Repetition Structure ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
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 11  12  For  counter =  1   To   10 13  14  ' skip remaining code in loop only if counter = 3 15  If  counter =  3   Then 16  Exit   For 17  End If 18  19  Next 20  21  output =  &quot;counter = &quot;  & counter & _ 22  &quot; after exiting For/Next structure&quot;  &  vbCrLf 23  24  Do Until  counter >  10 25  26  ' skip remaining code in loop only if counter = 5 27  If  counter =  5   Then 28  Exit   Do 29  End   If 30  31  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
Program Output 34  output   &=   &quot;counter = &quot;   & counter & _ 35  &quot; after exiting Do Until/Loop structure&quot;  &  vbCrLf 36  37  While   counter <=   10 38  39  ' skip remaining code in loop only if counter = 7 40  If   counter =   7   Then 41  Exit   While 42  End If 43  44  counter +=   1 45  End While 46  47  output &=   &quot;counter = &quot;   & counter & _ 48  &quot; after exiting While structure&quot; 49  50  MessageBox.Show(output,   &quot;Exit Test&quot; , _ 51  MessageBoxButtons. OK ,  MessageBoxIcon. Information ) 52  End Sub  ' Main 53  54  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
5.9  Logical Operators ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
5.9  Logical Operators (II) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
5.9  Logical Operators (III) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
5.9 Logical Operators Fig. 5.17 Truth table for the AndAlso (logical AND) operator.
5.9 Logical Operators Fig. 5.18 Truth table for the OrElse (logical OR) operator.
5.9 Logical Operators Fig. 5.19 Truth table for the boolean logical exclusive OR (Xor) operator. Fig. 5.20 Truth table for operator Not (logical NOT).
LogicalOperator.vb 1  ' Fig. 5.21: LogicalOperator.vb 2  ' Using logical operators. 3  4  Public Class  FrmLogicalOperator 5  Inherits  System.Windows.Forms.Form 6  7  ' Visual Studio .NET generated code 8  9  Private Sub  FrmLogicalOperator_Load( _ 10  ByVal  sender  As  System.Object, _ 11  ByVal  e  As  System.EventArgs)  Handles   MyBase .Load 12  13  lblAndAlso.Text =   &quot;AndAlso&quot;  &  vbCrLf  &  vbCrLf  & _ 14  &quot;False AndAlso False: &quot;  & ( False AndAlso False ) &   _ 15  vbCrLf  &  &quot;False AndAlso True: &quot;  & _ 16  ( False AndAlso True ) &  vbCrLf  & _ 17  &quot;True AndAlso False: &quot;  & ( True AndAlso False ) & _ 18  vbCrLf  &  &quot;True AndAlso True: &quot;  & ( True AndAlso True ) 19  20  lblOrElse.Text =  &quot;OrElse&quot;  &  vbCrLf  &  vbCrLf  & _ 21  &quot;False OrElse False: &quot;  & ( False OrElse False ) & _ 22  vbCrLf  &  &quot;False OrElse True: &quot;  & ( False OrElse True ) & _ 23  vbCrLf  &  &quot;True OrElse False: &quot;  & ( True OrElse False ) & _ 24  vbCrLf  &  &quot;True OrElse True: &quot;  & ( True OrElse True ) 25  26  lblAnd.Text =  &quot;And&quot;  &  vbCrLf  &  vbCrLf  & _ 27  &quot;False And False: &quot;  & ( False And False ) &  vbCrLf  & _ 28  &quot;False And True: &quot;  & ( False And True ) &  vbCrLf  & _ 29  &quot;True And False: &quot;  & ( True And False ) &  vbCrLf  & _ 30  &quot;True And True: &quot;  & ( True And True ) 31  Code generated by Visual Studio represented by this comment Demonstrate  AndAlso  operator Demonstrate  OrElse  operator Demonstrate  And  operator
LogicalOperator.vb 32  lblOr.Text =  &quot;Or&quot;  &  vbCrLf  & _ 33  vbCrLf  &  &quot;False Or False: &quot;  & ( False Or False ) & _ 34  vbCrLf  &  &quot;False Or True: &quot;  & ( False Or True ) & _ 35  vbCrLf  &  &quot;True Or False: &quot;  & ( True Or False ) & _ 36  vbCrLf  &  &quot;True Or True: &quot;  & ( True Or True ) 37  38  lblXor.Text =  &quot;Xor&quot;  &  vbCrLf  & _ 39  vbCrLf  &  &quot;False Xor False: &quot;  & ( False Xor False ) & _ 40  vbCrLf  &  &quot;False Xor True: &quot;  & ( False Xor True ) & _ 41  vbCrLf  &  &quot;True Xor False: &quot;  & ( True Xor False ) & _ 42  vbCrLf  &  &quot;True Xor True: &quot;  & ( True Xor True ) 43  44  lblNot.Text =  &quot;Not&quot;  &  vbCrLf  &  vbCrLf  & _ 45  &quot;Not False: &quot;  & ( Not False ) &  vbCrLf  &  &quot;Not True: &quot;  & _ 46  ( Not True ) 47  48  End Sub   ' FrmLogicalOperator_Load 49  50  End Class   ' FrmLogicalOperator Demonstrate  Or  operator Demonstrate  Xor  operator Demonstrate  Not  operator
Program Output
5.9 Logical Operators Fig. 5.22 Precedence and associativity of the operators discussed so far.
5.10  Structured Programming Summary   ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
5.10 Structured Programming Summary Fig. 5.23 Visual Basic’s single-entry/single-exit sequence and selection structures. Sequence . . . Selection If / Then  structure (single selection) T F If / Then / Else  structure (double selection) T F . . . Select   Case  structure (multiple selection)
5.10 Structured Programming Summary Fig. 5.24 Visual Basic’s single-entry/single-exit repetition structures. While  structure  T F F T For / Next  structure  T F Do / Loop   Until  structure  Do / Loop   While  structure  F T Repetition
5.10 Structured Programming Summary Fig. 5.24 Visual Basic’s single-entry/single-exit repetition structures. Do   While / Loop  structure  T F Do   Until / Loop  structure  F T F T For   Each / Next  structure  Repetition
5.10 Structured Programming Summary Fig. 5.25 Rules for forming structured programs.
5.10 Structured Programming Summary Fig. 5.26 Simplest flowchart. . . . Rule 2 Rule 2 Rule 2 Fig. 5.27 Repeatedly applying rule 2 of Fig. 5.25 to the simplest flowchart.
5.10 Structured Programming Summary Fig. 5.28 Applying rule 3 of Fig. 5.25 to the simplest flowchart. Rule 3 Rule 3
5.10 Structured Programming Summary Fig. 5.29 Stacked, nested and overlapped building blocks. Nested building blocks  Stacked building blocks  Overlapping building blocks (Illegal in structured programs)
5.10 Structured Programming Summary Fig. 5.30 Unstructured flowchart.

Mais conteúdo relacionado

Mais procurados

Flowchart pseudocode-examples
Flowchart pseudocode-examplesFlowchart pseudocode-examples
Flowchart pseudocode-examplesGautam Roy
 
escape sequences and substitution markers
escape sequences and substitution markersescape sequences and substitution markers
escape sequences and substitution markersMicheal Ogundero
 
Unit 3 Foc
Unit  3 FocUnit  3 Foc
Unit 3 FocJAYA
 
Pseudocode algorithim flowchart
Pseudocode algorithim flowchartPseudocode algorithim flowchart
Pseudocode algorithim flowchartfika sweety
 
Program design techniques
Program design techniquesProgram design techniques
Program design techniquesfika sweety
 
Introduction to flowchart
Introduction to flowchartIntroduction to flowchart
Introduction to flowchartJordan Delacruz
 
Algorithmsandflowcharts1
Algorithmsandflowcharts1Algorithmsandflowcharts1
Algorithmsandflowcharts1luhkahreth
 
Loop structures chpt_6
Loop structures chpt_6Loop structures chpt_6
Loop structures chpt_6cmontanez
 
Android tutorials7 calulator_improve
Android tutorials7 calulator_improveAndroid tutorials7 calulator_improve
Android tutorials7 calulator_improveVlad Kolesnyk
 
Best Techniques To Design Programs - Program Designing Techniques
Best Techniques To Design Programs - Program Designing TechniquesBest Techniques To Design Programs - Program Designing Techniques
Best Techniques To Design Programs - Program Designing TechniquesTech
 
Introduction to Basic C programming 02
Introduction to Basic C programming 02Introduction to Basic C programming 02
Introduction to Basic C programming 02Wingston
 
pseudocode and Flowchart
pseudocode and Flowchartpseudocode and Flowchart
pseudocode and FlowchartALI RAZA
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAAiman Hud
 

Mais procurados (20)

Flowchart pseudocode-examples
Flowchart pseudocode-examplesFlowchart pseudocode-examples
Flowchart pseudocode-examples
 
selection structures
selection structuresselection structures
selection structures
 
escape sequences and substitution markers
escape sequences and substitution markersescape sequences and substitution markers
escape sequences and substitution markers
 
Unit 3 Foc
Unit  3 FocUnit  3 Foc
Unit 3 Foc
 
Pseudocode algorithim flowchart
Pseudocode algorithim flowchartPseudocode algorithim flowchart
Pseudocode algorithim flowchart
 
Pseudocode
PseudocodePseudocode
Pseudocode
 
Grade 10 flowcharting
Grade 10  flowchartingGrade 10  flowcharting
Grade 10 flowcharting
 
Program design techniques
Program design techniquesProgram design techniques
Program design techniques
 
Introduction to flowchart
Introduction to flowchartIntroduction to flowchart
Introduction to flowchart
 
Program sba
Program sbaProgram sba
Program sba
 
Unit 3
Unit 3Unit 3
Unit 3
 
Algorithmsandflowcharts1
Algorithmsandflowcharts1Algorithmsandflowcharts1
Algorithmsandflowcharts1
 
Loop structures chpt_6
Loop structures chpt_6Loop structures chpt_6
Loop structures chpt_6
 
Android tutorials7 calulator_improve
Android tutorials7 calulator_improveAndroid tutorials7 calulator_improve
Android tutorials7 calulator_improve
 
Best Techniques To Design Programs - Program Designing Techniques
Best Techniques To Design Programs - Program Designing TechniquesBest Techniques To Design Programs - Program Designing Techniques
Best Techniques To Design Programs - Program Designing Techniques
 
Algorithm and psuedocode
Algorithm and psuedocodeAlgorithm and psuedocode
Algorithm and psuedocode
 
Introduction to Basic C programming 02
Introduction to Basic C programming 02Introduction to Basic C programming 02
Introduction to Basic C programming 02
 
Pseudocode
PseudocodePseudocode
Pseudocode
 
pseudocode and Flowchart
pseudocode and Flowchartpseudocode and Flowchart
pseudocode and Flowchart
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 

Destaque

Programming learning: a hierarchical model based diagnosis approach
Programming learning: a hierarchical model based diagnosis approachProgramming learning: a hierarchical model based diagnosis approach
Programming learning: a hierarchical model based diagnosis approachWellington Pinheiro
 
Csphtp1 05
Csphtp1 05Csphtp1 05
Csphtp1 05HUST
 
SE - Software Requirements
SE - Software RequirementsSE - Software Requirements
SE - Software RequirementsJomel Penalba
 
Ch5 - Project Management
Ch5 - Project ManagementCh5 - Project Management
Ch5 - Project ManagementJomel Penalba
 
Control structures ii
Control structures ii Control structures ii
Control structures ii Ahmad Idrees
 
04 control structures 1
04 control structures 104 control structures 1
04 control structures 1Jomel Penalba
 
Control structures selection
Control structures   selectionControl structures   selection
Control structures selectionOnline
 
Control Structures
Control StructuresControl Structures
Control StructuresGhaffar Khan
 
Control Structures in Visual Basic
Control Structures in  Visual BasicControl Structures in  Visual Basic
Control Structures in Visual BasicTushar Jain
 
Requirements Engineering Process
Requirements Engineering ProcessRequirements Engineering Process
Requirements Engineering ProcessJomel Penalba
 
Divide by N clock
Divide by N clockDivide by N clock
Divide by N clockMantra VLSI
 

Destaque (17)

Programming learning: a hierarchical model based diagnosis approach
Programming learning: a hierarchical model based diagnosis approachProgramming learning: a hierarchical model based diagnosis approach
Programming learning: a hierarchical model based diagnosis approach
 
Csphtp1 05
Csphtp1 05Csphtp1 05
Csphtp1 05
 
Business hardware
Business hardwareBusiness hardware
Business hardware
 
Crm
CrmCrm
Crm
 
SE - Software Requirements
SE - Software RequirementsSE - Software Requirements
SE - Software Requirements
 
Ch5 - Project Management
Ch5 - Project ManagementCh5 - Project Management
Ch5 - Project Management
 
06 procedures
06 procedures06 procedures
06 procedures
 
Control structures ii
Control structures ii Control structures ii
Control structures ii
 
SE - System Models
SE - System ModelsSE - System Models
SE - System Models
 
12 gui concepts 1
12 gui concepts 112 gui concepts 1
12 gui concepts 1
 
04 control structures 1
04 control structures 104 control structures 1
04 control structures 1
 
Control structures selection
Control structures   selectionControl structures   selection
Control structures selection
 
Control Structures
Control StructuresControl Structures
Control Structures
 
Control Structures in Visual Basic
Control Structures in  Visual BasicControl Structures in  Visual Basic
Control Structures in Visual Basic
 
Requirements Engineering Process
Requirements Engineering ProcessRequirements Engineering Process
Requirements Engineering Process
 
Divide by N clock
Divide by N clockDivide by N clock
Divide by N clock
 
Counters
CountersCounters
Counters
 

Semelhante a 05 control structures 2

Chapter 3 Control structures.ppt
Chapter 3 Control structures.pptChapter 3 Control structures.ppt
Chapter 3 Control structures.pptRahulBorate10
 
control statements of clangauge (ii unit)
control statements of clangauge (ii unit)control statements of clangauge (ii unit)
control statements of clangauge (ii unit)Prashant Sharma
 
Mid term sem 2 1415 sol
Mid term sem 2 1415 solMid term sem 2 1415 sol
Mid term sem 2 1415 solIIUM
 
Csphtp1 04
Csphtp1 04Csphtp1 04
Csphtp1 04HUST
 
Control Statement.ppt
Control Statement.pptControl Statement.ppt
Control Statement.pptsanjay
 
Md university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab newMd university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab neweyavagal
 
Md university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab newMd university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab newLast7693
 
Md university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab newMd university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab newscottbrownnn
 
Lecture#5 Operators in C++
Lecture#5 Operators in C++Lecture#5 Operators in C++
Lecture#5 Operators in C++NUST Stuff
 
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo JobyC++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo JobyGrejoJoby1
 
Java căn bản - Chapter6
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6Vince Vo
 
C# Tutorial MSM_Murach chapter-05-slides
C# Tutorial MSM_Murach chapter-05-slidesC# Tutorial MSM_Murach chapter-05-slides
C# Tutorial MSM_Murach chapter-05-slidesSami Mut
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in Csana shaikh
 
Cmis 102 Success Begins / snaptutorial.com
Cmis 102 Success Begins / snaptutorial.comCmis 102 Success Begins / snaptutorial.com
Cmis 102 Success Begins / snaptutorial.comWilliamsTaylorza48
 

Semelhante a 05 control structures 2 (20)

Chapter 3 Control structures.ppt
Chapter 3 Control structures.pptChapter 3 Control structures.ppt
Chapter 3 Control structures.ppt
 
control statements of clangauge (ii unit)
control statements of clangauge (ii unit)control statements of clangauge (ii unit)
control statements of clangauge (ii unit)
 
Mid term sem 2 1415 sol
Mid term sem 2 1415 solMid term sem 2 1415 sol
Mid term sem 2 1415 sol
 
Lecture 1
Lecture 1Lecture 1
Lecture 1
 
Lecture 1
Lecture 1Lecture 1
Lecture 1
 
Python Control structures
Python Control structuresPython Control structures
Python Control structures
 
Csphtp1 04
Csphtp1 04Csphtp1 04
Csphtp1 04
 
Control Statement.ppt
Control Statement.pptControl Statement.ppt
Control Statement.ppt
 
Md university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab newMd university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab new
 
Ch05
Ch05Ch05
Ch05
 
Vb (2)
Vb (2)Vb (2)
Vb (2)
 
Md university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab newMd university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab new
 
Md university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab newMd university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab new
 
Lecture#5 Operators in C++
Lecture#5 Operators in C++Lecture#5 Operators in C++
Lecture#5 Operators in C++
 
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo JobyC++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
 
Control All
Control AllControl All
Control All
 
Java căn bản - Chapter6
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6
 
C# Tutorial MSM_Murach chapter-05-slides
C# Tutorial MSM_Murach chapter-05-slidesC# Tutorial MSM_Murach chapter-05-slides
C# Tutorial MSM_Murach chapter-05-slides
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
 
Cmis 102 Success Begins / snaptutorial.com
Cmis 102 Success Begins / snaptutorial.comCmis 102 Success Begins / snaptutorial.com
Cmis 102 Success Begins / snaptutorial.com
 

Mais de Jomel Penalba

Copy of business hardware
Copy of business hardwareCopy of business hardware
Copy of business hardwareJomel Penalba
 
Business functions and supply chains
Business functions and supply chainsBusiness functions and supply chains
Business functions and supply chainsJomel Penalba
 
Laboratory activity 3 b3
Laboratory activity 3 b3Laboratory activity 3 b3
Laboratory activity 3 b3Jomel Penalba
 
Laboratory activity 3 b2
Laboratory activity 3 b2Laboratory activity 3 b2
Laboratory activity 3 b2Jomel Penalba
 
Laboratory activity 3 b1
Laboratory activity 3 b1Laboratory activity 3 b1
Laboratory activity 3 b1Jomel Penalba
 
Software process models
Software process modelsSoftware process models
Software process modelsJomel Penalba
 
03 intro to vb programming
03 intro to vb programming03 intro to vb programming
03 intro to vb programmingJomel Penalba
 
02 intro to vb-net ide
02 intro to vb-net ide02 intro to vb-net ide
02 intro to vb-net ideJomel Penalba
 
Soft Eng - Software Process
Soft  Eng - Software ProcessSoft  Eng - Software Process
Soft Eng - Software ProcessJomel Penalba
 
Soft Eng - Introduction
Soft Eng - IntroductionSoft Eng - Introduction
Soft Eng - IntroductionJomel Penalba
 
Planning Your Multimedia Web Site
Planning Your Multimedia Web SitePlanning Your Multimedia Web Site
Planning Your Multimedia Web SiteJomel Penalba
 
Introduction To Multimedia
Introduction To MultimediaIntroduction To Multimedia
Introduction To MultimediaJomel Penalba
 

Mais de Jomel Penalba (14)

Copy of business hardware
Copy of business hardwareCopy of business hardware
Copy of business hardware
 
Chapter 1
Chapter 1Chapter 1
Chapter 1
 
Business functions and supply chains
Business functions and supply chainsBusiness functions and supply chains
Business functions and supply chains
 
Laboratory activity 3 b3
Laboratory activity 3 b3Laboratory activity 3 b3
Laboratory activity 3 b3
 
Laboratory activity 3 b2
Laboratory activity 3 b2Laboratory activity 3 b2
Laboratory activity 3 b2
 
Laboratory activity 3 b1
Laboratory activity 3 b1Laboratory activity 3 b1
Laboratory activity 3 b1
 
Software process models
Software process modelsSoftware process models
Software process models
 
03 intro to vb programming
03 intro to vb programming03 intro to vb programming
03 intro to vb programming
 
02 intro to vb-net ide
02 intro to vb-net ide02 intro to vb-net ide
02 intro to vb-net ide
 
01 intro to vb-net
01 intro to vb-net01 intro to vb-net
01 intro to vb-net
 
Soft Eng - Software Process
Soft  Eng - Software ProcessSoft  Eng - Software Process
Soft Eng - Software Process
 
Soft Eng - Introduction
Soft Eng - IntroductionSoft Eng - Introduction
Soft Eng - Introduction
 
Planning Your Multimedia Web Site
Planning Your Multimedia Web SitePlanning Your Multimedia Web Site
Planning Your Multimedia Web Site
 
Introduction To Multimedia
Introduction To MultimediaIntroduction To Multimedia
Introduction To Multimedia
 

Último

Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Bhuvaneswari Subramani
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontologyjohnbeverley2021
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...apidays
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxRemote DBA Services
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 

Último (20)

Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 

05 control structures 2

  • 1. Chapter 5 – Control Structures: Part 2 Outline 5.1 Introduction 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.8 Using the Exit Keyword in a Repetition Structure 5.9   Logical Operators 5.10   Structured Programming Summary
  • 2.
  • 3.
  • 4. 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 10 11 While (counter <= 10 ) ' repetition condition 12 Console.Write(counter & &quot; &quot;) 13 counter += 2 ' increment counter 14 End While 15 16 End Sub ' Main 17 18 End Module ' modWhileCounter 2 4 6 8 10 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
  • 5.
  • 6. 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 & &quot; &quot; ) 14 Next 15 16 End Sub ' Main 17 18 End Module ' modForCounter 2 4 6 8 10 Control variable initialized to 2 To specifies final value of 10 Step increments counter by 2 each iteration Next marks end of loop
  • 7. 5.3 For / Next Repetition Structure Fig. 5.3 Components 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
  • 8.
  • 9. 5.4 Examples Using the For / Next Structure Fig. 5.4 Flowcharting 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
  • 10. 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 11 12 ' add even numbers from 2 to 100 13 For number = 2 To 100 Step 2 14 sum += number 15 Next 16 17 MessageBox.Show( &quot;The sum is &quot; & sum, _ 18 &quot;Sum even integers from 2 to 100&quot; , _ 19 MessageBoxButtons. OK , MessageBoxIcon. Information ) 20 End Sub ' Main 21 22 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
  • 11. 5.4 Examples Using the For / Next Structure Fig. 5.6 Icons for message dialogs.
  • 12. 5.4 Examples Using the For / Next Structure Fig. 5.7 Button constants for message dialogs.
  • 13. 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 14 15 principal = 1000.00 16 rate = 0.05 17 18 output = &quot;Year&quot; & vbTab & &quot;Amount on deposit&quot; & vbCrLf 19 20 ' calculate amount after each year 21 For year = 1 To 10 22 amount = principal * ( 1 + rate) ^ year 23 output &= year & vbTab & _ 24 String.Format( &quot;{0:C}&quot; , amount) & vbCrLf 25 Next 26 27 ' display output 28 MessageBox.Show(output, &quot;Compound Interest&quot; , _ 29 MessageBoxButtons. Ok , MessageBoxIcon. Information ) 30 31 End Sub ' Main 32 33 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
  • 15. 5.4 Examples Using the For / Next Structure Fig. 5.9 String formatting codes.
  • 16.
  • 17. 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 13 14 Console.Write( &quot;Enter a grade, -1 to quit: &quot; ) 15 grade = Console.ReadLine() 16 17 ' input and process grades 18 While grade <> -1 19 20 Select Case grade ' check which grade was input 21 22 Case 100 ' student scored 100 23 Console.WriteLine( &quot;Perfect Score!&quot; & vbCrLf & _ 24 &quot;Letter grade: A&quot; & vbCrLf ) 25 aCount += 1 26 27 Case 90 To 99 ' student scored 90-99 28 Console.WriteLine( &quot;Letter Grade: A&quot; & vbCrLf ) 29 aCount += 1 30 31 Case 80 To 89 ' student scored 80-89 32 Console.WriteLine( &quot;Letter Grade: B&quot; & 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
  • 18. SelectTest.vb 35 Case 70 To 79 ' student scored 70-79 36 Console.WriteLine( &quot;Letter Grade: C&quot; & vbCrLf ) 37 cCount += 1 38 39 Case 60 To 69 ' student scored 60-69 40 Console.WriteLine( &quot;Letter Grade: D&quot; & vbCrLf ) 41 dCount += 1 42 43 ' student scored 0 or 10-59 (10 points for attendance) 44 Case 0 , 10 To 59 45 Console.WriteLine( &quot;Letter Grade: F&quot; & vbCrLf ) 46 fCount += 1 47 48 Case Else 49 50 ' alert user that invalid grade was entered 51 Console.WriteLine( &quot;Invalid Input. &quot; & _ 52 &quot;Please enter a valid grade.&quot; & vbCrLf ) 53 End Select 54 55 Console.Write( &quot;Enter a grade, -1 to quit: &quot; ) 56 grade = Console.ReadLine() 57 End While 58 59 ' display count of each letter grade 60 Console.WriteLine( vbCrLf & _ 61 &quot;Totals for each letter grade are: &quot; & vbCrLf & _ 62 &quot;A: &quot; & aCount & vbCrLf & &quot;B: &quot; & bCount _ 63 & vbCrLf & &quot;C: &quot; & cCount & vbCrLf & &quot;D: &quot; & _ 64 dCount & vbCrLf & &quot;F: &quot; & fCount) 65 66 End Sub ' Main 67 68 End Module ' modEnterGrades Optional Case Else executes if no match occurs with previous Case s End Select marks end of structure
  • 19. 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
  • 20. 5.5 Select Case Multiple Fig. 5.11 Flowcharting the Select Case multiple-selection structure. Case a Case b Case z . . . Case Else action(s) false false false Case a action(s) Case b action(s) Case z action(s) true true true
  • 21.
  • 22. 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 = 1 9 10 ' print values 1 to 5 11 Do 12 Console.Write(counter & &quot; &quot; ) 13 counter += 1 14 Loop While (counter <= 5 ) 15 16 End Sub ' Main 17 18 End Module ' modDoWhile 1 2 3 4 5 Do keyword begins structure Loop While ends structure Condition tested after body executes
  • 23. 5.7 Do / Loop Until Repetition Structure Fig. 5.13 Flowcharting the Do/Loop While repetition structure. true false action(s) condition
  • 24.
  • 25. 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 = 1 9 10 ' print values 1 to 5 11 Do 12 Console.Write(counter & &quot; &quot; ) 13 counter += 1 14 Loop Until counter > 5 15 16 End Sub ' Main 17 18 End Module ' modLoopUntil 1 2 3 4 5 6 7 8 9 Condition tested after body executes
  • 26. 5.7 Do / Loop Until Repetition Structure Fig. 5.15 Flowcharting the Do/Loop Until repetition structure. true false action(s) condition
  • 27.
  • 28. 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 11 12 For counter = 1 To 10 13 14 ' skip remaining code in loop only if counter = 3 15 If counter = 3 Then 16 Exit For 17 End If 18 19 Next 20 21 output = &quot;counter = &quot; & counter & _ 22 &quot; after exiting For/Next structure&quot; & vbCrLf 23 24 Do Until counter > 10 25 26 ' skip remaining code in loop only if counter = 5 27 If counter = 5 Then 28 Exit Do 29 End If 30 31 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
  • 29. Program Output 34 output &= &quot;counter = &quot; & counter & _ 35 &quot; after exiting Do Until/Loop structure&quot; & vbCrLf 36 37 While counter <= 10 38 39 ' skip remaining code in loop only if counter = 7 40 If counter = 7 Then 41 Exit While 42 End If 43 44 counter += 1 45 End While 46 47 output &= &quot;counter = &quot; & counter & _ 48 &quot; after exiting While structure&quot; 49 50 MessageBox.Show(output, &quot;Exit Test&quot; , _ 51 MessageBoxButtons. OK , MessageBoxIcon. Information ) 52 End Sub ' Main 53 54 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
  • 30.
  • 31.
  • 32.
  • 33. 5.9 Logical Operators Fig. 5.17 Truth table for the AndAlso (logical AND) operator.
  • 34. 5.9 Logical Operators Fig. 5.18 Truth table for the OrElse (logical OR) operator.
  • 35. 5.9 Logical Operators Fig. 5.19 Truth table for the boolean logical exclusive OR (Xor) operator. Fig. 5.20 Truth table for operator Not (logical NOT).
  • 36. LogicalOperator.vb 1 ' Fig. 5.21: LogicalOperator.vb 2 ' Using logical operators. 3 4 Public Class FrmLogicalOperator 5 Inherits System.Windows.Forms.Form 6 7 ' Visual Studio .NET generated code 8 9 Private Sub FrmLogicalOperator_Load( _ 10 ByVal sender As System.Object, _ 11 ByVal e As System.EventArgs) Handles MyBase .Load 12 13 lblAndAlso.Text = &quot;AndAlso&quot; & vbCrLf & vbCrLf & _ 14 &quot;False AndAlso False: &quot; & ( False AndAlso False ) & _ 15 vbCrLf & &quot;False AndAlso True: &quot; & _ 16 ( False AndAlso True ) & vbCrLf & _ 17 &quot;True AndAlso False: &quot; & ( True AndAlso False ) & _ 18 vbCrLf & &quot;True AndAlso True: &quot; & ( True AndAlso True ) 19 20 lblOrElse.Text = &quot;OrElse&quot; & vbCrLf & vbCrLf & _ 21 &quot;False OrElse False: &quot; & ( False OrElse False ) & _ 22 vbCrLf & &quot;False OrElse True: &quot; & ( False OrElse True ) & _ 23 vbCrLf & &quot;True OrElse False: &quot; & ( True OrElse False ) & _ 24 vbCrLf & &quot;True OrElse True: &quot; & ( True OrElse True ) 25 26 lblAnd.Text = &quot;And&quot; & vbCrLf & vbCrLf & _ 27 &quot;False And False: &quot; & ( False And False ) & vbCrLf & _ 28 &quot;False And True: &quot; & ( False And True ) & vbCrLf & _ 29 &quot;True And False: &quot; & ( True And False ) & vbCrLf & _ 30 &quot;True And True: &quot; & ( True And True ) 31 Code generated by Visual Studio represented by this comment Demonstrate AndAlso operator Demonstrate OrElse operator Demonstrate And operator
  • 37. LogicalOperator.vb 32 lblOr.Text = &quot;Or&quot; & vbCrLf & _ 33 vbCrLf & &quot;False Or False: &quot; & ( False Or False ) & _ 34 vbCrLf & &quot;False Or True: &quot; & ( False Or True ) & _ 35 vbCrLf & &quot;True Or False: &quot; & ( True Or False ) & _ 36 vbCrLf & &quot;True Or True: &quot; & ( True Or True ) 37 38 lblXor.Text = &quot;Xor&quot; & vbCrLf & _ 39 vbCrLf & &quot;False Xor False: &quot; & ( False Xor False ) & _ 40 vbCrLf & &quot;False Xor True: &quot; & ( False Xor True ) & _ 41 vbCrLf & &quot;True Xor False: &quot; & ( True Xor False ) & _ 42 vbCrLf & &quot;True Xor True: &quot; & ( True Xor True ) 43 44 lblNot.Text = &quot;Not&quot; & vbCrLf & vbCrLf & _ 45 &quot;Not False: &quot; & ( Not False ) & vbCrLf & &quot;Not True: &quot; & _ 46 ( Not True ) 47 48 End Sub ' FrmLogicalOperator_Load 49 50 End Class ' FrmLogicalOperator Demonstrate Or operator Demonstrate Xor operator Demonstrate Not operator
  • 39. 5.9 Logical Operators Fig. 5.22 Precedence and associativity of the operators discussed so far.
  • 40.
  • 41. 5.10 Structured Programming Summary Fig. 5.23 Visual Basic’s single-entry/single-exit sequence and selection structures. Sequence . . . Selection If / Then structure (single selection) T F If / Then / Else structure (double selection) T F . . . Select Case structure (multiple selection)
  • 42. 5.10 Structured Programming Summary Fig. 5.24 Visual Basic’s single-entry/single-exit repetition structures. While structure T F F T For / Next structure T F Do / Loop Until structure Do / Loop While structure F T Repetition
  • 43. 5.10 Structured Programming Summary Fig. 5.24 Visual Basic’s single-entry/single-exit repetition structures. Do While / Loop structure T F Do Until / Loop structure F T F T For Each / Next structure Repetition
  • 44. 5.10 Structured Programming Summary Fig. 5.25 Rules for forming structured programs.
  • 45. 5.10 Structured Programming Summary Fig. 5.26 Simplest flowchart. . . . Rule 2 Rule 2 Rule 2 Fig. 5.27 Repeatedly applying rule 2 of Fig. 5.25 to the simplest flowchart.
  • 46. 5.10 Structured Programming Summary Fig. 5.28 Applying rule 3 of Fig. 5.25 to the simplest flowchart. Rule 3 Rule 3
  • 47. 5.10 Structured Programming Summary Fig. 5.29 Stacked, nested and overlapped building blocks. Nested building blocks Stacked building blocks Overlapping building blocks (Illegal in structured programs)
  • 48. 5.10 Structured Programming Summary Fig. 5.30 Unstructured flowchart.