260 likes | 385 Views
CSE 1341 Principles of Computer Science I. Spring 2008 Mark Fontenot mfonten@engr.smu.edu. Note Set 4. Note Set 4 Overview. Blaze through chapter 4 and 5 of text Control Structures. Increment and Decrement Operators. //What’s the output? int x = 5; System.out.printf (“%d”, x++);
E N D
CSE 1341Principles of Computer Science I Spring 2008 Mark Fontenot mfonten@engr.smu.edu Note Set 4
Note Set 4 Overview • Blaze through chapter 4 and 5 of text • Control Structures
Increment and Decrement Operators //What’s the output? int x = 5; System.out.printf(“%d”, x++); System.out.printf(“%d”, ++x);
do…while int x = 0; do { System.out.printf(“%d “, x); x++; } while (x < 10); • Post-test loop • checks condition after exactly one iteration of the loop
for loop for (inti = 0; i < 10; i++) Loop Control Variable – special variable that is in scope for the body of the loop… cannot be accessed after the loop body. Condition Loop Control Variable Update Remember: You only need braces around the body if it contains more than one Java statement. You can however always put them. • Pre-Test Loop • Checks the condition before the body executes
Variable Declarations Outer Block public static void main (String [] args) { int x = 3; for (inti = 0 ; i < 10; i++) { x++; System.out.printf(“%d “, x); } System.out.printf(“%d “, i); } This is OK! Inner Block Syntax Error – i is out of scope Variables are visible (in scope) in the block in which they are declared and any blocks inside that block
For Loops in Depth: public static void main (String [] args) { inti = 0; for (; i < 10; i++) { x++; System.out.printf(“%d “, x); } } Usually a bad idea to put a semicolon here You can leave parts of the for loop out – make sure you put the semicolon. Putting a semicolon after a for loop header is usually a syntax error All part of for loop header are not required.
For Loop Activity Diagram From Textbook, P 193
Exercise Print all integers that are a multiple of 3 and are between -29 and 84 inclusive…. in reverse order …. with a for loop
Example: Calculating Compound Interest • A person invests $1000 in a savings account yielding 5% interest. Assuming that all the interest is left on deposit, calculate and print the amount of money in the account at the end of each year for 10 years. Use the formula:a = p(1 + r)n • p is principal • r is the annual interest rate • n is the number of years • a amount on deposit at the end of the n-th year.
Interest Calculation public class Interest { public static void main( String args[] ) { double amount; double principal = 1000.0; double rate = 0.05; System.out.printf( "%s%20s\n", "Year", "Amount on deposit" ); for ( int year = 1; year <= 10; year++ ) { amount = principal * Math.pow( 1.0 + rate, year ); System.out.printf( "%4d%,20.2f\n", year, amount ); } } }
Do…While Activity Diagram From Textbook p 199
Switch Statement • Perform different actions based on the possible values of an integer variable or expression • Add a method to GradeBook that will allow the user to enter grades. The method should accumulate the sum and count of grades entered into instance variables. • Add a method to GradeBook that will allow the user to enter the grades for all student on a given exam. It should count the number of As, Bs, Cs, Ds, and Fs. Store the counts of the respective letter grades as instance variables
GradeBook public class GradeBook { private String courseName; private int total; private intgradeCounter; private intaCount; private intbCount; private intcCount; private intdCount; private intfCount; //All the other fun stuff new instance variables
Input Grades public void inputGrades() { Scanner input = new Scanner( System.in ); int grade; // grade entered by user System.out.printf( "%s\n%s\n %s\n %s\n", "Enter the integer grades in the range 0-100.", "Type the end-of-file indicator to terminate input:", "On UNIX/Linux/Mac OS X type <ctrl> d then press Enter", "On Windows type <ctrl> z then press Enter" ); while (input.hasNext() ){ grade = input.nextInt(); total += grade; ++gradeCounter; incrementLetterGradeCounter(grade); } }
Switch Stmt for Grades public void incrementLetterGradeCounter (int grade) { switch ( grade / 10 ) { case 9: case 10: aCount++; break; case 8: bCount++; break; case 7: cCount ++; break; case 6: dCount ++; break; default: fCount++; break; //Not actually needed, but won’t hurt } } Integer or expression that evaluates to integer. No Relational Ops Cases must be constant integer literals or integers. NO RELATIONAL OPS.
Switch Activity Diagram Example This diamond is not needed. See pg: 206.
Break System.out.println(“Before”); for (inti = 0; i < 10; i++) { System.out.printf(“%d\n”, i); if( i == 5) break; } System.out.println(“After”); Before 0 1 2 3 4 5 After • Break • when executed in a for, while, do..while, or switch cause immediate exit from that statement. • commonly used to exit from a loop if a condition is reached • You’ve seen it with switch stmts.
Continue System.out.println(“Before”); for (inti = 0; i < 10; i++) { if( (i % 2) == 0) continue; System.out.printf(“%d\n”, i); } System.out.println(“After”); Before 1 3 5 7 9 After • When executed in a loop, skips remaining statements in the loop body • While and Do…While – evaluates condition • For – performs update then evaluates loop condition
Creating Complex Conditions • Logical Operators • && (x > 3) && (y < 5) • Both conditions must evaluate to true for full expression to be true • || (x > 3) || (y < 5) • At least one of the conditions must be true for full expression to be true • ! !(x>3) • negates the truth value of a logical expression • Short Circuit Evaluation • Only evaluates enough of the expression to determine truth value • Assume Gender = MALE • if ((Gender == FEMALE) && (age > 30)) • No need to evaluate second part of expression . Why?
Tricky Situations: • (i != 0) && (10 / i == 2) • (i => 3) || ( a++ > 6) • Side effects • Other Logical Op Possibilities • & • | • same as && and || except force all parts of expression to be evaluated
Structured Programming Review p 216, textbook
Structured Programming Review P 219, Textbook
Refining a Structures Designs P219 of Text
Rogue Activity Diagram Drawing What’s wrong here?