Professional Documents
Culture Documents
By Mbogo Njoroge
1
2
The IF statement
Conditional Statements The C++ syntax of the if statement is:
• A conditional statement lets us choose which statement If ( logicalCodnition) Then
will be executed next // Block of code to execute if expression is TRUE;
• Therefore they are sometimes called selection statements End If
• Conditional statements give us the power to make basic
decisions
• The VB conditional statements are the:
– If statement If
true
Condition
?
– If … Else statement
Block of code exc.
– If … ElseIf statement false
– Select statement
If (a < b) Then
if true if true
MessageBox.Show( “a = “ & a & “b = “ & b &
condition
condition
?
? VbCrLf “a is smaller than b” )
End If
Block of code
Block of code
exc.
false exc.
false End Sub
7 8
If ( logicalCondition) Then
T // Block1 of code to execute if condition is true;
false if true
Else
condition
F // Block2 of code to execute if condition is false;
?
End If
9
10
false if true
condition MessageBox.Show(“Both a and b are positive” & VbCrLf &
T
“a - b = “ & (a – b) & VbCrLf &
? “ b - a = ” ( b – a) )
Else
Block2 of Block1 of
stmts MessageBox.Show(“Either or both a and b are < = zero:”;
stmts
executes & VbCrLf & “a + b = ” & (a + b) & VbCrLf
executes F “b x a = ” & ( b * a ) )
End If
MessageBox.Show(“Hongera kwa ujuzi wako wa HESABU”)
End Sub
11 12
3/2/2024
15 16
17 18
3/2/2024
Repetition/Loop Statements
Most Common Uses of Loop Statements
• Repetition statements allow us to execute a statement or a block
of statements multiple times
• Iteration or looping refers to the specified sequence in which a • The repetitive/loop statements are used for one or
pattern or a block of code is repeated until the condition becomes some of the following tasks:
no longer true. That is, as long as the condition remains true, the
loop is executed repeatedly. The repetitive cycle is terminated – counting
when the condition becomes false.
– accumulating, i.g. summing
• VB has three kinds of repetition statements:
– searching
i. While … Wend
– sorting
ii. Do … Loop (in two variations/flavours While and Until)
iii. For … Next – displaying tables
• Basic Components of Loop structures are; – data entry – from files and users
– Loop control variable: A variable used to determine whether a loop – menu processing
will be executed – list processing
– Loop body: The statement (s) that are executed each time a loop
repeats
• The programmer should choose the right kind of loop statement
for a particular situation
21 22
23 24
3/2/2024
Assignment TASK
F Draw a complete flowchart to depict the logic of the above event procedure
Amend the event procedure code so as to display the SUM of numbers and the SUM of the squires as
the last items in the two list boxes respectively designated as; TOTAL = ?_
25 26
Similarities & differences between pre-condition Logical Similarities & differences between pre-condition
Do While … Loop and pre-condition Do Until … Loop Do While … Loop and pre-condition Do Until … Loop
Do While … Loop Do Until … Loop Do While … Loop Do Until … Loop
Syntax: Syntax: The loop Semantics Pseudcode The loop Semantics Pseudcode
Do While(condition) Do Until(condition)
statements to execute statements to execute 1.Test the condition 1.Test the condition
repeatedly as long as the repeatedly as long as the
condition remains TRUE condition remains FALSE 2. If condition is TRUE execute action 2. If condition is FALSE execute action
Loop Loop 3. Repeat step 1 process 3. Repeat step 1 process
29 30
3/2/2024
NB:- The main logical operational difference between these two is that the
Do While … Loop statement iterates as long as the condition is true and the
Do Until … Loop statement iterates as long as the condition is false
31 32
NB:- The main logical operational difference between these two is that the
Do … Loop While statement iterates as long as the condition is true and the
Do … Loop Until statement iterates as long as the condition is false
33 34
• Both the post-condition Do … LOOP While and Do … LOOP This do while loop will prompt the user
Private Sub frmShowNum_Click( ) for a numeric data item one or more
Until are exit-controlled loop(why?). times until a time he/she enters a
Dim numYako As Single NEGATIVE number for the iterations to
• Both gives the user the provision to execute the loop action at least
Dim numSum As Single stop
once, which is useful for checking user input among other things
numSum = 0 Note the Exit Do statement , ina
umuhimu upi?(What is its purpose?)
numYako = 0
• In both;
First, the body of the loop is executed, then the condition is tested Do
for truth or falsity numYako = InputBox("Enter +ve No. to CONTINUE or –ve No. to STOP “)
• In theory and infact the loop body can be either a single statement If (numYako < 0) Then
or a block of statements Exit Do
End If
• If the execution of the body statements of a Do Loop strictly numYako = numSum + numYako
depends on the condition, you should use either the pre-condition Loop While (numYako >= 0)
Do While … Loop OR the pre-condition Do Until … Loop
However, if the body statements of a Do Loop must be executed at MessageBox.Show(“ The sum of your numbers is: “ & numSum)
least once or if it is inconsequential whether the condition is End Sub
checked before entering the loop, Then use either post-condition
Do … Loop While OR the post-condition Do … Loop Until
35 36
3/2/2024
Infinite Loops
• The Endless Loop
• Notice that in the previous structures, some of the statements
• In a Do … Loop statement the While/Until
inside the loop must produce some result clauses and their associated condition are
• that causes the condition to be False in co optional; that is, the entire Do structure
• njunction with the keyword While, or True in conjunction with
can do without any While/Until condition.
• the keyword Until; otherwise, the loop will execute endlessly,
resulting in an undesirable trap. Eg, it is perfectly legitimate to have a
• Thoroughly test your program to prevent this situation from Do...Loop structure as follows:
happening.
Do
• A loop must have some way to end itself ‘Statements to be executed
• Something within the body of the loop must eventually force
the test expression to false
Loop
• In the previous example
– The loop continues to repeat
– intCount increases by one for each repetition
– Finally (intCount <= 10) is false and the loop ends
• If the test expression can never be false, the loop will
continue to repeat forever
– This is called an infinite loop 39 40
The For … Next repetitive statement The For … Next statement operational Logic
The general syntax/format of the For … Next loop and its • A For … Next loop is counter-controlled, meaning that it is normally
corresponding flowchart are shown below used whenever the number of iterations is known in advance.
For counter = startingValue To terminalValue Step incrementValue • Within the header of the For loop the loop counter initialization,
terminal value condition, and counter increment are specified
statements to execute repeatedly as long as the terminal
condition remains TRUE
• Note that For, To, Step, and are keywords
Next counter • The loop counter increment value associated with the Step clause can
be either a positive or a negative value; if the Step clause is omitted,
automatically 1 is by default qualified as the increment value.
A Closer Look at the Loop Index Increment value For ... Next loop with positive and negative
loop counter/index increment value
Kumbuka:- the loop counter/index increment value can be either
positive or negative.
49
F. Eventually the FINAL appreciation
screen pops up
Which Loop do I use AND under which under Controlling the Loop Iterations
circumstances
• If number of time action is to be repeatedly executed is NOT known • Sentinel Value
AND there is a possibility of the action not being executed at all, with – For all loop statement/structure type, the loop continuation
NO possibility of enforcing an early/premature EXIT use the While … condition usually determine when to end the loop by making some
Wend loop. comparison to a Sentinel value
• If number of time action is to be repeatedly executed is NOT known – The sentinel value serves as the test value or comparison criteria for
AND there is a possibility of the action not being executed at all, BUT ending the loop
with a possibility of enforcing an early/premature EXIT then use the
• In counted loops this is usually the number of times to loop
pre-condition Do … Loop While or the Do … Loop Until statement.
• In event controlled loops this is the state or value that the event
• If it is important that the action MUST be executed at least ONCE, with variable is compared to in the test expression
a possibility of enforcing an early/premature EXIT, then use either the
post-condition Do … Loop While or the post-condition Do … Loop • Loop Counter /index
Until statement – A numeric variable that keeps track of or counts the number of
• If it is necessary that the action MUST be executed at least ONCE, with iterations a loop repeats, items that have been processed, or some
a possibility of enforcing an early/premature EXIT, then use either the other repeated occurrence within a loop
post-condition Do … Loop While or the post-condition Do … Loop
Until statement • Accumulator
• If you know ( or can calculate ) the number of times an action is to be – A numeric variable that is used to hold a sub-total that is computed
repeatedly executed, with a possibility of enforcing an during multiple passes/iterations through a loop
early/premature EXIT then use the counter-controlled For … Next
loop. 51 52
• Event-controlled loops
– repeat until something happens within the loop body to change
the value of loop control variable
– Events can be: User Input, Expression results, Function results,
etc.
53