• No results found

VB Control Structures.ppt

N/A
N/A
Protected

Academic year: 2020

Share "VB Control Structures.ppt"

Copied!
65
0
0

Loading.... (view fulltext now)

Full text

(1)
(2)

Control Flow

In a program, statements may be executed

sequentially, selectively or iteratively. Every programming language provides constructs to support sequence, selection or iteration. So there are three types of programming

constructs :

Sequential Constructs

Selection Constructs

(3)

Sequential Construct

The sequential construct means the

statements are being executed sequentially. This represents the default flow of

statements.

Stament 1

Stament 2

(4)

Selection Construct

The selection construct means the execution

of statement(s) depending upon the

condition-test. If a condition evaluates to true, a course-of-action (a set of statements) is

followed otherwise another course-of-action is followed. This construct is also called

(5)

Selection Construct

Condition

? Statement 1 Statement 2

Statement 2 Statement 1

One course-of-action

Another course-of-action true

(6)

Iterative Constructs

The iterative or repetitive constructs

means repetition of a set-of-statements

depending upon a condition-test. A

set-of-statements are repeated again and

again till the condition or Boolean

Expression evaluates to true. The

(7)

Iterative Construct

Condition ?

Statement 2

Statement 1 The loop

body false

The exit condition

(8)

8

Essentials of Counter-Controlled

Repetition

Elements needed

Control variable

Used to determine whether loop continues to iterate

Initial value of control variableIncrement (or decrement)

Describes how control variable is modified during each

iteration

Condition

(9)

Selection Constructs

VB provides two types of selection construct :

1) If statement

2) Select Case statement

The If Statement : If statement of VB comes in

various forms & are given below:

1) If..Then Statement

2) If..Then..Else Statement

3) If..Then..ElseIf Statement

(10)

If..Then Statement

Def. : An If..Then statement tests a particular

condition; if the condition evaluates to true, a course-of-action is followed otherwise it is

ignored.

Syntax :

If (boolean expression) Then statements

(11)

If..Then Statement

Example 1. :

If (Num>0) Then

msgbox “It is a positive number” End if

Example 2 :

If txtAge.Text>=18 Then

(12)

If..Then..Else Statement

If..Then..Else statement provides an alternate choice

to the user i.e. if the condition is true then a set of statements are executed otherwise another set of statements are executed.

Syntax :

If (boolean Expression) Then VB Statement(s)

Else

(13)

Examples of If..Then..Else

Example 1 :

If txtAge.Text>=18 Then

Msgbox “You are eligible to vote” Else

Msgbox “Sorry, You are not eligible to vote” End If

Example 2 :

If Num Mod 2=0 Then

msgbox “It is an Even Number” Else

(14)

If..Then..ElseIf Statement

• If..Then..ElseIf statement is used to test a number of mutually exclusive cases and only executes one set of statements for the case that is true first.

Syntax :

If (Boolean Expression) Then Statement(s)

ElseIf (Boolean Expression 2) Then Statement(s)

ElseIf (Boolean Expression 3) Then Statement(s)

:

