Fundamentals of visual basic

38

description

Fundamentals of visual basic. Comments. 1. are inserted to document programs and to improve readability. 2. help other people to read and understand your program code. 3. do not cause the computer to perform any action when a program is running as the compiler ignore it. - PowerPoint PPT Presentation

Transcript of Fundamentals of visual basic

Page 1: Fundamentals of visual basic
Page 2: Fundamentals of visual basic

CommentsComments1. are inserted to document programs and to

improve readability.

2. help other people to read and understand your program code.

3. do not cause the computer to perform any action when a program is running as the compiler ignore it.

4. can begin with either ‘ or Rem (short for Remark) and is a single line-comment that terminated at the end of the current line.

5. colored green.

Page 3: Fundamentals of visual basic

Comments..contComments..contExample:Example:

‘ ‘ Code for the single selectionCode for the single selection

Rem Rem Code for the single selectionCode for the single selection

Page 4: Fundamentals of visual basic

KeywordsKeywords

1. reserved words that cannot be used for anything other than for the features they represents. (e.g. Private, Sub, End, Print, etc.)

2. colored blue.

3. Visual Basic sets to uppercase the first letter of keywords, so typing dim would be changed to Dim.

Page 5: Fundamentals of visual basic

VariableVariable1. location in the computer memory where a value can be stored for use by a program.2. A variable name is any valid identifier.3. A variable name should not be any of the Visual Basic keywords and must begin with a letter.5. It must not contain a space or any of the special characters. Underscore (_) is the only special character accepted.6. The maximum length of a variable name is 255 characters containing only letters, numbers, and underscores.

Page 6: Fundamentals of visual basic

ConstantConstant- Has a fixed value and may not be changed by the program as it run.

Example:Dim Var_name As TypeDim Input1 As Integer

Page 7: Fundamentals of visual basic

Data TypesData Types

1. describe the information that a variable may store or hold.2. describe how many bytes of memory are required to represent a type.3. the number of bytes determined the range of values that can be stored.4. Byte sizes and range values are fixed.5. assigning any value outside a data type’s range is a run-time error.

Page 8: Fundamentals of visual basic

OperatorsOperators

- are symbols that trigger computations, comparisons, and decisions.

- Are combinations of operators and operands

ExpressionsExpressions

Page 9: Fundamentals of visual basic

OperatorsOperators

- are symbols that trigger computations, comparisons, and decisions.

- Are combinations of operators and operands

ExpressionsExpressions

Page 10: Fundamentals of visual basic

PROGRAM CONTROL PROGRAM CONTROL STRUCTURESSTRUCTURES

Page 11: Fundamentals of visual basic

PROGRAM CONTROL PROGRAM CONTROL STRUCTURES STRUCTURES

Refers to the order of program execution whether statements are executed sequentially, repeatedly, or conditionally or selectively

Page 12: Fundamentals of visual basic

Ten (10) Program Control Structures

1. Sequence Control Structure

- Statements are executed one after the other in the order in which they are written (called as Sequential Execution)

- Considered as the simplest form of a program control structure where the statements are executed from top to bottom.

Page 13: Fundamentals of visual basic

Sequence Control Structure..cont

Example:

Dim x As Integer, y As Integer, sum As Integer

X=3

Y=4

Sum = x+y

Label1.caption = “The sum is:” & sum

Page 14: Fundamentals of visual basic

2. Selection Structure

2.1. Single Selection

- performs or selects an action if a condition is True and skips the action if the condition is False.

Syntax:

If <condition> Then

Statements

End if

Page 15: Fundamentals of visual basic

2.2 Double Selection

- performs an action if the condition is True and performs a different action if the condition is False.Syntax:

If<condition>Then

Statement1

Else

Statement2

End if

Page 16: Fundamentals of visual basic

3. Multiple SelectionSyntax: (A)

If<condition1> Then

Statement1

ElseIf<condition2>

Statement2

ElseIf<condition3>

Statement3

ElseIf<condition4>

Statement4

End If

Page 17: Fundamentals of visual basic

Select Case Multiple StructureSyntax(B):

Select Case <var_name>

Case Is <range_of_values>

Statement

Case <value>

Statement

Case<value1> To <value2>

