Lecture 5 Layers of Control Nested while Loops

  • Slides: 21
Download presentation
Lecture 5: Layers of Control

Lecture 5: Layers of Control

Nested while Loops Problem Multiplying two numbers and outputting the result only if they

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 = 2: 1 * j = 3: 1 * j = 4: 1 * j = i=2 j = 1: 2 * j = 2: 2 * j = 3: 2 * j = 4: 2 * 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;

Nested while Loops Formulate pseudocode algorithm i=3 j = 1: 3 * j =

Nested while Loops Formulate pseudocode algorithm i=3 j = 1: 3 * j = 2: 3 * j = 3: 3 * j = 4: 3 * j = i=4 j = 1: 4 * j = 2: 4 * j = 3: 4 * j = 4: 4 * 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;

Formulate Pseudocode Algorithm i=1 j = 1; While j is less than 5 print

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

Pseudocode i = 1; While i is less than 5 j = 1; While

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

C Code

C Code

Nested Control Structures Problem A college has a list of test results (1 =

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. ”

Before Formulate the Algorithm The program must process 10 test results Counter-controlled loop will

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.

Formulate the Pseudocode Algorithm Top level outline Analyze exam results and decide if tuition

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

Formulate the Pseudocode Algorithm Refine Input the ten quiz grades and count passes and

Formulate the Pseudocode Algorithm Refine Input the ten quiz grades and count passes and failures to 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 raised to Print the number of passes Print the number of failures If more than eight students passed Print “Raise tuition”

Pseudocode for Test Results Analysis

Pseudocode for Test Results Analysis

C code for Test Results Analysis Initializing variables when they are defined can help

C code for Test Results Analysis 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

C code for Test Results Analysis

Assignment Operators Assignment operators abbreviate assignment expressions c=c+3 can be abbreviated as c +=

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 = variable operator expression; can be rewritten as variable operator= expression; Examples of other assignment operators: d e f g -= *= /= %= 4 5 3 9 (d = d - 4) (e = e * 5) (f = f / 3) (g = g % 9)

Arithmetic Assignment Operators

Arithmetic Assignment Operators

Increment and Decrement Operators Increment operator (++) Can be used instead of c +=

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

Increment and Decrement Operators If c equals 5, then printf( "%d", ++c ); Prints

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 );

Increment and Decrement Operators

Increment and Decrement Operators

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

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

Precedence of the Operators

Precedence of the Operators

In-Class Programming Exercise Create a program that prints out a triangle with a user

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 XXXX XX X