Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 3 Decisions Three control structures Algorithms Pseudocode Flowcharts If…then …else Nested if statements Code blocks { } multi statement blocks.

Similar presentations


Presentation on theme: "Chapter 3 Decisions Three control structures Algorithms Pseudocode Flowcharts If…then …else Nested if statements Code blocks { } multi statement blocks."— Presentation transcript:

1 Chapter 3 Decisions Three control structures Algorithms Pseudocode Flowcharts If…then …else Nested if statements Code blocks { } multi statement blocks

2 Chapter 3 more Control Structures switch statements multiple-selection Relational operators logical operators

3 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

4 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;.

5 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

6 Structured design Algorithms 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

7 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.

8 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

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

10 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

11 Logic operators < <= > >= == !=

12 The if…else statement if (condition) statement; or if (condition) statement else statement; or if (condition) { statements; …. } else { statements; }

13 Lets write the program to compare two numbers We will use if statements…

14 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

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

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

17 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”;

18 Chained if else commands Rewrite the nested if statements to use chained if else statements.

19 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?

20 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;

21 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”); }

22 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; }

23 Switch errors forgetting a break can be a logic error default case is optional

24 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”;

25 More logical operators && conditional and || conditional or & boolean and | boolean or ! not boolean operators always evaluate both expressions

26 Truth tables illustrate logical operations ABA&&BA||B!B TTTTF TFFTT FTFT- FFFF-

27 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.

28 Expression side effects 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. Here is the side effect

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


Download ppt "Chapter 3 Decisions Three control structures Algorithms Pseudocode Flowcharts If…then …else Nested if statements Code blocks { } multi statement blocks."

Similar presentations


Ads by Google