Java Coding 2 Decisions decisions David Davenport Computer

  • Slides: 11
Download presentation
Java Coding 2 Decisions, decisions…! David Davenport Computer Eng. Dept. , Bilkent University Ankara

Java Coding 2 Decisions, decisions…! David Davenport Computer Eng. Dept. , Bilkent University Ankara - Turkey. email: david@bilkent. edu. tr

IMPORTANT… n Students… This presentation is designed to be used in class as part

IMPORTANT… n Students… This presentation is designed to be used in class as part of a guided discovery sequence. It is not selfexplanatory! Please use it only for revision purposes after having taken the class. Simply flicking through the slides will teach you nothing. You must be actively thinking, doing and questioning to learn! n Instructors… You are free to use this presentation in your classes and to make any modifications to it that you wish. All I ask is an email saying where and when it is/was used. I would also appreciate any suggestions you may have for improving it. thank you, David.

Decision n The Java if statement if (condition) statement. T; n if (condition) statement.

Decision n The Java if statement if (condition) statement. T; n if (condition) statement. T; else statement. F; where n statement is any Java statement n condition is a boolean expression

Conditions n Any expression with Boolean result n boolean variable • is. Over 21

Conditions n Any expression with Boolean result n boolean variable • is. Over 21 n taxable found Method with boolean result • exists() is. Sent( my. Email) n Operand relational. Operator Operand n Boolean expressions combined with logical. Operator (where relational. Operator is: >, <, >=, <=, ==, != ) • age >= 21 speed == 0 year % 4 != 0 (where logical. Operator is: &&, ||, ! ) • height > 2 && weight <= 80 x < 5 || x > 10 • ! exists() a. Char >= ‘ 0’ && a. Char <= ‘ 9’

Examples (1) n Print message when x is positive n n Print warning if

Examples (1) n Print message when x is positive n n Print warning if input exceeds threshold n n if ( x > 0 ) System. out. println( “The value of x is positive”); if ( input. Value >= THRESHOLD ) System. out. println( “Warning – overload!”); Report whether x is negative or not n if ( x < 0 ) System. out. println( “Negative”); else System. out. println( “Positive or zero”); n n Rewrite with alternative condition Check user’s password n if ( !actual. Password. equals( entered. Password) ) System. out. println( “Sorry, incorrect Password”); else // do secure things!

Examples (2) n Compute z as absolute value of x-y n if ( x

Examples (2) n Compute z as absolute value of x-y n if ( x – y < 0 ) z = y – x; else z = x – y; n if ( x > y ) z = x - y; else z = y - x; n z = x – y; if ( z < 0 ) z = -z; Can also use z = Math. abs(x-y);

Examples (3) n Given three values stored in variables first, second, third, store the

Examples (3) n Given three values stored in variables first, second, third, store the minimum in a variable called min n if ( first < second ) min = first; else min = second; if ( third < min ) min = third; n Generalise…?

Examples (4) n Avoid divide-by-zero errors n n if ( y = 0 )

Examples (4) n Avoid divide-by-zero errors n n if ( y = 0 ) System. out. println( “Error: can’t divide by zero”); else z = x / y; System. out. println( “The result is “ + z ); Use braces (curly brackets) to form compound statement { statement; } statement;

Examples (5) n Choosing between three alternatives: n if ( x < 0 )

Examples (5) n Choosing between three alternatives: n if ( x < 0 ) System. out. println( “Negative”); else { if ( x == 0 ) System. out. println( “Zero”); else System. out. println( “Positive”); } n if ( x >= 0 ) { if ( x == 0 ) System. out. println( “Zero”); else System. out. println( “Positive”); } else System. out. println( “Negative”);

Examples (6) n A neater way of writing multi-choice code segments is (nested if):

Examples (6) n A neater way of writing multi-choice code segments is (nested if): n if ( x < 0 ) System. out. println( “Negative”); else if ( x == 0 ) // and x >= 0 System. out. println( “Zero”); else // x >= 0 and x != 0 System. out. println( “Positive”);

Distinguish… if (cond) print “A” else if (cond) print “B” else if (cond) print

Distinguish… if (cond) print “A” else if (cond) print “B” else if (cond) print “C” else print “D” if (cond) print “A” if (cond) print “B” if (cond) print “C” if (cond) print “D”