Presentation is loading. Please wait.

Presentation is loading. Please wait.

Case & Repetitive Statements

Similar presentations


Presentation on theme: "Case & Repetitive Statements"— Presentation transcript:

1 Case & Repetitive Statements
Skill Area 306.2 Materials Prepared by Dhimas Ruswanto, BMm

2 Lecture Overview Case Statement Do While…Loop Do…Loop While
Do Until…Loop Do…Loop Until For…Next

3 Case Statement

4 Case Statement Its objective is to check several possible constant values for an expression. Something similar to what we did at the beginning of this section with the concatenation of several if and else if instructions.

5 Case Statement Select Case (expression) Case constant1
The syntax for the case statement is as follows: Select Case (expression) Case constant1 group of statements 1 Case constant2 group of statements 2 . . . Case Else group of statements End Select

6 Case Statement (Example)
Dim num As Integer Dim msg As String num = Val(txtDisplay.Text) Select Case num Case 1 To 50 msg = "Pass“ Case 71, 76, 79 To 100 msg = "High" Case Is > 101 msg = "Higher“ Case Else msg = "Invalid“ End Select MsgBox(msg)

7 Case Statement (Example)
Both of the following code fragments have the same behavior: Case example if-else equivalent Select Case name Case "Adam" MsgBox(“Position is MANAGER") Case "Bob” MsgBox(“Position is SUPERVISOR") Case "Charlie” MsgBox(“Position is ACCOUNTANT") Case "Eve" MsgBox(“Position is SECRETARY") Case Else MsgBox("No data found") End Select If name = "Adam" Then ElseIf name = "Bob" Then ElseIf name = "Charlie" Then ElseIf name = "Eve" Then Else End If

8 Types of repetition statements:
A repetition statement can repeat actions, depending on the value of a condition (which can be either truer or false) Types of repetition statements: Do While…Loop Do…Loop While Do Until…Loop Do…Loop Until

9 Do While…loop & do Loop…while

10 Do While..Loop & Do..Loop While
Do While..Loop & Do..Loop While – a control statement that executes a set of body statements while its loop-continuation condition is True Loop-continuation condition – the condition used in a repetition statement that enables repetition to continue while the condition is True and that causes repetition to terminate when the condition becomes False Syntax: Do While loop-continuation condition block of statements Loop Do block of statements Loop While loop-continuation condition

11 Do While..Loop & Do..Loop While
Example: Do While product <= 50 product += 3 Loop Do product +=3 Loop While product <= 50 In the Do While..Loop statement, the loop-continuation condition is tested at the beginning of the loop, before the body of the loop is performed. In Do..Loop While statement performs the loop-continuation condition after the loop body if performed. Therefore, in a Do..Loop While statement, the loop body always executes at least once. A Do While..Loop executes only if its loop-continuation condition evaluates to true.

12 Do While..Loop & Do..Loop While
Example: Dim number As Integer Do While number < 4  MsgBox("The number is " & number)  number = number + 1 Loop Dim number As Integer Do   MsgBox("The number is " & number)  number = number + 1 Loop While number < 4

13 Do While..Loop & Do..Loop While
Example: Dim number As Integer Do While number < 4  MsgBox("The number is " & number)  number = number + 1 Loop The output of the code above will be different with the code below Dim number As Integer Do While number < 4   number = number + 1 Loop MsgBox("The number is " & number) 

14 Do until…loop & do Loop…until

15 Do Until..Loop & Do..Loop Until
Do Until..Loop & Do..Loop Until– a control statement that executes a set of body statements while its loop-termination condition becomes True. Loop-termination condition – the condition used in a repetition statement that enables repetition to continue while the condition is False and that causes repetition to terminate when the condition becomes True Syntax: Do Until loop-termination condition block of statements Loop Do block of statements Loop Until loop-termination condition

16 Do Until..Loop & Do..Loop Until
Example: Do Until product >= 50 product += 3 Loop Do product +=3 Loop Until product >= 50 In the Do Until..Loop statement, the loop-termination condition is tested at the beginning of the loop, before the body of the loop is performed. In Do..Loop Until statement performs the loop-termination condition after the loop body if performed. Therefore, in a Do..Loop Until statement, the loop body always executes at least once. A Do Until..Loop executes only if its loop-termination condition evaluates to true.

17 Do While..Loop & Do Until..Loop
Dim number As Integer Do While number < 4  MsgBox("The number is " & number)  number = number + 1 Loop Do Until…Loop Dim number As Integer Do Until number > 4 MsgBox("The number is " & number)   number = number + 1 Loop

18 For…next

19 Counter-controlled Repetition
Four essential elements of counter-controlled repetition: The name of a control variable (or loop counter) that is used to determine whether the loop continues to iterate The initial value of the control variable The increment (or decrement) by which the control variable is modified during each iteration of the loop (that is, each time the loop is performed) The condition that tests for the final value of the control variable (to determine whether looping should continue)

20 answer = answer + startNumber
For…Next Statement Optional (depends on the program) Syntax: For controlVariable = initialValue To finalValue Step increment statements Next Example Dim counter As Integer For counter = 2 To 10 Step 2 MsgBox("The number is " & counter) Next Dim answer As Integer Dim startNumber As Integer answer = 0 For startNumber = 1 To 4 answer += startNumber MsgBox(answer) Next answer = answer + startNumber

21 For…Next vs. Do While..Loop vs. Do Until..Loop
Dim number As Integer Do While number < 4  MsgBox("The number is " & number)  number = number + 1 Loop Do Until…Loop Dim number As Integer Do Until number > 4 MsgBox("The number is " & number)   number = number + 1 Loop For..Next Dim number As Integer For number = 1 To 4 MsgBox("The number is " & number)   Next

22 --- continue to next slide ---


Download ppt "Case & Repetitive Statements"

Similar presentations


Ads by Google