Download presentation
Presentation is loading. Please wait.
Published byJoel Stevens Modified over 9 years ago
1
Spring 2008 Mark Fontenot mfonten@engr.smu.edu CSE 1341 Principles of Computer Science I Note Set 4
2
Note Set 4 Overview Blaze through chapter 4 and 5 of text Control Structures
3
Increment and Decrement Operators //What’s the output? int x = 5; System.out.printf(“%d”, x++); System.out.printf(“%d”, ++x);
4
do…while Post-test loop checks condition after exactly one iteration of the loop int x = 0; do { System.out.printf(“%d “, x); x++; } while (x < 10);
5
for loop Pre-Test Loop Checks the condition before the body executes for (int i = 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.
6
Variable Declarations Variables are visible (in scope) in the block in which they are declared and any blocks inside that block public static void main (String [] args) { int x = 3; for (int i = 0 ; i < 10; i++) { x++; System.out.printf(“%d “, x); } System.out.printf(“%d “, i); } Syntax Error – i is out of scope Outer Block Inner Block This is OK!
7
For Loops in Depth: Putting a semicolon after a for loop header is usually a syntax error All part of for loop header are not required. public static void main (String [] args) { int i = 0; for (; i < 10; i++) { x++; System.out.printf(“%d “, x); } You can leave parts of the for loop out – make sure you put the semicolon. Usually a bad idea to put a semicolon here
8
For Loop Activity Diagram From Textbook, P 193
9
Exercise Print all integers that are a multiple of 3 and are between -29 and 84 inclusive…. in reverse order …. with a for loop
10
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.
11
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 ); }
12
Do…While Activity Diagram From Textbook p 199
13
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
14
GradeBook public class GradeBook { private String courseName; private int total; private int gradeCounter; private int aCount; private int bCount; private int cCount; private int dCount; private int fCount; //All the other fun stuff new instance variables
15
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 d then press Enter", "On Windows type z then press Enter" ); while ( input.hasNext() ) { grade = input.nextInt(); total += grade; ++gradeCounter; incrementLetterGradeCounter(grade); }
16
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.
17
Switch Activity Diagram Example This diamond is not needed. See pg: 206.
18
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. System.out.println(“Before”); for (int i = 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 Before 0 1 2 3 4 5 After
19
Continue 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 System.out.println(“Before”); for (int i = 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 Before 1 3 5 7 9 After
20
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?
21
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
22
Structured Programming Review p 216, textbook
23
Structured Programming Review P 219, Textbook
24
Refining a Structures Designs P219 of Text
25
Rogue Activity Diagram Drawing What’s wrong here?
26
?
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.