statements conditionals loops
Download
Skip this Video
Download Presentation
Statements, Conditionals & Loops

Loading in 2 Seconds...

play fullscreen
1 / 11

Statements, Conditionals Loops - PowerPoint PPT Presentation


  • 131 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Statements, Conditionals Loops' - lindsey


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
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

Jeffrey P. Landry, University of South Alabama

statement syntax
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

statement syntax cont
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

procedure calls
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

referencing objects
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

if then conditional statement
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

select case statement
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

select case statement cont
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

loops
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

while wend loops
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

programming principles
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

ad