Statement

Case <value1>,<value2>,<value3> To <value4>Statement

Case Else

Statement

End Select

Page 18: Fundamentals of visual basic

Example:Dim nGrade As Integer

nGrade = Val(Text1.Text)

Select Case ngrade

Case Is >= 90

Text2.Text = “A”

Case 80 To 89

Text2.Text = “B”

Case 70 To 79

Text2.Text = “C”

Case 60 To 69

Text2.Text = “D”

Case Is < 60

Text2.Text = “F”

End Select

Page 19: Fundamentals of visual basic

Repetition Structure

Page 20: Fundamentals of visual basic

Looping Statements

- Is a program instruction that repeats some statement or sequence of statements in a specified number of times.

- Allows a set of instructions to be performed all over and over again until a certain condition is reached, met, proven or tested as false or true.

- Allows us to execute one or more lines of code repetitively.

Page 21: Fundamentals of visual basic

6 Types of Repetition Control Structures

1. While/Wend

2. Do

2.1Do While/Loop

2.2 Do/ While Loop

2.3 Do Until/Loop

2.4 Do/ Until Loop

3. For / Next

Page 22: Fundamentals of visual basic

1. While/WendSyntax:

Index

While [condition]

Statement/s

Wend

Page 23: Fundamentals of visual basic

Parts

Index – a user defined numeric variable that the loop uses as a counter .

Condition- Optional. Boolean expression. If condition is Nothing, Visual Basic treats it as False.

Statement- Optional. One or more statements that are repeated while, or until, condition is True.

Step - is an optional keyword indicating the loop should step .

Page 24: Fundamentals of visual basic

Example:

Cnt = 1

While cnt<11

Print cnt;

Cnt = cnt + 1

Wend

Page 25: Fundamentals of visual basic

2. Do 2.1Do While/Loop

Syntax:

Index

Do While [Condition]

Statement/s[Step: Increment or Decrement]

Loop

Page 26: Fundamentals of visual basic

Example:

cnt = 1

Do While cnt <11

Print cnt

cnt = cnt +1

Loop

Page 27: Fundamentals of visual basic

2.2 Do/ While Loop

Syntax:

Index

Do

Statement

[Step: Increment or Decrement]

Loop While [Condition]

Page 28: Fundamentals of visual basic

Example:

cnt = 1

Do

Print cnt;

cnt = cnt +1

Loop While cnt<1

Page 29: Fundamentals of visual basic

2.3 Do Until/Loop Syntax:

Index

Do Until [condition]

Statement

[Step: Increment or Decrement]

Loop

Page 30: Fundamentals of visual basic

Example:

cnt = 1

Do Until cnt = 11

Print cnt;

cnt = cnt +1

Loop

Page 31: Fundamentals of visual basic

2.4 Do/ Until Loop Syntax:

Index

Do

Statement

[Step: Increment or Decrement]

Loop Until [Condition]

Page 32: Fundamentals of visual basic

Example:

cnt=1

Do

Print cnt;

cnt = cnt + 1

Loop Until cnt = 11

Page 33: Fundamentals of visual basic

3. For / NextParts:

The For is the keyword you use to start the loop.Index1

- is a user defined numeric variable that the loop uses as a counter .start

-is the number from which the loop starts from .

Page 34: Fundamentals of visual basic

To

- is the keyword which separates the start and end numbers .

end

- is the number at which the loop stops.

Step(Keyword)

- is an optional keyword indicating the loop should step .

Page 35: Fundamentals of visual basic

step

- is the size of increment / decrement the step should have (this can be a negative number)Next

- is the keyword that completes the loop .index2

- is used to identify which index is updated by the Next keyword .

Page 36: Fundamentals of visual basic

Syntax:

For index1 = start To end [Step step]

[ Statements ]Next [index2]

Sample:

For cnt=1 To 10

Print cnt;

Next cnt

Page 37: Fundamentals of visual basic

End of the slidesEnd of the slides

“Your value is not who you are and what you have, but how other people have become

because of you”

-

Page 38: Fundamentals of visual basic

End of the slidesEnd of the slides

“Heaven is not for people who are better than the rest, but for

people who sincerely try to become better than who

they are..”

- John 3:3