1 / 11

Statements, Conditionals & Loops

Statements, Conditionals & Loops. Statement Syntax Procedure Calls Referencing Objects If-Then Conditional Statement Select Case Statement Loops While-Wend Loops Programming Principles. Assignment statement: Let varname = value Equal sign is assignment operator

lindsey
Download Presentation

Statements, Conditionals & Loops

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. Statements, Conditionals & Loops • Statement Syntax • Procedure Calls • Referencing Objects • If-Then Conditional Statement • Select Case Statement • Loops • While-Wend Loops • Programming Principles Jeffrey P. Landry, University of South Alabama

  2. Assignment statement: Let varname = value Equal sign is assignment operator Let Name$ = “Name: “ & txtEmpName.Text Expressions have a data type string, numeric, boolean (true/false, etc.) conditional expressions in If-Then statements must evaluate to a boolean value Type compatibility between varname and value A procedure call is a statement Call cmdQuit_Click() - Statement Syntax Jeffrey P. Landry, University of South Alabama

  3. Statement Syntax (cont.) • Use underscore for statement continuation • Response = MsgBox(“Invalid input.”, _ vbExclamation, “Error”) • No line termination character in VB • Must have a complete statement • not an expression on a line by itself • remember pairs of delimiters (parens, quotes) • Common syntax errors • End of statement • Expecting ‘)’ Jeffrey P. Landry, University of South Alabama

  4. Function calls can appear in expressions but they must return a value function must match expected type Wrong: if Len(txtLast) Then… Correct: if Len(txtLast) = 0 Then... Subroutines called on a line by itself Call CalculatePay CalculatePay Procedure Calls Jeffrey P. Landry, University of South Alabama

  5. Referencing Objects • Objects can be assigned values or referenced • Properties have a data type • variant type for some properties • watch for type incompatibility errors • Refer to objects in code using the object.property notation • if txtLast.Text = “” Then … • fMainMenu.Enabled= False • Also the form.control.property notation • Form1.Label3.Caption = “My Name: ” • Default form and default properties Jeffrey P. Landry, University of South Alabama

  6. Format End line with Then Indent 2 spaces before {statements} If, Else & End If all line up Only one set of statements gets executed If-Then’s can be nested, or use the ElseIf structure If condition Then {statements} Else {statements} End If If condition1 Then {statements} ElseIf condition2 Then {statements} Else {statements} End If If-Then Conditional Statement Jeffrey P. Landry, University of South Alabama

  7. An alternative to nested If-Then’s Actual values of testexpression are matched to expressionlists following the Case statements Only code in (one) appropriate block executes Select Case testexpression [Case expressionlist [statements1]]… [Case Else [elsestatements]] End Select Select Case Index Case 0 fAdd.Show Case 1 fPrint.Show Case Else End Select Select-Case Statement Jeffrey P. Landry, University of South Alabama

  8. Select-Case Statement (cont.) • Several forms of the Case statement • Case “No” ‘* string values • Case 3 ‘* numeric values • Case 3 To 7 ‘* range of values • Case Is > 7 • Case Is <= 2 ‘* comparisons • Case 3,5,8 • Case 3 To 7, 9 ‘* lists of values Jeffrey P. Landry, University of South Alabama

  9. For / Counted Loop Dim intCount as Integer For intCount = 1 to 3 Step 1 Print intCount Next intCount Do While Loop Dim intCount as Integer intCount = 1 Do While intCount <= 3 Print intCount intCount = intCount + 1 Loop (Pre-Test Loop) Do Until Loop Dim intCount as Integer intCount = 1 Do Print intCount intCount = intCount + 1 Loop until intCount > 3 (Post Test Loop) Loops Jeffrey P. Landry, University of South Alabama

  10. An example of a conditional loop statements repeatedly execute until the condition is satisfied condition must be a boolean expression See help on Do-Loop statement for more flexibility Read about For-Next counting loops While condition {statements} Wend Let Count = 0 Let Sum = 0 Open “Grades.txt” For Input As #1 While Not EOF(1) Input #1, Name$, Grade Let Sum = Sum + Grade Let Count = Count + 1 Wend picOut.Print “Average=“;Sum/Count While-Wend Loops Jeffrey P. Landry, University of South Alabama

  11. Programming Principles • Use incremental development strategy • Top-down design & development • Debug & comment as-you-go • Don’t code on speculation -- pseudocode instead • Meaningful identifiers in mixed case style • cmdQuit, EmployeePayRate, LastName • Make use of proper indenting & spacing • No GoTo statements! • GoTo’s create ‘spaghetti’ code Jeffrey P. Landry, University of South Alabama

More Related