210 likes | 304 Views
Learn how to efficiently incorporate selection and repetition into nested structures for improved programming. Explore examples and self-check exercises.
E N D
CMP 131Introduction to Computer Programming Violetta Cavalli-Sforza Week 11
THIS WEEK • Today • Finish loops • Quiz results & going over .. • Tomorrow (Tuesday): LAB 01 • Work on homework #5 • Wednesday: Lecture • Start on File Processing • Thursday: LAB 01 • More work on homework #5
NEXT WEEK • Quiz #4 • Wednesday May 30th • No make-up session but possibly start classes 15 minutes early and end 15 minutes late (since you have ½ hour breaks)
Nested Selection and Repetition • Selection (IF-THEN-ELSE, CASE) and repetition (FOR-DO, WHILE-DO, REPEAT-UNTIL) are commonly occurring programming structures • They are frequently nested within each other: • Selection inside repetition • Repetition inside selection
Selection Inside Repetition • We could have done Fibonacci’s problem differently (not necessarily better) by incorporating the function in the loop:
Previous Program: Fibonacci PROGRAM Fibonacci; CONST Number = 20; VAR J , {counter variable} N1, N2, N3 {counter variable} : integer; BEGIN N1 := 0; writeln('Fibonacci 0:', N1:5); N2 := 1; writeln('Fibonacci 1:', N2:5); FOR J := 2 TO Number – 1 DO BEGIN N3 := N1 + N2; writeln('Fibonacci',J:3,':',N3:5); N1 := N2; N2 := N3; END; readln END.
New Program: Fibonacci PROGRAM Fibonacci; CONST Number = 20; VAR J , {counter variable} N1, N2, N3 {counter variable} : integer; BEGIN FOR J := 0 TO Number-1 DO IF J = 0 THEN BEGIN N1 := 0; writeln('Fibonacci 0: 0') END ELSE IF J = 1 THEN BEGIN N2 := 1; writeln('Fibonacci 1: 1') END ELSE BEGIN N3 := N1 + N2; writeln('Fibonacci',J:3,':',N3:5); N1 := N2; N2 := N3; END; readln END. Repeated at each iteration of the loop!
Selection Inside Repetition • If unnecessary, avoid it. It makes for inefficient programs • May be necessary and part of the design, e.g. Selecting a menu option inside a loop.
PROGRAM MenuSelect; VAR Option : char; BEGIN REPEAT {print menu of options} writeln; writeln('You have the following options:'); writeln(' 1 : to perform action 1'); writeln(' 2 : to perform action 2'); writeln(' ....'); writeln(' 9 : to perform action 9'); writeln(' Q : to QUIT'); {read user choice} write('Your choice: ') ; readln(Option); {handle user choice} CASE Option OF '1'..'9' : writeln('Performing action', Option:2); 'Q' : ; ELSE writeln('Unknown option', Option:2,'. Try again.') END; {CASE} UNTIL Option = 'Q' END.
PROGRAM MenuSelect; VAR Option : char; BEGIN REPEAT {print menu of options} writeln; writeln('You have the following options:'); writeln(' 1 : to perform action 1'); writeln(' 2 : to perform action 2'); writeln(' ....'); writeln(' 9 : to perform action 9'); writeln(' Q : to QUIT'); {read user choice} write('Your choice: ') ; readln(Option); {handle user choice} IF ((Option >= ‘1’) AND (Option <= ‘9’)) OR (Option = ‘Q’) THEN CASE Option OF '1'..'9' : writeln('Performing action', Option:2); 'Q' : ; END {CASE} ELSE writeln('Unknown option', Option:2,'. Try again.') UNTIL Option = 'Q' END.
Repetition Inside Selection • Example: • Menu for printing out different types of pictures • ExampleMenu for accessing other menus:(selection inside repetition + repetition inside selection)
Selection Inside Repetition + Repetition Inside Selection • Example: Menu for accessing other menus • Each menu is embedded inside a loop: selection inside repetition • Each selection contains a menu within a loop: repetition inside selection
REPEAT {top-level menu} display options read user choice {handle user choice – CASE or IF-THEN-ELSE stmt} CASE Option OF choice1 : REPEAT display options read user choice handle user choice UNTIL exit from menu1 ... choiceN : REPEATdisplay options read user choice handle user choice UNTIL exit from menuN END; {CASE} UNTIL exit top level menu END.
Self-Check 1 • What is displayed by the following program fragment, assuming N is 5? FOR I := 1 TO N DO BEGIN FOR J := 1 TO I DO write(‘*’); writeln END;
Self-Check 2 • What is displayed by the following program fragment, assuming M is 3 and N is 5? FOR I := N DOWNTO 1 DO BEGIN FOR J := M DOWNTO 1 DO write(‘*’); writeLn END;
Self-Check 3 • Show the output printed by the following nested loops: FOR I := 1 TO 2 DO BEGIN writeln(‘Outer’ : 5, I : 5); FOR J := 1 TO 3 DO writeln(‘Inner’ : 7, I : 3, J : 3) FOR K := 2 DOWNTO 1 DO writeln(‘Inner’ :7, I : 3, K : 3) END;
Self-Check 4 Write a program fragment that, given an input value N, displays N rows in the form 1 2 ... N, 2 3 ... N + 1, and so forth.
PROGRAM Check; VAR I,J,N : integer; BEGIN write('What is N? '); read(N); FOR I := 1 TO N DO BEGIN FOR J := I TO I + N - 1 DO write(J:2); writeln END; readln END.
Self-Check 5 Write a program that prints a nicely labeled multiplication table for the digits 1 through 9.
PROGRAM MultiplicationTable; VAR I,J : integer; BEGIN FOR I := 1 TO 9 DO BEGIN FOR J := 1 TO 9 DO write(I*J:3); writeln END; readln END.
Quiz Results • Above Median: 60.5 49.5 46.5 43 40 40 37.5 • Median: 35 • Below Median: 26.5 22 21.5 21 16.5 11.5 11 • Mean: 32.13333333 • Total Points: 80 • N = 15