0% found this document useful (0 votes)
63 views

Today: Looping - Part 1

The document discusses loops in computer programming. It explains that loops allow programs to repeat instructions over and over. There are two main types of loops: event-controlled loops, which repeat until a certain event occurs, and count-controlled loops, which repeat a specific number of times. The document examines while and do-while loops in detail. It provides examples of how to write while and do-while loops using pseudocode. It also discusses the risk of infinite loops and provides an example of one. Finally, it previews an upcoming looping example and provides some optional coding shortcuts for students.

Uploaded by

blufus
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views

Today: Looping - Part 1

The document discusses loops in computer programming. It explains that loops allow programs to repeat instructions over and over. There are two main types of loops: event-controlled loops, which repeat until a certain event occurs, and count-controlled loops, which repeat a specific number of times. The document examines while and do-while loops in detail. It provides examples of how to write while and do-while loops using pseudocode. It also discusses the risk of infinite loops and provides an example of one. Finally, it previews an upcoming looping example and provides some optional coding shortcuts for students.

Uploaded by

blufus
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

COMPUTER SCIENCE

20S

Today: Looping – Part 1


Looping...
Computer Programs are good at repeating the
same thing over and over...
Computer Programs are good at repeating the
same thing over and over...
Computer Programs are good at repeating the
same thing over and over...
Computer Programs are good at repeating the
same thing over and over...
Computer Programs are good at repeating the
same thing over and over...
Loops (cont’d)
Loops allow you to repeat one or more
instructions over and over
Loops are another control structure that
can be incorporated to control program flow
Your programming preferences and the type
of program will determine how many times
the loop will execute
We have already seen this structure:
Repetition Control
Repeat
Structure
False
For Condition
example…
True

Loop Action

Next Action
Loops (cont’d)
Each repetition of the loop is usually controlled
by the outcome of a conditional statement
which will evaluate to true or false
Then inside the body of the loop, something
will occur that may (or may not) cause the testing
condition to evaluate differently the next time
through the loop
Once the condition evaluates to false the loop is
exited and the next statement following the loop
is executed
Loops (cont’d)
Loops can occur in two ways:
Event controlled loops are used when
you don’t know when to stop a loop (an
event – e.g. the end of a file) will trigger the
end of the loop
Count controlled loops are used when
you need to execute the loop a specific
number of times (e.g. X times)
Loops (cont’d)
The event controlled No
loop... Yes
Are you at
Stop when you
the end of
reach the end the file?
of the file..
Read Check
another again
piece of
the file

Go To the next code statement


Loops (cont’d)
The count controlled No
loop... Yes
is
Count to 5,
x = x + 1
x >= 5
x = 1
Check
x = 1 again
x = 2
x = 3
x = 4
x = 5 Next code statement
Students let us examine ...

THE while LOOP…


The while Loop
This structure is used to continue executing code as long
as a condition is true
This loop is the first form of the while loop
Event controlled loops come in two forms:
1. When you want a test first at the top of the loop
2. When you want the code body executed at least once
and then tested at the bottom of the loop

Loops are always tested with a Boolean expression


With logic, we can also combine logical
expressions (using and && , or || , not ! )
For Example

Notice that
like if
statements,
loops are
indented

The body of the Loop’s code block


executes five times. On the 6th time,
number is greater than 5, so we exit
the loop (the condition evaluates to
false)
The while Loop
while loops always begin with the while
keyword, then round braces ( ) with a boolean
expression, (a true/false test) inside
Then the loop’s code body, contained within
braces { } which define the loop block
Loops like conditional statements indent the
code block 1 tab space
Also, as a (not recommended) option, if the
code block is only 1 line (statement), the curly
braces { } may be omitted
Students let us examine ...

A VARIATION ON THE while


LOOP…
The do...while Loop
A variation of the while loop adds the do
keyword and a different structure
This loop is the second form of the while
loop, when you want the code body
executed at least once and then tested at
the bottom of the loop instead of the top
It has a stricter syntax and strangely adds a
semicolon ; to the end of the loop
Seen here…
The do...while Loop (cont’d)

 The do keyword is first


 Then the braces { } containing the code block
 Then the while(test) statement
 Then the semicolon ; ends this loop style
 This variation on the while loop is less prominent in coding
Students let us examine ...

WATCH FOR INFINITE LOOPS…


Infinite Loops...
Infinite loops are loops that never
reach a false condition
Therefore, they never stop executing
The run infinitely forever
This is usually the result of a logic
error in programming
Find how this occurs in this
Infinite Loop example...

This never becomes false, because number is


initially –1, and never becomes positive as the
Loop body keeps making number more and more
negative, and thus, the Loop never ends...
Which Loop is better?
Use the loop that is appropriate for the
circumstance that you need in your code
Sometimes the logic dictates that a
do...while loop is more appropriate
that a while... Loop, and visa versa
In part 2 of this unit, we will learn the
second type of loop (the count controlled
loop)
Some
Bonus
Code and
C#
Objects
(for use in the unit
problems as well as
your further coding)
Some coding shortcuts
These shortcuts are not totally necessary,
for students still struggling with the coding
so far – these may add to the confusion
and may take longer to ‘process’ – and
therefore may not be recommended
But, for some students these might make
your coding shorter, simpler, and easier
Multiple Variable Declarations
To this point, we have been declaring variables each
on a separate line
However to save space, you can declare multiple
variables on the same line
The syntax for doing this is as follows...
Math shortcuts
Since adding 1 to a variable is done so often, you can
use the shortcut ++ to always add 1 to a variable…

Even more advanced shortcuts can be used to


shorten the math statements…
Modulus Division
Modulus is a special math operation that gives you the
remainder from a division and not the answer
In C# the symbol for modulus is %
This can have use for some problems, but
for some students is difficult to grasp, for
example, 20 divided by 7 is 2, with a
remainder of 6
Therefore 6 = 20 % 7
TextBox Trim() and Focus()
Using TextBox objects and
string variables can be
improved using Trim() and
Focus() as options:
List and Combo Boxes
The ListBox object is something you can add to
your form, they allow the user to select from a set
of values
If the ListBox gets too many values, a scroll bar
will appear, for example:
List and Combo Boxes (cont’d)
You can add items to the ListBox using code like this:
List and Combo Boxes (cont’d)
Combo boxes work in a similar manner, but the user
can select from a drop down list, and can enter values
not in the list
Students let us examine ...

LET’S DO OUR FIRST


LOOPING EXAMPLE …
Looping Example 1 …
Please create a form (frmExample1) containing:

1. Button (btnFind)
2. ComboBox (cboValues)
3. Label (lblMax)
4. ListBox (lstNumbers)
5. Label (lblDisplay)
Looping Example 1 (cont’d)

 To give clues for the unit problems, as well as reviewing


some concepts, please open your code and type this into
the form load code (by double clicking the form)
Looping Example 1 (cont’d)

 Add this code to the button…


COMPUTER SCIENCE
20S
Please move
on to today’s
assignment

9/16/19

You might also like