UTPA – Fall 2011

33
CSCI 3327 Visual Basic CSCI 3327 Visual Basic Chapter 4: Control Chapter 4: Control Statements in Visual Statements in Visual Basic (Part 1A) Basic (Part 1A) UTPA – Fall 2011

description

CSCI 3327 Visual Basic Chapter 4: Control Statements in Visual Basic ( Part 1A). UTPA – Fall 2011. Objectives. In this chapter, you will: Learn the primitive data types in Visual Basic Become familiar with arithmetic operators Explore how to design algorithms to solve problems - PowerPoint PPT Presentation

Transcript of UTPA – Fall 2011

Page 1: UTPA – Fall 2011

CSCI 3327 Visual Basic CSCI 3327 Visual Basic Chapter 4: Control Statements in Chapter 4: Control Statements in

Visual Basic (Part 1A)Visual Basic (Part 1A)

UTPA – Fall 2011

Page 2: UTPA – Fall 2011

Objectives

• In this chapter, you will:– Learn the primitive data types in Visual Basic– Become familiar with arithmetic operators– Explore how to design algorithms to solve

problems– Learn the components of basic control structures– Study the syntax of basic sequence, selection, and

repetition structures in Visual Basic

2

Page 3: UTPA – Fall 2011

Introduction

• Computer program– Sequence of statements whose objective is to

accomplish a task

• Programming – Process of planning and creating a program

3

Page 4: UTPA – Fall 2011

