Agenda Remarks about last homework for loop Nested for loop Flowchart Debugging techniques breakpoint; variable trace; println Homework
Remarks on the last homework Do NOT declare variable inside the loop Good design needs some thinking and trying - if you want the user to play until he/she gets it right?...what if it is hard to be right? - the program quits when user enters some number? Is it the way we play games?
for Loop for Loop executes a set of statements a fixed number of times. The for statement takes the form: for (<initialization>; <condition>; <increment>) { <statements> } example for (int i = 1; i <= 10; i++) { System.out.println(i); How can this example change to equivalent while or do-while loop?
Practice for loop In each problem below state what is printed unless directed otherwise. 1. int j=0; for(int g=0; g< 5; g++) j++; System.out.println(j); int s=1; for(int j=3; j >+0; j--) { s = s+j; } System.out.println(s);
Nested for loop When one loop is placed inside another loop for(int j=0; j < 5; j++) { System.out.println(“outer loop”); for(int k = 0; k < 8; k++) { System.out.println(“inner loop”); } }
Flowchart It allows user to design the logical flow of a program without first getting into the details of specific code. Flowcharts use symbols and text to give a visual representation of a solution. indicates start or end. indicates input/ouput. indicates a decision indicates the process
The BirthdayGame flowchart Start birthDay = number %100 Display steps for calculating numbers Display player’s birthday Prompt user for number End Substract 165 from numbers birthMonth = number/100
Example of decision symbol yes no If (input number==secrete number)? Display “sorry, better luck next time” Display “you guess it”
Debugging Technique Debugging is the process of getting an application to work correctly. Additional println Adding println() statements just after a variable is assigned a new value or before and after a condition is evaluated can help detect the source of a logic error. Variable trace A variable trace is a table listing the values of variables at the points of assignment Debugger breakpoint
Println debugging example int num1 =0; int num2 = 0; while (num1 < 10) { if (num1 % 3 == 0) num2 = num2 + num1; System.out.println(num2 + “ “ ); } num1++
Variable trace num1 Num2 output 1 2 3 4 5 6 7 8 9
Practice for homework * ** *** **** ***** ****** ******* If we wanne print out the picture on the left, using the for loop?
Homework I. Draw a flowchart on the program gradeSystemConversion II. Using a for loop to print out a star triangle upside down like the following: ****** ***** **** *** ** *