Lecture 10
Online Portal http://structuredprogramming.wikispaces.com/
Menus A menu is a list of functions that a program can perform; the user chooses the desired function from the list. E.g., 1 convert to US dollar 2 convert to pounds 3 convert to Euro 4 convert to Yen
Example CLS PRINT INPUT Enter number of Ghana cedis to be convertred, Cedis REM Display the menu PRINT PRINT Please enter one of the following numbers: PRINT TAB(4); 1 - Convert to US dollar PRINT TAB(4); 2 - Convert to pounds PRINT TAB(4); 3 - Convert to Euro PRINT TAB(4); 4 - Convert to Japanese Yen INPUT Code PRINT
REM Perform the conversion and display result CASE 1 Result = Cedis / 1.54 PRINT There are ; Result US dollars in ; cedis GHC CASE 2 Result = Cedis / 2.25 PRINT There are ; Result Pounds in ; cedis GHC CASE 3 Result = Cedis / 3.12 PRINT There are ; Result Euros in ; cedis GHC CASE 4 Result = Cedis * 0.98 PRINT There are ; Result Japanese Yen in ; cedis GHC REM If invalid code was entered, display error message CASE ELSE PRINT Invalid conversion number END SELECT END
Logical Operations An operation that acts on one or more conditions to produce a value of true or false The three most commonly used logical operators are AND, OR, and NOT. The operator AND combines two expressions and produces a value of true only when both of these conditions are true. E.g., IF (Score> 75) AND (Time < 50) THEN PRINT Nme$ END IF
Example A program to print out You passed in CSC 200/201 if score is greater than or equal to 40 and less than 50, else print Not applicable. INPUT Enter your Exam score ; Score IF Score >=40 AND Score <50 THEN PRINT You passed in CSC 200/201 ELSE PRINT Not applicable END IF END
Logical Operations (cont.) The logical operator OR also combines two expressions, but only one of the expressions needs to evaluate as true for the entire statement to be true. E.g., IF (Score> 75) OR (Time < 50) THEN PRINT Nme$ END IF
Logical Operations (cont.) The third logical operator, NOT, is a unary operator (an operator used with only one operand) and is used with a single expression The effect of the NOT is to reverse (negate) the logical value of the expression that it precedes. E.g., NOT (A<B)
Example Write a program to determine the type of triangle. REM REM side1 side3 length of each side CLS INPUT Enter the three sides, side1, side2, side3 PRINT This triangle is ; IF (side1 = side2) AND (side2 = side3) THEN PRINT equilateral ELSEIF (side1 = side2) OR (side2 = side3) OR (side1 = side3) THEN PRINT Isosceles ELSE PRINT scelene END IF END
Repetitive /Loop Control Structurest This design strategy instructs the interpreter to execute a set of instructions repeatedly for a finite number of times Looping is the process of repeating a series of statements as many times as needed The DO LOOP 1. DO WHILE LOOPLOOP 2. DO UNTIL LOOP
The DO WHILE LOOP LOOP A DO WHILE LOOP is executed as long as the expression at the top of the loop is true. If it is false, control transfers to the first statement after LOOP, which makes the loop end
Example A program to add a list of positive integers CLS Sum = 0 INPUT Enter the first number (-1 to quit: ), Number DO WHILE Number <> -1 Sum = sum + Number INPUT Enter the next number (-1 to quit: ), Number LOOP PRINT The Sum is ; Sum END
Controlling Loops Trailer values It is a data value indicating that a loop should stop executing It must be a value that would not ordinarily occur in the input data. The trailer value can be either a numeric value or a character string.
Example A program to calculate employees salaries Rate = 8 INPUT Enter name and number of hours:, Nme$, Hours DO WHILE Nme$ <> Finished Wage = Rate * Hours PRINT Name, Wage PRINT Nme$, wage PRINT INPUT Enter next name and number of hours:, Nme$, Hours PRINT LOOP PRINT Finished END
Controlling Loops Counting Loop A 2 nd method of controlling a loop is to create a loop control variable, called a counter, that t keeps track of the number of times the loop has been executed I ti l th t i i t d In counting loop, the counter is incremented or decremented during each repetition
Example of an infinite it loop Count = 1 DO WHILE Count < 50 PRINT Count LOOP
Infinite it loop, corrected Count = 1 DO WHILE Count < 50 PRINT Count Count = Count +1 LOOP
Example Rate = 8 DO WHILE Counter <= 3 INPUT Enter name and number of hours:, Nme$, Hours Wage = Rate * Hours PRINT Name, Wage PRINT Nme$, wage PRINT Counter = Counter + 1 LOOP PRINT Finished END
The DO UNTIL LOOP The 2 nd type of DO LOOP, the DO UNTIL LOOP is very similar to the DO WHILE LOOP. LOOP The only difference is that the loop is executed until the logical expression becomes true rather than while it is true
Example 1 Rate = 8 INPUT Enter name and number of hours:, Nme$, Hours DO UNTIL Nme$ = Finished Wage = Rate * Hours PRINT Name, Wage PRINT Nme$, wage PRINT INPUT Enter name and number of hours:, Nme$, Hours LOOP PRINT Finished END
Example 2
The EXIT statement t t Sometimes, it becomes necessary to exit a loop prematurely. This occurs commonly when an error condition is encountered. E.g., invalid data entry
Example A program to prompt user to enter names of 12 voters Count = 1 INPUT Enter person s name:, Voter$ INPUT Enter person ss age:, Age DO WHILE Count <= 12 REM If underage, display error message and stop loop IF Age <18 PRINT PRINT Voter$; is not eligible to vote. EXIT DO END IF Count = Count +1 INPUT Enter person s name:, Voter INPUT Enter person s name:, Voter LOOP
Assignment 6 1. (a) What is the output of the program segment : X = 7 DO WHILE X <= 1 PRINT X X = X 1 LOOP 1. (b) Write a DO WHILE LOOP that t counts by fives until a given value (limit) is reached. E.g., if value of limit is 49, the output should be similar to the following: 5 10 15 20 25 30 35 40 45
The FOR NEXT Loop The FOR NEXT is used to create a counting loop E.g., FOR X = 1 TO 6 PRINT X; NEXT X END
Find out more examples