[ Else

(15)

Example of If..Then..ElseIf

If (Age<=4) Then

msgbox “Your rate is free.” ElseIf (Age<=12) Then

msgbox “You qualify for the children’s rate.” ElseIf (Age<65) Then

msgbox“You must pay full rate” Else

(16)

Nested Ifs

A nested If is an if that has another If in its if’s body

or in its else’s body. The nested if can have one of the following three forms :

1. If (expresssion 1) Then If (expression 2 ) Then Statement 1

[Else

Statement 2 End If

Else

(17)

Nested Ifs

2. If (expression 1) Then body-of-if

Else :

If (expression 2) Then Statement-1

[Else

(18)

Nested If’s

3)If (expression 1) Then :

If (expression 2) Then Statement-1 [Else

Statement-2] End If

Else

(19)

Example of Nested If’s

If Num>0 Then

Print “It is a positive number” Else

If Num<0 Then

Print “It is a negative number” Else

Print “The number is equal to zero” End If

(20)

Select-Case Statement

Select-Case is a multiple branching statement

and is used to executed a set of statements

depending upon the value of the expression. It is better to use Select-Case statement in

comparison to If..Then..ElseIf Statement when the number of checks are more. There are 3 different forms of using Select-Case

(21)

Different forms of Select-Case

1. Select Case : Simplest Form [Exact match] Select Case Expression

Case Value

’one or more visual basic statements Case Value

’one or more visual basic statements Case Else :

(22)

Example of Form 1

Select Case byMonth Case 1,3,5,7,8,10,12

number_of_days=31 Case 2

number_of_days=28 Case 4,6,9,11

(23)

Syntax of Form 2

Select Case : Second Form [Relational Test] Select Case Expression

Case is relation :

’one or more visual basic statements Case is relation :

’one or more visual basic statements [Case Else :

(24)

Example of Form 2

Select Case marks Case Is < 50

Result = “Fail” Case Is < 60

Result = “Grade B” Case Is < 75

Result = “Grade A” Case Else

(25)

Third Form of Select Case

Select Case : Third Format [Range Check] Select Case Expression

Case exp1 To exp2:

’one or more visual basic statements Case exp1 To exp2:

’one or more visual basic statements Case Else:

(26)

Example of Form 3

Select Case Age

Case 2 to 4 : msgbox “PreNursery”

Case 4 to 6 : msgbox “Kindergarden”

Case 6 to 10 : msgbox “Primary”

(27)

Home Assignment 1

Write a program in VB to compare three no’s

and print the smallest number among these no’s.

Write a program in VB to check the eligibilty

of a person to vote. Display a message “You are eligible to vote” if the age of the person is greater than or equal to 18 otherwise print

“Sorry! You are not eligible to vote”

Write a program to compare two no’s and

(28)

Home Assignment

Write a program to display the grade obtained

by the child according to the marks obtained by him/her. Criteria for assigning the grades is given below : If marks are

>=90 - Grade is A

(29)

Iterative Constructs (Looping

Structures)

Loop : A loop is said to be the set of

instructions which are repeated again and again in a program.

Types of Loops in VB :

1) Sentinel-controlled Loop Structures : repeat statements until a special value called sentinel value (or the terminating value) is reached.

(30)

Looping Structures

VB offers broadly following three types of

looping structures : 1. For..Next

2. Do Loop

(31)

31

For

/

Next

Repetition Structure

For/Next counter-controlled repetition

Structure header initializes control variable, specifies final

value and increment

For keyword begins structure

Followed by control variable initialization

To keyword specifies final valueStep keyword specifies increment

Optional

Increment defaults to 1 if omittedMay be positive or negative

Next keyword marks end of structure

(32)

For..Next Statement

This type of statement is used when the user

knows in advance how many times the loop is going to be executed.

Syntax :

For <counter Variable>=<start_val> To <end_val> Step <increment/Decrement Value>

(33)

Examples

Example 1 : Generate natural no’s from 1 to

100

For I = 1 To 100 Print I

Next I

Example 2 : Generate first 20 even no’s.

For E = 2 to 40 Step 2 Print E

(34)

More Examples

Example 3 : Generate odd no’s from 100 to 30

in a list box.

For O = 99 to 31 Step -2 ListO.AddItem(O)

Next O

Example 4 : Generate table of any number N.

For T = 1 To N

(35)

More Examples

Example 5 : Find factorial of a given number N.

:

Fact=1

For I= 1 to N

Fact = Fact * I Next I

(36)

Home Assignment 2

Write Programs for the following problems :

1) To Generate all the natural no’s from 200 to 500.

2) To Generate the table of 10.

3) To Generate all the even no’s from 200 to 400 in reverse order.

(37)

Do..Loop Structures

Do While..Loop : Do While loop is an entry

controlled loop in which the condition is placed at the entry point. This statement

executes the statements specified in the body of the loop till the condition evaluates to true. The loop may not be executed at all the if the condition is initially false.

Syntax :

Do While <condition or boolean expression> ‘ One or more VB Statements

(38)

Examples of Do While..Loop

Example 1 : Never executes loop

Dim A as Byte A=10

Do While A>10 A=A-1

Loop

Example 2 : Executes loop

Dim P as Byte P=20

Do While P>5 P=P-2

(39)

Do..Loop While

Do Loop While is an exit controlled loop as the

condition is placed at exit point. The body of the loop is going to be executed at least once whether the condition evaluates to true or

false. Loop is executed as long as the result of the condition remains true.

Syntax :

Do

One or more VB Statements

(40)

Examples

Example 1 :

Do

num = InputBox (“Enter a number”) sum = sum + num

Loop While num < > 0

Here the statements inside the loop will be

(41)

Do..Until Loop

Do Until loop is an entry controlled loop in which the

condition is placed at the entry point. This statement executes the statements specified in the body of the loop till the condition evaluates to false. The loop

may not be executed at all the if the condition is initially true.

Syntax :

Do Until <condition or boolean expression> ‘ One or more VB Statements

(42)

Examples of Do Until..Loop

Example 1 : Never executes loop

Dim A as Byte A=10

Do Until A<10 A=A-1 Loop

Example 2 : Executes loop

Dim P as Byte P=20

Do Until P<5 P=P-2

(43)

Do..Loop Until

Do Loop Until is an exit controlled loop as the

condition is placed at exit point. The body of the loop is going to be executed at least once whether the condition evaluates to true or

false. Loop is executed as long as the result of the condition remains false.

Syntax :

Do

One or more VB Statements

(44)

Examples

Example 1 :

Do

num = InputBox (“Enter a number”) sum = sum + num

Loop Until num = 0

Here the statements inside the loop will be

(45)

While..Wend

While..Wend loop is functionally equivalent to

the Do While..Loop. It executes a set of VB

statements till the condition evaluates to true.

Syntax :

While <Condition>

(46)

Examples

Example 1 : Generate the sum of first 10

natural no’s I = 1

While I<=10

Sum = Sum + I I = I + 1

Wend

(47)

Nested Loops

