290 likes | 313 Views
Chapter 3 Decisions. Three control structures Algorithms Pseudocode Flowcharts If…then …else Nested if statements Code blocks { } multi statement blocks. Chapter 3 more Control Structures. switch statements multiple-selection Relational operators logical operators. Secondary concepts.
E N D
Chapter 3 Decisions • Three control structures • Algorithms • Pseudocode • Flowcharts • If…then • …else • Nested if statements • Code blocks { } multi statement blocks
Chapter 3 more Control Structures • switch statements multiple-selection • Relational operators • logical operators
Secondary concepts • Keywords • Condition operators (c?t:f) • Top down design aka step wise refinement • Compound or shortcut assignment operators • Increment and decrement operators • Pre increment (++x) and post increment (x++) • Primitive data types and sizes
NOTE: • Some versions of the JDK will require a class path be set in the DOS window Type this after setting the path. Note the period on the end is very important. • SET CLASSPATH=c:\jdk1.2.1\lib;.
Three control structures • Sequence – flow • The programs flow • Top down • Method calls • Selection – logic – conditional • Choose between alternate code blocks • If…then • Iterations - Repetition– loops • Perform a code block multiple times • While • For
Structured designAlgorithms • An algorithm is a solution to a problem. • To create a solution you must first understand the problem. • Then plan a solution. • Algorithms describe: • The actions to be taken • The order in which to perform the actions
Pseudocode • Informal (code like) language used to describe an algorithm. • Written in common language • Helps programmer think out the algorithm (design). • Includes only execution logic.
Pseudocode • A trivial example: • Get user input for radius • Compute diameter • Compute circumference and area • Display results • A single program may have many parts. • What is better one big pseudocode or many small ones? • Which is easier to read and unserstand
Flowcharts • Used to illustrate program flow Start initial state notes go here get radius transition lines Compute Diam Compute area action statements Display area final state End
Now comparisons • The comparison statement used in almost every programming language is the “If….then” statement • If (a==10) System.out.println(“A is 10”); • The word then is implied • A semicolon delimits the end of the if….then statement. • If (a=10) // may be a logic error • If (a<10); // may be a logic error
Logic operators • < • <= • > • >= • == • !=
The if…else statement if (condition) statement; • or if (condition) statement else statement; • or if (condition) { statements; …. } else { statements; }
Lets write the program to compare two numbers • We will use if statements…
Comparing strings • Strings are objects and must be compared using special methods. • s1.compareto(s2) • Returns negative if s1 is less than s2 • Returns 0 if s1=s2 • Returns positive if s1 is greater than s2 • S1.equals(s2) • Used to compare objects in general
Grades • if (grade>60) • system.out.print(“student passed”); get grade grade >60 yes Print “passed” no Do Other commands
Grades with else • if (grade>60) • system.out.print(“student passed”) • else • system.out.print(“student failed”); get grade grade >60 no yes Print “passed” Print “failed” Do Other commands
A neat trick • The conditional operator • a ternary operator (the only one) • for example • message = (grade>60 ? “passed” : “failed”); • Same as • If (grade>60) message=“passed” • else message = “failed”;
Chained if else commands • Rewrite the nested if statements to use chained if else statements.
All of the grades • Let’s print the letter grade? For this we will use nested if else statements if (grade>90) print “A” else if (grade>80) print “B” …. What are some other ways to write this?
Other operators • Compound or shortcut assignment operators • x += 200; // same as x=x+200 • x *= 2; // same as x=x*2 • also for %=, -= and /=. • Increment and decrement operators • Pre increment (++x) • x=5; y = ++x; // so y will be 6 • post increment (x++) • x=5; y = x++; // so y will be 5 • also for decrement x--; or --x;
Nested if statements • If (grade > 70) • { • if (grade > 80) • { • if (grade > 90) • System.out.println(“A”); • else • System.out.println(“B”); • } • else System.out.println(“C”); • }
The switch multiple selection statement • Like a string of if statements switch( variable ) { case ‘l’: case ‘L’: print “A line”; break; case ‘C’: print “A circle”; break; default: print “Invalid choice”; break; }
Switch errors • forgetting a break can be a logic error • default case is optional
Logical operators • consider: if (speed==0) { if (engineon==true) { print “ok to stop engine”; } } • or if ((speed==0) && (engineon==true)) print “ok to stop engine”;
More logical operators • && conditional and • || conditional or • & boolean and • | boolean or • ! not • boolean operators always evaluate both expressions
Truth tables • illustrate logical operations
Short circuit evaluation • if (speed==0 && engineon==true) print X; • if condition A is false then condition B will not be evaluated at all. • if (speed==0 || engineon==true) print X; • if condition A is true then condition B will not be evaluated at all. • This can lead to logic errors if side effect exist in condition B. • Boolean operators always evaluate both expressions.
Expression side effects Here is the side effect • stay away from these • while (++x<10) print x; • If (retiredFlag==true) &&(++age<65) • the increment operator is used as a side effect in the case of this conditional expression.
Homework #3 (CS211/212 only) • Problems 7 and 9 on page 159 • Due in one week.