Pseudocode Contents Output Variables Strings If then else

Pseudocode

Contents • • Output Variables Strings If. . then. . else AND-OR loop name from. . to. . loop while… loop until. .

output “……. ” , ……. • With that command, we print on screen whatever is in the quotation marks, exactly as it is written. – Example: output “Welcome” – Prints: Welcome • Or we can print on screen simple calculations. – Example: output 1+2+3+4+5+6+7+8+9+10 – Prints: 55 • Or we can print on screen both. – Example: output “Adding 1. . . 10 = “ , 1+2+3+4+5+6+7+8+9+10 – Prints: Adding 1. . . 10 = 55

Variables • Variable could be any letter or word. There are some “bad variable names” that we should not use such as: NOT, NOTHING. We use variables for more complicated calculations. – Example: X =1 Y =2*X+1 output Y – Prints: 3

*Solve the problem: • We have 2 numbers (A and B) and we want to add them and multiply them. – Suggestion: A=10 and B=35 • Now try to solve this equation : x^2 - 9 x + 14 = 0 – Suggestion: use 2 variables (X and Y) and put your own numbers at X, Y to try solve it. (ex. X=4) – Suggestion: X^2 = X*X

String • A STRING is a value that contains letters and maybe numbers and punctuation marks - e. g. any characters on a keyboard. – Example: name =“Maria” output name – Prints: Maria

INPUT : the input command makes a box pop-up on the screen, waits for the user to type an answer, and then stores the answer in a variable. IF. . THEN. . : checks whether a variable matches a specific value. If so, the program executes the command(s) between then…end if. Notice that the user is going to type numbers or letters. But if the user is to type letters, you need "quotation marks" around the matching STRING.

if…. . then…. . end if X = input ("type a number") Y = X*X - 9*X + 14 if Y<0 then output Y, "is not the solution" end if if Y>0 then output Y, "is not the solution" end if if Y=0 then output Y, "is the solution" end if X = input ("type a number") Y = X*X - 9*X + 14 if Y<0 then output Y, "is not the solution" else if Y>0 then output Y, "is not the solution" else output Y, "is the solution“ end if

if. . then. . else if. . else with STRING • This program knows a few German words. If you choose an English word from the list, it will tell you the matching German word. • It knows the following words in German: hello goodbye stop input ENGLISH if ENGLISH = "hello" then GERMAN = "guten Tag" else if ENGLISH = "goodbye" then GERMAN = "auf Wiedersehen" else if ENGLISH = "stop" then GERMAN = "halt" else GERMAN = "? ? ? " end if output "English = " , ENGLISH output "German = " , GERMAN

AND-OR • • The computer can perform more complex LOGIC by using BOOLEAN OPERATORS like AND and OR. For example, consider the problem of inputting a user name and password. Both the account name and the password must be correct. So the NAME AND PASSWORD must be correct. NAME = input("Type your user name") PASSWORD = input("Type your password") if NAME = "bozo" AND PASSWORD = "clown" then output "Correct!" end if if NAME = "einstein" AND PASSWORD = "e=mc 2" then output "Correct!" end if if NAME = "guest" OR NAME = "trial" then output "You will be logged in as a GUEST" end if

• Computers can also perform LOGICAL operations, meaning that they can do COMPUTATIONS involving calculations PLUS making LOGICAL decisions. • An example: where the price of buying items changes if you buy a larger quantity - called a QUANTITY DISCOUNT. QUANTITY = input("How many hamburgers do you want? ") if QUANTITY >= 10 then PRICE = 2. 59 else if QUANTITY <= 9 AND QUANTITY >= 5 then PRICE = 2. 89 else if QUANTITY < 5 then PRICE = 3. 25 end if output "That costs " , PRICE , " per burger" output "Total cost = " , PRICE * QUANTITY , " for " , QUANTITY , " burgers"

* Solve the problem : • Make program which reads a STARTING time and a FINISH time. Then it calculates the number of elapsed minutes between those two times. For example: START_HOURS = 8 START_MINUTES = 30 END_HOURS = 10 END_MINUTES = 10 MINUTES = (10 -8)*60 + (10 -30) = 100

Solution input START_HOURS input START_MINUTES input END_HOURS input END_MINUTES if START_HOURS > 23 OR START_MINUTES > 59 then output "Start time is not valid" else if END_HOURS > 23 OR END_MINUTES > 59 then output "Times are not valid" else MINUTES = (END_HOURS - START_HOURS)*60 + (END_MINUTES-START_MINUTES) output "Elapsed time = " , MINUTES , " minutes" end loop

loop name from. . to. . X=0 loop A from 1 to 5 X =X+1 output X end loop ---------------------------------------------------X=0 loop B from 1 to 5 output X X =X+1 end loop ---------------------------------------------------- • • This loop repeats itself 5 times. It counts automatically. Try both programs and see the difference.

loop while… C=5 loop while C < 20 output C C = C*2 end loop -------------------------------- • It repeats itself until C is bigger than 20.

loop until. . NUM = 29 loop until NUM = 1 output NUM =NUM-1 end loop output NUM --------------------------------------------- • It repeats itself (the loop) until NUM equals 1, then it stops and execute the others commands (under the loop).

• This program prints all the common factors of A and B. A = 28 B = 42 --------------------------------------output "Common factors of " , A , " and " , B loop C from 1 to B if (A mod C = 0) AND (B mod C = 0) then output C end if end loop

* Solve the problem: • Make a program which adds up a lot of numbers : 1 + 2 + 3 +. . . + max e. g. max=10

Solution MAX = 10 SUM = 0 loop COUNT from 0 to MAX output COUNT SUM = SUM + COUNT end loop output "Total = " , SUM

Resources • https: //dl. dropboxusercontent. com/u/275979 /ibcomp/pseduocode/pcode. html
- Slides: 20