Introduction (cont'd)

• Function– Collection of statements; when executed,

accomplishes something

• Syntax– Rules that specify which statements (instructions)

are legal

• Programming language– A set of rules, symbols, and special words– Visual Basic

4

Page 5: UTPA – Fall 2011

Introduction (cont'd)

• Reserved words, keywords, or word symbols– Words that are reserved by Visual Basic– Usually in blue color in the IDE (Visual Studio)

5

Page 6: UTPA – Fall 2011

Primitive Data Types in Visual Basic

• Boolean• Byte• Char• Date• Decimal• Double• Integer• Long

6

• SByte

• Short

• Single

• String

• Uinteger

• Ulong

• UShort

Page 7: UTPA – Fall 2011

Declaration of Variables

• All variables must be declared before they are used in a program

• Declaring a variable– Dim number1 As Integer– Dim number2 As Integer

• Declaring multiple variables of the same type– Dim number1, number2 As Integer

7

Page 8: UTPA – Fall 2011

Naming Convention

• Camel case – Variable / function name– E.g., taxRate, salaryPayment

• Control naming convention– Variable name = the meaning of control's value+

control's type– E.g., number1Label, number2TextBox

• Although the variable/function name is not case sensitive, it is important to follow a consistent naming convention

8

Page 9: UTPA – Fall 2011

Arithmetic Operators in Visual Basic

• Addition: +

• Subtraction: -

• Multiplication: *

• Division (floating point): /

• Division (integer): \

• Modulus: Mod

• Exponentiation: ^

9

Page 10: UTPA – Fall 2011

Division

• Division (floating point)– x / y– E.g., 7.l / 4 evaluates to 1.775

10

Page 11: UTPA – Fall 2011

Division (cont'd)

• Division (integer)– x \ y– x and y are integers

• 7\4 evaluates to 1, and 17\5 evaluates to 3

– x and y are not integers• Numbers are first rounded to the nearest whole number

• E.g., 7.1 is rounded to 7, and 7.7 is rounded to 8

• Thus, 7.1\4 evaluates to 1, and 7.7\4 yields 2

11

Page 12: UTPA – Fall 2011

Modulus & Exponentiation

• Modulus– r Mod s– 7 Mod 3 evaluates to 1 (since 7=3*2+1)

• Exponentiation– 3^2 evaluates to 3*3=9– 2^3 evaluates to 2*2*2=8– 2^10 evaluates to 1024

12

Page 13: UTPA – Fall 2011

Sign Operations

• Unary Minus– -e– E.g., -10, -3.14

• Unary Plus– +g– E.g., +100 (equivalent to 100)

13

Page 14: UTPA – Fall 2011

Rules of Operator Precedence

• ^

• +, - (sign operations)

• *, /

• \

• Mod

• +, - (addition and subtraction)

• If there are several operators of the same priority, then they are evaluated from left to right

14

priorityhigh

low

Page 15: UTPA – Fall 2011

Exercises

• What are the values of the following expressions?– 5.2/2– 9 Mod 3– 4\2– 4.4\2

• What is the order of the following expression?– X = 2 * 5 ^ 2 + 3 * 5 + 7

15

Page 16: UTPA – Fall 2011

Comparison Operators

• Equality operators– = (equal)– <> (not equal)

• Relational operators– >– <– >=– <=

16

Page 17: UTPA – Fall 2011

Rules of Operator Precedence

• ^• +, - (sign operations)• *, /• \• Mod• +, - (addition and subtraction)• =, <>, <, <=, >, >= (equality and relational)

17

priority

high

low

Page 18: UTPA – Fall 2011

Example 3.27: Comparison.vb

• URL: http://media.pearsoncmg.com/ph/esm/deitel/vb_htp_2010/codeexamples.html

• TextBox– Set MultiLine property to true– AppendText method– vbCrLf – press an "enter" key

18

Page 19: UTPA – Fall 2011

Problem Solving Techniques

• Problem-solving process has three steps:– Analyze problem and design an algorithm– Implement the algorithm in code– Maintain the program

• Algorithm is independent of languages – Actions to be executed, and – The order in which these actions are executed

• Pseudo code

19

Page 20: UTPA – Fall 2011

Control Structures

• Problem with the GoTo statement– GoTo statement can specify a control to any place

(line or destination) in a program– Making the program unstructured and hard to

follow

• Research indicates that all programs can be written by only 3 control structures– With "GoTo elimination"

20

Page 21: UTPA – Fall 2011

Categories of Control Structures

• Control Structures– Sequence structure– Selection structure– Repetition structure

21

Page 22: UTPA – Fall 2011

Sequence Structure

• Visual Basic statement– total = total + grade– counter=counter+1

• UML activity diagram– Flowchart – Initial state action state 1

… action state n

final state

22

Add grade to total

Add 1 to counter

Page 23: UTPA – Fall 2011

Selection Structure

• If … Then

• If … Then … Else

• Select … Case

23

display "passed"

[grade>=60]

[grade<60]

Page 24: UTPA – Fall 2011

Selection (1)

• If … Then– If grade >= 60 Then

write(“Passed”)

End If

24

Page 25: UTPA – Fall 2011

Selection (2)

• If … Then … Else– If grade >= 60 Then

write(“Passed”)

Else

write (“Failed”)

End If

25

Page 26: UTPA – Fall 2011

Nested SelectionIf grade >= 90 Then write(“A”)Else If grade >= 80 Then write(“B”) Else If grade >= 70 Then write(“C”) Else write(“F”) End If End IfEnd If

26

Page 27: UTPA – Fall 2011

Alternative Version

If grade >=90 Then write (“A”)ElseIf grade >=80 Then write(“B”)ElseIf grade >= 70 Then write(“C”)Else write(“D”)End If

27

Page 28: UTPA – Fall 2011

Repetition Structure

• Visual Basic provides 7 repetition statements– Do While … Loop– While … End While– Do Until … Loop– Do … Loop While– Do … Loop Until– For … Next– For Each … Next

28

Page 29: UTPA – Fall 2011

Example of Repetition

• See example programs– Do While … Loop– Find the first power of 3 larger than 100

29

triple the product value

[product<=100]

[product>100]

decision

merge

Page 30: UTPA – Fall 2011

Example of Repetition (cont'd)

• While + loop-continuation condition-------------------------------------------------------------------Do While product <=100 product = product * 3 ' compute next power of 3Loop

-----------------------------------------------------------While product <=100 product = product * 3 ' compute next power of 3End While

30

Page 31: UTPA – Fall 2011

Example of Repetition (cont'd)

• Until + loop-termination condition-------------------------------------------------------------------

Do Until product > 100

product = product * 3 ' compute next power of 3

Loop

31

Page 32: UTPA – Fall 2011

Example 4.12: ClassAverage.vb

• URL: http://media.pearsoncmg.com/ph/esm/deitel/vb_htp_2010/codeexamples.html

• ListBox– gradeListBox.Items.Add(gradeTextBox.Text)– gradeListBox.Items(gradeCounter)– gradeListBox.Items.Count

32

Page 33: UTPA – Fall 2011

33