120 likes | 131 Views
Repetition Structures. Dr. John P. Abraham UTPA. While End While. Only enters the loop if condition yields true Choose LCV Initialize LCV Setup condition for looping statements Change value of LCV. Module Module1 Dim rate As Double Dim principle As Double
E N D
Repetition Structures Dr. John P. Abraham UTPA
While End While • Only enters the loop if condition yields true • Choose LCV • Initialize LCV • Setup condition for looping • statements • Change value of LCV
Module Module1 Dim rate As Double Dim principle As Double Dim period As Integer Dim dinterest, yinterest As Double Dim i As Integer = 1 Sub Main() rate = 0.07 principle = 10000.0 period = 2 'years Console.WriteLine("Beginning Principle: " & principle) While i <= period * 365 dinterest = principle * rate / 365 yinterest = yinterest + dinterest principle = principle + dinterest If i Mod 365 = 0 Then Console.WriteLine("Interest for the year: " & yinterest) Console.WriteLine("Principle at the year End: " & principle) yinterest = 0 Console.ReadKey() End If i = i + 1 End While End Sub End Module
Do while ..loop Similar to while..end Do Until..Loop Enters the loop if condition yields false Loop termination condition is different than the previous two Other pretest loops
Counter controlled A variable keeps track of number of time the loop should be executed. The number of executions are known before loop begins to execute. For-next is a counter controlled You provide LCV initial count, ending count, increment. Condition checking and incrementing is done automatically Sentinel controlled – sentinel value, events etc. Counter controlled & Sentinel controlled loops
Inner loop Inner loop executes for every instance of outer loop Outer loop Nested Repetition statements
Executed at least once Do..Loop While Do..Loop Until Termination condition is different than do..loop while Post test loops
Not recommended Can alter program flow of control Exit statement will break out of the loop Using Exit in Repetition Statements
= Equal to > More than < Less Than >= More than and equal <= Less than and equal <> Not Equal to Logical Oper Conditional (assignment) Operators
And :Both sides must be true AndAlso :If the left side is false does not check the right side Or :One side or other must be true OrElse : If the left side is true does not check the right side Xor :One side or other must be true but not both Not :Negates truth Logical Operators
Allows you to make multiple references to the same object in a concise manner. Suppose you have: GRADEBOOK.DISPLAYMESSAGE() GRADEBOOK.INPUTGRADES() GRADEBOOK.DISPLAYGRADEREPORT() You can do this: With gradebook .displaymessage(0 .inputgrades() .displaygradeReport() End with Using With statement
Using continue in Repetiton statements Skips the remaining statements in the loop body of the repetiton and causes control to proceed to the next iteration of the loop. Example While counter <10 counter +=1 If counter = 5 then Continue while ‘skips to next iteration of loop End if Console.write (“{0}”, counter) End while. Display 1 2 3 4 6 7 8 9 10