Download presentation
Presentation is loading. Please wait.
Published byNoreen Butler Modified over 8 years ago
1
Selection Using IF THEN ELSE CASE Introducing Loops
2
Making a choice or deciding something. Conditions can be true or false Age=18 (simple condition) Month>=1 AND Month <=12 (complex condition) AND, OR, NOT
3
Algorithm IF age=18 THEN I can vote ELSE I can ’ t vote END IF IF Month >=1 AND Month<=12 THEN process date ELSE Display error message END IF
4
Algorithm An Algorithm is a sequence of instructions that can be used to solve a problem.
5
Grade algorithm using IF – IF pupil’s test mark is greater than or equal to 80 THEN Grade is grade 1 ELSE IF pupil’s test mark is greater than or equal to 60 THEN Grade is grade 2 ELSE IF pupil’s mark is greater than or equal to 40 THEN Grade is grade 3 ELSE IF pupil’s mark is greater than or equal to 20 THEN Grade is grade 4 ELSE IF pupil’s test mark is greater than or equal to 10 THEN Grade is grade 5 ELSE Grade is grade 6 END IF
6
CASE pupil’s test mark OF WHEN greater than or equal to 80 Grade is grade 1 WHEN greater than or equal to 60 Grade is grade 2 WHEN greater than or equal to 40 Grade is grade 3 WHEN greater than or equal to 20 Grade is grade 4 WHEN greater than or equal to 10 Grade is grade 5 OTHERWISE Grade is grade 6 END CASE
7
Repetition Unconditional loops Conditional Loops
8
Unconditional Loops Repeats a set of program statements for a predetermined number of times Controlled by a variable called a loop counter Loop counter counts the number of times the loop structure is to be repeated between the two limits set at the start of the loop
9
FOR counter equals start number TO finish number do something NEXT counter Eg: Private Sub Command1_Click() Dim word As String word = Txtwordin.Text For Counter = 1 To 5 PicDisplay.Print "The word is "; word Next End Sub
10
Algorithm to display a name five times Start loop FOR one to five times display a name End loop
11
Algorithm to display a name a number of times Ask use how many times name is required Take in the number of times name is required Start loop FOR one to number of times Display name End loop
12
Algorithm showing an unconditional loop with steps Start loop FOR two to thirty times STEP 2 display loop counter End loop
13
Algorithm showing nested loops Start down loop FOR one to five times start across loop FOR ten to twenty times display a star character end across loop End down loop
14
Conditional Loops Manages situations where the number of times repetition must take place is not known in advance Calculation can continue until an answer is found More than one exit condition may be used Test at start Test at end
15
While loop – test at start WHILE condition is true do something END WHILE
16
REPEAT – Test at end REPEAT do something UNTIL condition is TRUE
17
Examples Loop REPEAT ask a user to enter a word take in a word UNTIL the word “end” is entered
18
Loop REPEAT display message “Press Space Bar” take in a character UNTIL the character “ ” is entered
19
Sum numbers algorithm using a running total Set total to zero Set number to zero Loop REPEAT make total equal to total+number ask user to enter a number take in a number UNTIL number equals –999 Display total
20
Quiz Question Algorithm Get question Get user’s answer Loop REPEAT ask question take in response UNTIL response equals correct answer
21
Algorithm with test at start Take in a word Loop WHILE the word is NOT “end” take in a word End loop
22
Input Validation with test at start Ask user for data input Take in the data Loop WHILE data input is out with range prompt user to re-enter data take in the data End loop
23
Recursion Recursion, like looping, is a programming technique that causes an operation or set of operations to be repeated. Looping requires the programmer to specify, among other things, how many times to repeat a step and how much space to reserve in memory for the results of each operation – a level of detail that can be inefficient if not impossible to provide. In looking for relationships in the large varying databases characteristic of artificial intelligence programs for example, the programmer often cannot declare all conditions in advance. Recursion lets the programmer focus on finding the solution to the problem rather than the mechanics of implementing the solution. More on this when we look at PROLOG programming language.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.