Variable, constant, operators and control statement

Post on 22-May-2015

303 views 0 download

Tags:

Transcript of Variable, constant, operators and control statement

Variable, Constant, Operators and Control Statement

Variables• Programming element used to store a

value in the program while the program is running.

• It is a name area in the memory which holds temporary data.

EXAMPLE:Name = “Shaira”Age = text1.text

• EXPLICIT DECLARATION – declaring variable by typing DIM (dimension) statement and a variable name.

Syntax: Dim Variable Name As Data typeExample:Dim LastName As String

• IMPLICIT DECLARATION – declaring a variable without the Dim Statement; simply use the variable on its own.

Example:LastName=“Fernandez”

Constant• Meaningful Name that takes place of a

number or a string that does not change.• Values that do not change during the

execution of the program.Syntax: Const Constant Name As data type =

valueExample: const Pi As Single = 3.142Score= 100

Operators• Operators are symbols that indicates

operation to be performed on data.There are 3 types of Operators1. Arithmetic Operators2. Relational Operators3. Conditional Operators

Arithmetic Operators- Mathematical Operators that is used to compute inputs from users to generate results.

Relational Operators – Operators that is used to compare two values basing on a certain conditions yields a TRUE or FALSE result.

Logical Operators – Operators that determine if a particular condition is met.

NEXT

Operators Description Example Result

+ Add 5+5 10

- Substract 10-5 5

/ Divide 25/5 5

\ Integer Division (disregards the decimal places)

20\3 6

* Multiply 5*4 20

^ Exponent (power of) 3^3 27

Mod Remainder of division 20 Mod 6 2

& String concatenation "George"&" "&"Bush" "George Bush"

Operators Description Example Result

> Greater than 10>8 True

< Less than 10<8 False

>= Greater than or equal to 20>=10 True

<= Less than or equal to 10<=20 True

<> Not Equal to 5<>4 True

= Equal to 5=7 False

Operators Description

OR Operation will be true if either of the operands is true

AND Operation will be true only if both the operands are true

XOR One sides or other must be true but not both sides

NOT Negate truth

Condition of AND OperatorCONDITION 1 CONDITION2 RESULT

TRUE TRUETRUE FALSEFALSE TRUEFALSE FALSE

•If there’s a FALSE the result is false.

Condition of OR Operators

CONDITION 1 CONDITION2 RESULT

TRUE TRUETRUE FALSEFALSE TRUEFALSE FALSE

•If there’s a TRUE the result is true.

Condition of XOR OperatorCONDITION 1 CONDITION2 RESULT

TRUE TRUETRUE FALSEFALSE TRUEFALSE FALSE

•One sides or other must be true but not both sides

Condition of NOT Operator

CONDITION RESULT

TRUEFALSE

• Negate truth

Get a ¼ sheet of PaperFind the result

1. 9+7+82. 8^33. 65/134. 95 mod 9555. (-5)+(-4)

6. 10>17. .05>.58. 1500>=1501009.0>=0.0010.5<>10

Find the result

1. 10*10<100 And 12<>122. 500=500.0 Or 95-15<853.True And False4.Not False5.1000/100>77 And 96<>95

ANSWER KEY

• 1. 24• 2. 512• 3. 5• 4. 5• 5. -9

• 1. FALSE• 2. TRUE• 3. FALSE• 4. TRUE• 5. FALSE

•6. TRUE•7. FALSE•8. FALSE•9. TRUE•10. TRUE

CONDITIONAL STATEMENT

Conditional Statement

• It is one of the vital components in programming. It enables a program to respond in different manner every time a program is executed depending on the data entered.

Most Commonly Used Conditional Statement.

1. If.. Then Statement2. If.. Then.. Else Statement3. If.. Then.. ElseIf Statement4. Select Case Statement

If.. Then Statement• The If...Then statement examines the

truthfulness of an expression. It allows your Program to make a decision based on the certain condition.

• SYNTAX:If condition then

Statement/sEnd If

If.. Then.. Else Statement

• The If...Then statement offers only one alternative: to act if the condition is true. Whenever you would like to apply an alternate expression in case the condition is false, you can use the If...Then...Else statement.

If.. Then.. Else Statement

SYNTAX: If Condition Then

Statement1 Else

Statement2 End If

If Score = 100 Then Label1.caption =“Perfect”Else Label1.caption =“with Mistakes”End If

If...Then...ElseIf StatementSYNTAX:

If Condition1 Then Statement1

ElseIf Condition2 Then Statement2

ElseIf Condition 3 Then Statement 3

End If

The If...Then...ElseIf statement acts like the If...Then...Else expression, except that it offers as many choices as necessary.

SELECT CASE STATEMENT• If you have a large number of

conditions to examine, the If...Then...Else will go through each one of them. Visual Basic offers the alternative of jumping to the statement that applies to the state of the condition.

SyntaxSelect Case Expression Case Expression1

Statement1 Case Expression2

Statement2 Case Expression3 Statement3 End Select

Select Case Subject

Case “Mathematics” lblsubject.caption=“Mathematics”

Case “Science” lblsubject.caption=“Science”

Case “English” lblsubject.caption=“English”

End Select

LOOP Structure

• A loop is an expression used to repeat an action. Visual Basic presents many variations of the loops and they combine the Do and the Loop keywords.

Do...While Loop• Used to execute a block of statements in

an unspecified number of times while a condition is false on the first pass. The statement is not executed.

Syntax: Do while conditions

Statement/sloop

Dim Number As IntegerNumber = 10Do While Number <20 Number=Number+2 Print NumberLoop

Do...Loop...While Statement

Dim Number As IntegerNumber = 10Do Number=Number+2 Print NumberLoop While Number <20

Reverse formula of the do while statement. Syntax:Do Statement(s) Loop While Condition

Do...Until...Loop Statement• This loop will first examine the Condition,

instead of examining whether the Condition is true, it will test whether the Condition is false.

Syntax:Do Until Condition

Statement(s)Loop

Example:Dim Number As Integer

Number=30Do Until Number <=20

Number= Number-2Print NumberLoop

Do...Loop...Until Statement

• An alternative to the Do...Until...loop consists of executing the Statement first.

Syntax:Do Statement(s) Loop Until Condition

Example:Dim Number As Integer

Number=30Do

Number= Number-2Print NumberLoop Until Number <=20

Get a ¼ Sheet of PaperIdentify the following.1. Name area which holds temporary data.2. Operators that is used to compare two

values basing on a certain conditions.3. An expression used to repeat an action.4. Values that do not change during the

execution of the program.5. symbols that indicates operation to be

performed on data.

6. Declaring a variable without the Dim Statement.

7. A Data type that stores a value of True or False

8. Operators that determine if a particular condition is met.

9. Type of data that consists of numbers which can be manipulated with various standard operators.

10. Declaring variable by typing DIM (dimension) statement and a variable name.

Identify whether a variable is valid or invalid

1. Name 12. (The_variable)3. My_First_Name4. He&his_Father5.Long_Name_Can_beuse

ANSWER KEY

1. Variable2. Relational Operator3. Loop4. Constant5. Operators6. Implicit declaration7. Boolean8. Conditional operator9. Numerical data type10. Explicit declaration

1.Invalid2.Invalid3.Valid4.Invalid5.Valid