Chapter 3 Decisions Three control structures Algorithms Pseudocode





























- Slides: 29

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 n 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: jdk 1. 2. 1lib; .

Three control structures Sequence – flow n n n The programs flow Top down Method calls Selection – logic – conditional n n Choose between alternate code blocks If…then Iterations - Repetition– loops n n n Perform a code block multiple times While For

Structured design Algorithms An algorithm is a solution to a problem. n n To create a solution you must first understand the problem. Then plan a solution. Algorithms describe: n n 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: n n 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? n Which is easier to read and unserstand

Flowcharts Used to illustrate program flow initial state Start get radius transition lines action statements Compute Diam Compute area Display area final state End notes go here

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. s 1. compareto(s 2) n n n Returns negative if s 1 is less than s 2 Returns 0 if s 1=s 2 Returns positive if s 1 is greater than s 2 S 1. equals(s 2) n Used to compare objects in general

Grades if (grade>60) system. out. print(“student passed”); get grade yes grade >60 no Do Other commands Print “passed”

Grades with else if (grade>60) system. out. print(“student passed”) else system. out. print(“student failed”); get grade no Print “failed” yes grade >60 Do Other commands Print “passed”

A neat trick The conditional operator a ternary operator (the only one) for example n n message = (grade>60 ? “passed” : “failed”); Same as n n 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 n n n x += 200; // same as x=x+200 x *= 2; // same as x=x*2 also for %=, -= and /=. Increment and decrement operators n n n 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 A B A&&B A||B !B T T F T F F T T F T - F F -

Short circuit evaluation if (speed==0 && engineon==true) print X; n if condition A is false then condition B will not be evaluated at all. if (speed==0 || engineon==true) print X; n 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 stay away from these n n Here is the side effect while (++x<10) print x; If (retired. Flag==true) &&(++age<65) the increment operator is used as a side effect in the case of this conditional expression.

Homework #3 (CS 211/212 only) Problems 7 and 9 on page 159 Due in one week.