COMPUTER TRAINING: Conditional statements and Loop structure

Monday, 24 December 2012

Conditional statements and Loop structure

Conditional statements and Loop structure
 
If...Then..Statement
 
The If...Then statement is used to evaluate whether a condition is True or False.
You can use If…Then block to execute one or more statements conditionally.
 
Syntax:
If <condition> Then
[statements]
Else
[else statements]
End If
 
Example:
 
if a>5 Then a=a+10
Here condition is that 'a' should be greater than 5 and if it is true then 'a' should be incremented by 10.
<condition>:-The condition is usually a comparison, but it can be any expression that evaluates to a numeric value.
 
[Statements] :- One or more statements separated by colons; executed if condition is True.
 
Note that:
 
1.A single line of If…Then does not use End If statement.
 
Syntax:-
If condition Then statement.
But if you use this in more than one line ,then syntax will be If…Then…End If.
 
2.If anything other than a comment appears after Then on the same line, the statement is treated as a single line If statement.
 
3.A block If statement must be the first statement on a line. The block If must end with anEnd If statement.
 
4.When a If condition is tested ,then If condition is True, the statements following Then are executed,otherwise not.
 
 
 
 
Conditional statements and Loop structure
 
If...Then..Else Statement
 
It conditionally executes a group of statements depending upon value of the expression.
If...Then…Else block is used to define several blocks of statements ,in order to execute one block.
 
Syntax:
If <condition>then
[statements]
Else if <condition>Then
[else if statement] . . .
Else
[elsestatement]
End If
 
Example:
If a<10 then
a=a+10
elseif a>15 then
a=a-10
else
a=100
end if
 
Note that:
1.Both Else and ElseIf statements are optional. You can have any number of ElseIfstatements as you want in an If block. But no ElseIf statement can appear after the Else.
 
2.Block If statements can be nested. Nested means a block contained within one another.
 
3.In fact If…Then is just a special case of If…then…Else.
 
4.If none of ElseIf statement is True ,then the statement following Else will execute.
 
 
 
 
Conditional statements and Loop structure
 
Select Case Statement
 
It is just as the alternative of If…Then…Else statement.It executes one statement in several groups of statements.
 
Select Case provides the same functionality as the If…then…Else statement but it makes the code more efficient and readable
 
Select Case works with a single test expression.
Value of the statements will be dependent on test expression.
 
Syntax:
 
Select Case testexpression
[Case expression1
[statements-n]] . . .
[Case expression2
[statements-n]]
.
.
Case Else
Statement..n
End Select
<Testexpression>
Any numeric or string expression.
<statement>
Statement executes if Testexpression matches any part of expressionlist.
 
Example:
 
The following example describes the use of the Select Case statement-
 
Dim num as integer
Dim a(10) as integer
'Calculations containing num
Note "'" Indicates the Comment Line
Select Case num
Case 1 a(1)=num
Case 2 a(2)=num
Case 3 a(3)=num
Case else a(1)=num
End Select
 
Num is declared as integer variable here and a(10) is declared as an array of ten elements.
Select Case checks values of num, according to that it executes the corresponding case statement.
 
Note that:
 
1.Value of the statement will be dependent on expression.
 
2.The Case Else clause is used to indicate the elsestatements to be executed if no match is found between the testexpression and an expressionlist
 
3.Select Case statements can be nested.
 
4.Each nested Select Case statement must have a matching End Select statement.
 
 
 
 
Conditional statements and Loop structure
 
Do...Loop Structure
 
Do…loop is used to execute a block of statements for indefinite number of times. There are several variations of Do...Loop statement.
Each variation evaluates a numeric condition to determine whether to continue execution or not.
 
Syntax:
 
Do While <condition>
statements…
Loop
 
Note that:
 
1.When Visual Basic executes Do While loop, it first tests condition.
 
2.If condition is False (zero), it skips past all the statements.
 
3.If it's True (nonzero), Visual Basic executes the statements and then goes back to the Do While statement and tests the condition again.
 
Example:
 
Do While count<=5
count = count + 1
Loop
 
Here loop increments the value of count until it becomes equal or less than 5.
Another variation of Do...Loop statement executes the statement first and then tests condition after each execution. This variation guarantees at least one execution of statements.
 
Syntax:
 
Do
<statement>
Loop While <condition>
 
 
 
 
Conditional statements and Loop structure
 
For...Next Structure
 
For...Next loop is used when you know how many times you want to execute loop.But in case of Do While it is not the case.
Do While is used when you do not know how many times you want to execute that particular loop/Condition.
 
Syntax:
 
For counter = start To end [Step increment]
statements
Next [counter]
The arguments counter, start, end, and increment are all numeric.
 
In executing the For loop, Visual Basic:
 
1. Sets counter equal to start.
 
2. Tests to see if counter is greater than end. If so, Visual Basic exits the loop.
 
3. Executes the statements.
 
4. Increments counter by 1 or as it's specified.
 
5. Repeats steps 2 through 4.
 
Example:
 
Dim A(40) as Integer
For i = 0 To 40 step 1
A(i)=i
Next
Print A(9)
 
Here index values are stored in array a(40).Then it prints the value of a(9).
 
 
 
 

No comments:

Post a Comment