A loop within another loop is called as Nested

Loop.

Example :

For I = 1 to 5 For J = 1 To 3 Print J

Next J Print Next I

(48)

Working of Nested Loops

In nested loops the inner loop is executed

completely for one time execution of the

outer loop. In the Previous example the Inner Loop will be executed three times for every execution of the outer loop.

Nested loops are very useful when there is a

(49)

Examples

Example : Program to generate the output given below :

1 2 2 3 3 3 4 4 4 4 5 5 5 5 5Sol :

For I = 1 To 5 For J = 1 To I Print I;

Next J Print

(50)

Home Assignment 3 – Nested Loops

Write programs to generate the following patterns :

1) 1 2) 1 2 3 4 5 1 2 1 2 3 4 1 2 3 1 2 3

1 2 3 4 1 2 1 2 3 4 5 1

(51)

Home Assignment 4 – Output Related

Questions

Specify the output of the following output related

questions, assume that variables are declared :

1) sum=0 2) sum = 0 For I = 1 To 4 p=2 : q=4

For J = 1 To 3 Do While p<=10

sum = sum + J If p Mod 2 = 0 Then Next J sum = sum + p

Print “Sum = ”;sum End If Next I p=p+1

Loop

(52)

Output related questions

3) R=5 : S=3 4) M=2 : N=4

Do Do Until M>12

Print R, S N = N + M

R = R + 1 If M Mod 3 = 0 Then

If R = 7 Then N = N - M

Exit Do Else

End If N = N + M

S = S + R End If

Loop While R <=10 M=M+1

Loop

(53)

Home Assignment - 5

Programming Questions : Develop programs for the

followings :

To generate the mirror image of the number entered

by the user.

• To generate the prime numbers between 100 and 700.

To Check whether the number entered by the user is a

prime number or not.

To print first 30 multiples of the number entered by the

user.

To generate the factorial of the number input by the

(54)

Home Assignment - 5

To generate the Fibonacci series upto first 15 terms

i.e. 0 1 1 2 3 5 8…………..

To find the sum of all the numbers divisible by 7 from

100 to 200.

To check whether the year entered by the user is

leap year or not.

To find and print the sum of digits of the number

entered by the user.

To find and print the product of digits of the number

(55)

55

Logical Operators

Used to form complex conditions by

combining simple ones

Short-circuit evaluation

Execute only until truth or falsity is known

AndAlso operator

Returns true if and only if both conditions are true

OrElse operator

(56)

56

Logical Operators (II)

Logical Operators without short-circuit evaluation

And and Or

Similar to AndAlso and OrElse respectivelyAlways execute both of their operands

Used when an operand has a side effect

Condition makes a modification to a variableShould be avoided to reduce subtle errors

Xor

Returns true if and only if one operand is true and the other

(57)

57

Logical Operators (III)

Logical Negation

Not

Used to reverse the meaning of a conditionUnary operator

– Requires one operand

Can usually be avoided by expressing a condition

(58)

58

Logical Operators

Operators Associativity Type

() left to right parentheses

^ left to right exponentiation

+ - left to right unary prefix

* / left to right multiplicative

\ left to right Integer division

Mod left to right modulus

+ - left to right additive

& left to right concatenation

< <= > >= = <> left to right relational and equality

Not left to right logical NOT

And AndAlso left to right boolean logical AND

Or OrElse left to right boolean logical inclusive OR

(59)

MsgBox

MsgBox(msg,style,title)

MsgBox("jhj", 2, "hello“)

0 vbOkonly Display Ok only Button

1 vbokCancel Display Ok & cancle2 Button

2 Display Abort, Retry,& Ignore Button 3 Display yes , no and cancel Button 4 Display yes and no Button

5 Display Retry and cancel Button 16 vbCritical Display Critical msg Button

(60)

InputBox

inputbox(what is u r msg,title,defalt

value,position)

Syntax:

Microsoft.visualbasic.inputbox(“Enter name",

(61)
(62)
(63)
(64)
(65)

References

Related documents

First is executed the statement 1 (normaly initialize a variable) and is check the condition, if the condition is false, the loop ends. But if it is true, the code 1

IF-THEN-ELSE statement can target be used in VBA code It executes one dream of code if a specified condition evaluates to TRUE or get set of code if it evaluates to FALSE

)epeats a &amp;loc* of statements #hile a condition is True True or until a  or until a condition &amp;ecomes.. condition &amp;ecomes

condition Is a Boolean variable or expression (TRUE, FALSE, or NULL) statement Can be one or more PL/SQL or SQL statements. If the variables involved in the conditions do not

Definition and divide The ifelse statement executes a excuse of code if a specified condition of true If the upcoming is false another martyr of code can be executed The

The if Flowchart boolean_expression statement (body of loop) true false... The while Flowchart boolean_expression statement (body of loop ) true

The Python ternary operator is a conditional expression that executes code depending on whether a condition is true or false.. The if block statement is executed if condition is

o uses booleans (true and false) as loop conditions; continues looping as long as condition is true, but when boolean is false, loop condition equals exit condition and loop