1 / 23

CMPF 114 Computer Literacy

CMPF 114 Computer Literacy. Chapter 4 & 5 Program Control Structures. Program Control. Refers to the order of execution of instructions or statements in a computer program. This requires Program Control Statements (Program Control Structures). Three basic program control structures:

lexi
Download Presentation

CMPF 114 Computer Literacy

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CMPF 114Computer Literacy Chapter 4 & 5 Program Control Structures

  2. Program Control • Refers to the order of execution of instructions or statements in a computer program. • This requires Program Control Statements (Program Control Structures). • Three basic program control structures: • Sequence • Selection • Repetition/Iteration/Loop

  3. Sequence Structure • In sequence structure, the instructions as executed sequentially one by one, starting from first instruction and ending with the last instruction. Entry Entry S1 S2 S3 Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load label1.text = "Hello" Label2.Text = "I am Fine" Label3.text = "Thank you!" End Sub End Class

  4. Selection Structure • The selection structure allow VB to execute instructions in a non-sequential fashion. • It compares two expressions, and based on the result (TRUE or FALSE), it branches off to another instruction in the procedure, which may not be the next instruction. • Selection Structures in VB: • IF…END IF • SELECT CASE…END SELECT • IIF FUNCTION

  5. IF…END IF • The conditional statement IF ELSE , is use for examining the conditions that we provided, and making decision based on that condition. The conditional statement examining the data using comparison operators as well as logical operators. • If the condition is TRUE then the control goes to between IF and Else block , that is the program will execute the code between IF and ELSE statements. • If the condition is FLASE then the control goes to between ELSE and END IF block , that is the program will execute the code between ELSE and END IF statements. Syntax Condition True False If [condition ] Then <TRUE_Statement> Else <FALSE_Statement> End If True Statement False Statement

  6. IF…END IF - Example Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load Dim Value1 As Integer Value1 = InputBox("Please enter an Integer:") If Value1 > 50 Then Label1.Text = "Your value is GREATER THAN 50" Else Label1.Text = "Your value is SMALLER THAN 50" End If End Sub End Class

  7. ELSEIF • If you want o check more than one condition at the same time , you can use ElseIf . • In this form, condition_1 is first tested. If condition_1 is TRUE, TRUE_Statement_1 is executed; otherwise, condition_2 is tested. If condition_2 is TRUE, TRUE_Statement_2 is executed; otherwise, condition_3 is tested. If condition_3 is TRUE, TRUE_Statement_3 is executed; otherwise, FALSE_Statement is executed. Note that only one of the statement blocks will be executed. If [condition_1] Then <TRUE_Statement_1> ElseIf [condition_2] Then <TRUE_Statement_2> ElseIf [condition_3] Then <TRUE_Statement_3> Else <FALSE_Statement>EndIf Syntax

  8. ELSEIF - Example Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim mark As Integer mark = InputBox("Please enter an Mark:") If mark < 50 Then Label1.Text = "Fail" ElseIf mark < 60 Then Label1.Text = "Pass" ElseIf mark < 70 Then Label1.Text = "Credit" Else Label1.Text = "Distinction" End If End Sub End Class

  9. Selection Case…End Select • The VB Select Case statement provides a neater solution to the more complex nested If…Else If Statement. The Select Case statement has the form Entry Syntax X = ? Select Case [Test_Expression ] Case [Expression_1] <Statement_1> Case [Expression_2] <Statement_2> … Case [Expression_n] <Statement_n> Case Else <Statement if Test_Expression does not match any of the Expressioin List> End Select X = 1 X = 2 X = 3 Y = X + 1 Y = X + 2 Y = X + 3 Exit

  10. Selection Case…End Select - Example Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim mark As Integer mark = InputBox("Please enter an Mark:") Select Case mark Case 70 To 100 Label1.Text = "Grade A" Case 60 To 69 Label1.Text = "Grade B" Case 50 To 59 Label1.Text = "Grade C" Case 40 To 49 Label1.Text = "Grade D" Case Else Label1.Text = "Grade F" End Select End Sub End Class

  11. Repetition Structure • Also called iteration or loop structure. • A loop is any group of instructions that executes repeatedly until specified condition is met. When the loop terminates, program control passes to the statement following the loop. • Types of loop in VB: • FOR…NEXT • DO WHILE…LOOP • WHILE…WEND

  12. FOR…NEXT • The FOR NEXT Loop , execute the loop body (the source code within For ..Next code block) to a fixed number of times. Syntax Entry For var = [startValue] To [endValue] [Step] [loopBody] Next [var] X = 1 X > 10? Yes var : The counter for the loop to repeat the steps. starValue : The starting value assign to counter variable . endValue : When the counter variable reach end value the Loop will stop . loopBody : The source code between loop body Step : if the Step is specified, the var will be incremented by the value given by Step each time through the loop No Statements X = X + 1 Exit

  13. FOR…NEXT - Example Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim x As Integer Debug.Print("X") For x = 0 To 6 'increament is 1 Debug.Print(x) Next x End Sub End Class

  14. FOR…NEXT – Example with STEP Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim x As Integer Debug.Print("X") For x = 0 To 6 STEP 2 'increament by 2 since STEP 2 Debug.Print(x) Next x End Sub End Class

  15. DO…LOOP • Similar to the FOR…NEXT loop expect that it is controlled by a condition rather than a loop index. The body of the loop continues to execute as long as the condition remains TRUE. The loop terminates when the condition is FALSE. • Two types of DO…LOOP: Type 1 Syntax Do While [condition] [StatementBlock] Loop Type 2 Syntax Do [StatementBlock] Loop Until [condition]

  16. DO…LOOP • Both types are quite similar except that in Type 1 the condition is tested at the beginning of the loop whereas in Type 2 the condition is tested at the end. • If the condition is TRUE, the statement block is executed; otherwise, it is skipped. Note that in Type 1, the statement block may or may not be executed depending on whether the condition is TRUE or FALSE whereas in Type 2 the statement block will be executed at least once. Type 2 Type 1 No TRUE? Statement Yes No TRUE? Statements Yes Statement Statement

  17. DO…LOOP – Example Type 1 Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim x As Integer x = 0 ‘initial the start value for x Do While x <= 6 Debug.Print(x) x = x + 1 ‘increament x by 1 Loop End Sub End Class

  18. DO…LOOP – Example Type 2 Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim x As Integer x = 0 ‘initial the start value for x Do Debug.Print(x) x = x + 1 Loop Until x > 6 End Sub End Class

  19. WHILE…WEND • Similar to DO WHILE…LOOP structure Syntax While [condition] [StatementBlock] End While Example Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim x As Integer x = 0 While x <= 6 Debug.Print(x) x = x + 1 End While End Sub End Class

  20. Some Guidelines Using Repetition Structure • Use the FOR…NEXT loop if you know exactly the number of times you want the execute the loop • Use the DO WHILE…LOOP loop if you need to test the condition before executing the statement block • Use the DO…LOOP UNTIL loop if you need to execute the statement block at least once.

  21. Exercise • Create a project to continuously check if the PIN entered by the user is correct. The user must enter the PIN correctly within three attempts; otherwise it will display an INVALID PIN message and terminate the project.

  22. Exercise - Answer Public Class Form1 Private Sub Form1_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated Dim pwd As String Dim attp As Integer attp = 0 Do pwd = InputBox("Enter your PIN:") attp = attp + 1 If attp >= 3 Then MsgBox("INVALID PIN", MsgBoxStyle.OkOnly, "Warning") Exit Do 'exit the loop End If Loop Until pwd = "unitar" 'check if the password is correct End Sub End Class

  23. The End

More Related