SlideServe
Menu
Préférences
© SlideServe · v2026

Statements, Conditionals & Loops

lindsey avatar
11 diapositives · 344 Views · Jul 04, 2012

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

Statements, Conditionals & Loops - slide 1
01 / 11Statements, Conditionals & Loops
1 / 17

Télécharger la présentation

Statements, Conditionals & Loops
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  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