1 / 21

Lecture 5: Layers of Control

Lecture 5: Layers of Control. Nested while Loops. Problem Multiplying two numbers and outputting the result only if they are both less than 5. (i.e. Start at i=1, j=1 and go from there) Formulate pseudocode algorithm i = 1 j = 1: 1 * j = j = 2: 1 * j =

eburch
Download Presentation

Lecture 5: Layers of Control

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 5: Layers of Control

  2. Nested while Loops • Problem Multiplying two numbers and outputting the result only if they are both less than 5. (i.e. Start at i=1, j=1 and go from there) • Formulate pseudocode algorithm • i = 1 j = 1: 1 * j = j = 2: 1 * j = j = 3: 1 * j = j = 4: 1 * j = • i = 2 j = 1: 2 * j = j = 2: 2 * j = j = 3: 2 * j = j = 4: 2 * j = j = 1; While j is less than 5 print out the result of 1 * j; j = j + 1; j = 1; While j is less than 5 print out the result of 2 * j; j = j + 1;

  3. Nested while Loops • Formulate pseudocode algorithm • i = 3 j = 1: 3 * j = j = 2: 3 * j = j = 3: 3 * j = j = 4: 3 * j = • i = 4 j = 1: 4 * j = j = 2: 4 * j = j = 3: 4 * j = j = 4: 4 * j = j = 1; While j is less than 5 print out the result of 3 * j; j = j + 1; j = 1; While j is less than 5 print out the result of 4 * j; j = j + 1;

  4. Formulate Pseudocode Algorithm • i = 1 • i = 2 • i = 3 • i = 4 j = 1; While j is less than 5 print out the result of 1 * j; j = j + 1; i j = 1; While j is less than 5 print out the result of 2 * j; j = j + 1; i = 1; While i is less than 5 i = i + 1; i j = 1; While j is less than 5 print out the result of 1 * j; j = j + 1; i j = 1; While j is less than 5 print out the result of 3 * j; j = j + 1; i j = 1; While j is less than 5 print out the result of 4 * j; j = j + 1; i

  5. Pseudocode i = 1; While i is less than 5 i = i + 1; j = 1; While j is less than 5 print out the result of i * j; j = j + 1;

  6. C Code

  7. Nested Control Structures • Problem A college has a list of test results (1 = pass, 2 = fail) for 10 students. Write a program that analyzes the results as follows: • Input each test result (i.e., a 1 or a 2). Display the prompting message “Enter result” each time the program requests another test result. • Count the number of test results of each type • Display a summary of the test results indicating the number of students who passed and the number who failed • If more than 8 students passed the test, print the message “Raise tuition.”

  8. Before Formulate the Algorithm • The program must process 10 test results • Counter-controlled loop will be used • Two counter can be used • One for number of passes, one for number of fails • Each test result is a number -- either a 1 or a 2 • If the number is not a 1, we assume that it is a 2 • Decide if more than 8 students passed the test.

  9. Formulate the Pseudocode Algorithm • Top level outline Analyze exam results and decide if tuition should be raised • First refinement Initialize variables Input the ten quiz grades and count passes and failures Print a summary of the exam results and decide if tuition should be raised • Refine Initialize variables to Initialize passes to zero Initialize failures to zero Initialize student counter to one

  10. Formulate the Pseudocode Algorithm • Refine Input the ten quiz grades and count passes and failuresto While student counter is less than or equal to ten Input the next exam result If the student passed Add one to passes else Add one to failures Add one to student counter • Refine Print a summary of the exam results and decide if tuition should be raisedto Print the number of passes Print the number of failures If more than eight students passed Print “Raise tuition”

  11. Pseudocode for Test Results Analysis

  12. Initializing variables when they are defined can help reduce a program’s execution time. while loop continues until 10 students have been processed if and else statements are nested inside while loop C code for Test Results Analysis

  13. C code for Test Results Analysis

  14. Assignment Operators • Assignment operators abbreviate assignment expressions c = c + 3 can be abbreviated as c += 3; using the addition assignment operator “+=” • Statements of the form variable=variableoperatorexpression; can be rewritten as variableoperator=expression; • Examples of other assignment operators: d -= 4 (d = d - 4) e *= 5 (e = e * 5) f /= 3 (f = f / 3) g %= 9 (g = g % 9)

  15. Arithmetic Assignment Operators

  16. Increment and Decrement Operators • Increment operator (++) Can be used instead of c += 1 • Decrement operator (--) Can be used instead of c -= 1 • Pre-increment/Pre-decrement • Operator is used before the variable (++c or --c) • Variable is changed before the expression it is in is evaluated • Post-increment/Post-decrement • Operator is used after the variable (c++ or c--) • Expression executes before the variable is changed

  17. Increment and Decrement Operators • If c equals 5, then • printf( "%d", ++c ); Prints 6 • printf( "%d", c++ ); Prints 5 • In either case, c now has the value of 6 • When variable not in an expression • Preincrementing and postincrementing have the same effect ++c; printf( “%d”, c ); Has the same effect as c++; printf( “%d”, c );

  18. Increment and Decrement Operators

  19. c is printed, then incremented c is incremented, then printed Increment and Decrement Operators

  20. Precedence of the Operators

  21. In-Class Programming Exercise Create a program that prints out a triangle with a user specified width. Example output: Enter the specified width:5 X XX XXX XXXX XXXXX XXXX XXX XX X

More Related