Download presentation
Presentation is loading. Please wait.
Published byStephanie Lamb Modified over 8 years ago
1
Recursion and Branching By: Engr. Faisal ur Rehman CE-105T Spring 2007 By: Engr. Faisal ur Rehman CE-105T Spring 2007
2
Call, Function, Property, SubUse procedures. Choose, If...Then...Else, Select Case, SwitchMake decisions. Do...Loop, For...Next, For Each...Next, While...End While, WithLoop. End, Exit, StopExit or pause the program. GoTo, On ErrorBranch. Language elementAction
3
Recursion It is the process of repetition of a particular statement Also know as iteration Achieved by Control Structure or Loop Also achieved by recursive functions It is the process of repetition of a particular statement Also know as iteration Achieved by Control Structure or Loop Also achieved by recursive functions
4
Loops Running a set of statements until a condition becomes true
5
Loop Types While Loop Do Loop For Loop For Each Loop While Loop Do Loop For Loop For Each Loop
6
While Loop Runs a series of statements as long as a given condition is True. While condition [ statements ] [ Exit While ] [ statements ] End While Runs a series of statements as long as a given condition is True. While condition [ statements ] [ Exit While ] [ statements ] End While
7
While Loop Dim counter As Integer = 0 While counter < 20 counter += 1 debug.writeline counter ‘if counter = 10 then exit while End While MsgBox("While loop ran " & CStr(counter) & " times") Put code in form_load and also check debug window (Ctrl + G) after running the code Dim counter As Integer = 0 While counter < 20 counter += 1 debug.writeline counter ‘if counter = 10 then exit while End While MsgBox("While loop ran " & CStr(counter) & " times") Put code in form_load and also check debug window (Ctrl + G) after running the code
8
Do Loop Repeats a block of statements while a Boolean condition is True or until the condition becomes True. Do { While | Until } condition [ statements ] [ Exit Do ] [ statements ] Loop -or- Do [ statements ] [ Exit Do ] [ statements ] Loop { While | Until } condition Repeats a block of statements while a Boolean condition is True or until the condition becomes True. Do { While | Until } condition [ statements ] [ Exit Do ] [ statements ] Loop -or- Do [ statements ] [ Exit Do ] [ statements ] Loop { While | Until } condition Debug.WriteLine("Do Demo 1:") Do while i <= 10 debug.writeline i i = i+1 Loop Debug.WriteLine("Do Demo 2:") Do debug.writeline i i = i+1 Loop while i <= 10 Debug.WriteLine("Do Demo 1:") Do while i <= 10 debug.writeline i i = i+1 Loop Debug.WriteLine("Do Demo 2:") Do debug.writeline i i = i+1 Loop while i <= 10
9
For Next Loop Repeats a group of statements a specified number of times. For counter [ As datatype ] = start To end [ Step step ] [ statements ] [ Exit For ] [ statements ] Next [ counter ] Repeats a group of statements a specified number of times. For counter [ As datatype ] = start To end [ Step step ] [ statements ] [ Exit For ] [ statements ] Next [ counter ]
10
For Next Loop For i = 1 to 10 Debug. writeline i Next i For i = 1 to 10 step 2 Debug. writeline i Next i For i = 1 to 10 Debug. writeline i Next i For i = 1 to 10 step 2 Debug. writeline i Next i For i = 1 to 10 step -1 Debug.writeline i ‘if i = 8 then exit for Next i For i = 1 to 10 step -1 Debug.writeline i ‘if i = 8 then exit for Next i
11
For Next Loop Code for Prime Composite number: Dim n As Double = 0 n = CDbl(txtNo.Text) For i As Integer = n - 1 To 2 Step -1 If n Mod i = 0 Then MsgBox("Composite number!") Exit Sub End If Next MsgBox("Prime number!") Code for Prime Composite number: Dim n As Double = 0 n = CDbl(txtNo.Text) For i As Integer = n - 1 To 2 Step -1 If n Mod i = 0 Then MsgBox("Composite number!") Exit Sub End If Next MsgBox("Prime number!")
12
For Each Loop Repeats a group of statements for each element in a collection. For Each element [ As datatype ] In group [ statements ] [ Exit For ] [ statements ] Next [ element ] Repeats a group of statements for each element in a collection. For Each element [ As datatype ] In group [ statements ] [ Exit For ] [ statements ] Next [ element ]
13
For Each Loop Dim found As Boolean = False Dim thisCollection As New Collection For Each thisObject As String In thisCollection If thisObject = "Hello" Then found = True Exit For End If Next thisObject Dim found As Boolean = False Dim thisCollection As New Collection For Each thisObject As String In thisCollection If thisObject = "Hello" Then found = True Exit For End If Next thisObject
14
Loops How to: Improve the Performance of a Loop
15
Recursive Function Self calling function is called recursive function Function factorial(ByVal n As Integer) As Integer If n <= 1 Then Return 1 Else Return factorial(n - 1) * n End If End Function Self calling function is called recursive function Function factorial(ByVal n As Integer) As Integer If n <= 1 Then Return 1 Else Return factorial(n - 1) * n End If End Function
16
Other Control Structures With object [ statements ] End With With btn1.text = “Test button”.Forecolor = vbblue.heght =40.width = 30 End with With object [ statements ] End With With btn1.text = “Test button”.Forecolor = vbblue.heght =40.width = 30 End with btn1.text = “Test button” btn1.Forecolor = vbblue btn1.heght =40 btn1.width = 30 btn1.text = “Test button” btn1.Forecolor = vbblue btn1.heght =40 btn1.width = 30
17
Branching / Decision Structure
18
If Condition Select case Try catch Goto On Error If Condition Select case Try catch Goto On Error
19
IF Condition If condition [ Then ] [ statements ] [ ElseIf elseifcondition [ Then ] [ elseifstatements ] ] [ Else [ elsestatements ] ] End If -or- If condition Then [ statements ] [ Else [ elsestatements ] ] If condition [ Then ] [ statements ] [ ElseIf elseifcondition [ Then ] [ elseifstatements ] ] [ Else [ elsestatements ] ] End If -or- If condition Then [ statements ] [ Else [ elsestatements ] ]
20
IF Condition Code for Even / Odd: Dim n As Double = 0 n = CDbl(txtNo.Text) If n Mod 2 = 0 Then MsgBox("Even number!") Else MsgBox("Odd number!") End If Code for Even / Odd: Dim n As Double = 0 n = CDbl(txtNo.Text) If n Mod 2 = 0 Then MsgBox("Even number!") Else MsgBox("Odd number!") End If
21
Select Case Select [ Case ] testexpression [ Case expressionlist [ statements ] ] [ Case Else [ elsestatements ] ] End Select Select [ Case ] testexpression [ Case expressionlist [ statements ] ] [ Case Else [ elsestatements ] ] End Select
22
Select Case Dim S as string = txtCode.text Select case S Case “A”, “a” Msgbox “Afghanistan Case “B”, “b” Msgbox “Brazil” Case else Msgbox “Unknown Code” End select Dim S as string = txtCode.text Select case S Case “A”, “a” Msgbox “Afghanistan Case “B”, “b” Msgbox “Brazil” Case else Msgbox “Unknown Code” End select
23
Try Catch Finally Try [ tryStatements ] [ Exit Try ] [ Catch [ exception [ As type ] ] [ When expression ] [ catchStatements ] [ Exit Try ] ] [ Catch... ] [ Finally [ finallyStatements ] ] End Try Try [ tryStatements ] [ Exit Try ] [ Catch [ exception [ As type ] ] [ When expression ] [ catchStatements ] [ Exit Try ] ] [ Catch... ] [ Finally [ finallyStatements ] ] End Try
24
Try Catch Finally Code for factorial: Try Dim n As Integer = InputBox("Enter a number for factorial:") MsgBox("Factorial is:" & vbCrLf & factorial(n)) Catch ex As Exception MsgBox(Err.Description & vbCrLf & "Error Value:" & Err.Number) Finally Beep End Try Code for factorial: Try Dim n As Integer = InputBox("Enter a number for factorial:") MsgBox("Factorial is:" & vbCrLf & factorial(n)) Catch ex As Exception MsgBox(Err.Description & vbCrLf & "Error Value:" & Err.Number) Finally Beep End Try
25
Goto Sub gotoStatementDemo() Dim number As Integer = 1 Dim sampleString As String ' Evaluate number and branch to appropriate label. If number = 1 Then GoTo Line1 Else GoTo Line2 Line1: sampleString = "Number equals 1" GoTo LastLine Line2: ' The following statement never gets executed because number = 1. sampleString = "Number equals 2" LastLine: ' Write "Number equals 1" in the Debug window. Debug.WriteLine(sampleString) End Sub Sub gotoStatementDemo() Dim number As Integer = 1 Dim sampleString As String ' Evaluate number and branch to appropriate label. If number = 1 Then GoTo Line1 Else GoTo Line2 Line1: sampleString = "Number equals 1" GoTo LastLine Line2: ' The following statement never gets executed because number = 1. sampleString = "Number equals 2" LastLine: ' Write "Number equals 1" in the Debug window. Debug.WriteLine(sampleString) End Sub GoTo line
26
On Error On Error { GoTo [ line | 0 | -1 ] | Resume Next } On error goto handler: Dim n As Integer = InputBox("Enter a number for factorial:") MsgBox("Factorial is:" & vbCrLf & factorial(n)) Exit sub Handler: MsgBox(Err.Description & vbCrLf & "Error Value:" & Err.Number) Resume next On Error { GoTo [ line | 0 | -1 ] | Resume Next } On error goto handler: Dim n As Integer = InputBox("Enter a number for factorial:") MsgBox("Factorial is:" & vbCrLf & factorial(n)) Exit sub Handler: MsgBox(Err.Description & vbCrLf & "Error Value:" & Err.Number) Resume next
27
Nested Control Structures Code for Prime / Composite Dim n As Double = 0 n = CDbl(txtNo.Text) For i As Integer = n - 1 To 2 Step -1 If n Mod i = 0 Then MsgBox("Composite number!") Exit Sub End If Next MsgBox("Prime number!") Code for Prime / Composite Dim n As Double = 0 n = CDbl(txtNo.Text) For i As Integer = n - 1 To 2 Step -1 If n Mod i = 0 Then MsgBox("Composite number!") Exit Sub End If Next MsgBox("Prime number!")
28
Q & A
29
Write structure of: For Next Loop For Each While Loop Do Loop With Structure If Condition Select case Try catch Goto On Error Write structure of: For Next Loop For Each While Loop Do Loop With Structure If Condition Select case Try catch Goto On Error
30
Q & A Define Branching and Recursion. Name components of Control Structure Name components of Decision Structure Define Branching and Recursion. Name components of Control Structure Name components of Decision Structure
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.