The ifelse statement Introducing the ifelse statement Programming

  • Slides: 51
Download presentation
The if-else statement

The if-else statement

Introducing the if-else statement • Programming problem: • Re-write the a, b, c-formula program

Introducing the if-else statement • Programming problem: • Re-write the a, b, c-formula program to solve for complex number solutions when b 2 - 4 ac < 0

Introducing the if-else statement (cont. ) • Algorithm: input a, b, c; Det =

Introducing the if-else statement (cont. ) • Algorithm: input a, b, c; Det = b*b - 4*a*c; // Compute the determinant if ( Det >= 0 ) { print -b/(2 a) + sqrt(Det)/(2 a); // Real number solutions print -b/(2 a) - sqrt(Det)/(2 a); } if ( Det < 0 ) { print -b/(2 a) "+" (sqrt(-Det)/(2 a) + "i"); // Complex number solutions print -b/(2 a) "-" (sqrt(-Det)/(2 a) + "i"); }

Introducing the if-else statement (cont. ) • Java program: import java. util. Scanner; public

Introducing the if-else statement (cont. ) • Java program: import java. util. Scanner; public class Abc 3 { public static void main(String[] args) { double a, b, c, Det, re, im; Scanner in = new Scanner(System. in); // Construct Scanner object a = in. next. Double(); // Read in next number into a b = in. next. Double(); // Read in next number into b c = in. next. Double(); // Read in next number into c

Introducing the if-else statement (cont. ) Det = b*b - 4*a*c; if ( Det

Introducing the if-else statement (cont. ) Det = b*b - 4*a*c; if ( Det >= 0 ) { System. out. println( (-b + Math. sqrt( Det ) ) / (2*a) ); System. out. println( (-b - Math. sqrt( Det ) ) / (2*a) ); } if ( Det < 0 ) { re = -b/(2*a); // Compute real part im = Math. sqrt( -Det )/(2*a); // Compute imaginary part System. out. println( re + "+" + im + "i" ); System. out. println( re + "-" + im + "i" ); } } }

Introducing the if-else statement (cont. ) • Example Program: (Demo above code) – Prog

Introducing the if-else statement (cont. ) • Example Program: (Demo above code) – Prog file: http: //mathcs. emory. edu/~cheung/Courses/170/Syllabus/06/Progs/ Abc 3. java • How to run the program: • Right click on link and save in a scratch directory • To compile: javac Abc 3. java • To run: java Abc 3

Introducing the if-else statement (cont. ) • Example: Enter a: 1 Enter b: 2

Introducing the if-else statement (cont. ) • Example: Enter a: 1 Enter b: 2 Enter c: 5 -1. 0+2. 0 i -1. 0 -2. 0 i

Introducing the if-else statement (cont. ) • Shortcoming: • We have to negate the

Introducing the if-else statement (cont. ) • Shortcoming: • We have to negate the if-condition ourselves. . . • This can introduce unnecessary errors

Introducing the if-else statement (cont. ) • Solution: • Extend the if-statement with an

Introducing the if-else statement (cont. ) • Solution: • Extend the if-statement with an alternative statement • The alternative statement is only executed when the if-condition is false

The if-else statement in Java • The if-else statement: • The if-else statement is

The if-else statement in Java • The if-else statement: • The if-else statement is the seconditional statement in Java • The if-else statement selects one of two statements to be executed based on a given condition

Syntax and meaning of the if-else-statement • Syntax of the if-else-statement: if ( CONDITION

Syntax and meaning of the if-else-statement • Syntax of the if-else-statement: if ( CONDITION ) ONEstatement else ONE-statement

Syntax and meaning of the if-else-statement (cont. ) • Explanation: • The keyword if

Syntax and meaning of the if-else-statement (cont. ) • Explanation: • The keyword if announces (to the Java compiler) that we started an if-else-statement • A conditional clause ( CONDITION ) follows the keyword if • This is the condition of the if-else-statement

Syntax and meaning of the if-else-statement (cont. ) • Following the condition clause, you

Syntax and meaning of the if-else-statement (cont. ) • Following the condition clause, you can write (only) one statement • This statement will only be executed if the condition is true • Following then-part, you must specify the keyword else followed by (only) one statement • This is the condition of the if-else-statement • This statement will only be executed if the condition is false

Syntax and meaning of the if-else-statement (cont. ) • Note: The way that the

Syntax and meaning of the if-else-statement (cont. ) • Note: The way that the Java compiler decide whether a conditional statement is: • An if-statement • An if-else-statement is by the presence/absence of the keyword else.

Computer Jargon: else-part • The statement following the keyword else in an if-elsestatement is

Computer Jargon: else-part • The statement following the keyword else in an if-elsestatement is called • The else-part of the if-else-statement (Or else-part for short)

Computer Jargon: else-part (cont. ) • Schematically:

Computer Jargon: else-part (cont. ) • Schematically:

The a, b, c-formula using an if-else-statement • Programming problem: • Re-write the a,

The a, b, c-formula using an if-else-statement • Programming problem: • Re-write the a, b, c-formula program to solve for complex number solutions when b 2 - 4 ac < 0 • Algorithm:

The a, b, c-formula using an if-else-statement (cont. ) • Java program: import java.

The a, b, c-formula using an if-else-statement (cont. ) • Java program: import java. util. Scanner; public class Abc 4 { public static void main(String[] args) { double a, b, c, Det, re, im; Scanner in = new Scanner(System. in); // Construct Scanner object a = in. next. Double(); // Read in next number into a b = in. next. Double(); // Read in next number into b c = in. next. Double(); // Read in next number into c Det = b*b - 4*a*c;

The a, b, c-formula using an if-else-statement (cont. ) if ( Det >= 0

The a, b, c-formula using an if-else-statement (cont. ) if ( Det >= 0 ) { System. out. println( (-b + Math. sqrt( Det ) ) / (2*a) ); System. out. println( (-b - Math. sqrt( Det ) ) / (2*a) ); } else { re = -b/(2*a); // Compute real part im = Math. sqrt( -Det )/(2*a); // Compute imaginary part System. out. println( re + "+" + im + "i" ); System. out. println( re + "-" + im + "i" ); } } }

The a, b, c-formula using an if-else-statement (cont. ) • Example Program: (Demo above

The a, b, c-formula using an if-else-statement (cont. ) • Example Program: (Demo above code) – Prog file: http: //mathcs. emory. edu/~cheung/Courses/170/Syllabus/06/Progs/ Abc 4. java • How to run the program: • Right click on link and save in a scratch directory • To compile: javac Abc 4. java • To run: java Abc 4

Programming example: find maximum of 2 numbers • Programming problem: • Read in 2

Programming example: find maximum of 2 numbers • Programming problem: • Read in 2 number a and b • Assign to the variable max the largest value of a and b

Programming example: find maximum of 2 numbers (cont. ) • Algorithm:

Programming example: find maximum of 2 numbers (cont. ) • Algorithm:

Programming example: find maximum of 2 numbers (cont. ) • Java program: import java.

Programming example: find maximum of 2 numbers (cont. ) • Java program: import java. util. Scanner; public class Max 01 { public static void main(String[] args) { double a, b, max; Scanner in = new Scanner(System. in); // Construct Scanner object a = in. next. Double(); // Read in next number into a b = in. next. Double(); // Read in next number into b if ( a >= b ) max = a; else max = b; System. out. println( "max value = " + max ); } }

Programming example: find maximum of 2 numbers (cont. ) • Example Program: (Demo above

Programming example: find maximum of 2 numbers (cont. ) • Example Program: (Demo above code) – Prog file: http: //mathcs. emory. edu/~cheung/Courses/170/Syllabus/06/Progs/ Max 01. java • How to run the program: • Right click on link and save in a scratch directory • To compile: javac Max 01. java • To run: java Max 01

Program example: find maximum of 3 numbers • Programming problem: • Read in 3

Program example: find maximum of 3 numbers • Programming problem: • Read in 3 number a, b and c • Assign to the variable max the largest value of a, b and c

Program example: find maximum of 3 numbers (cont. ) • Algorithm:

Program example: find maximum of 3 numbers (cont. ) • Algorithm:

Program example: find maximum of 3 numbers (cont. ) • Java program: import java.

Program example: find maximum of 3 numbers (cont. ) • Java program: import java. util. Scanner; public class Max 01 { public static void main(String[] args) { double a, b, max; Scanner in = new Scanner(System. in); // Construct Scanner object a = in. next. Double(); // Read in next number into a b = in. next. Double(); // Read in next number into b c = in. next. Double(); // Read in next number into c

Program example: find maximum of 3 numbers (cont. ) if ( a >= b

Program example: find maximum of 3 numbers (cont. ) if ( a >= b ) // Find max(a, b) max = a; else max = b; if ( c > max ) // Check c > max ? max = c; System. out. println( "max value = " + max ); } }

Program example: find maximum of 3 numbers (cont. ) • Example Program: (Demo above

Program example: find maximum of 3 numbers (cont. ) • Example Program: (Demo above code) – Prog file: http: //mathcs. emory. edu/~cheung/Courses/170/Syllabus/06/Progs/ Max 02. java • How to run the program: • Right click on link and save in a scratch directory • To compile: javac Max 02. java • To run: java Max 02

Programming example: leap year • Leap year description (Wikipedia): • In the Gregorian calendar,

Programming example: leap year • Leap year description (Wikipedia): • In the Gregorian calendar, the current standard calendar in most of the world, most years that are evenly divisible by 4 are leap years. • Years that are evenly divisible by 100 are not leap years, unless they are also evenly divisible by 400, in which case they are leap years

Programming example: leap year (cont. ) • Algorithm:

Programming example: leap year (cont. ) • Algorithm:

Programming example: leap year (cont. ) • Program in Java: import java. util. Scanner;

Programming example: leap year (cont. ) • Program in Java: import java. util. Scanner; public class Leap. Year 01 { public static void main(String[] args) { int year; boolean leap;

Programming example: leap year (cont. ) Scanner in = new Scanner(System. in); // Construct

Programming example: leap year (cont. ) Scanner in = new Scanner(System. in); // Construct Scanner object year = in. next. Int(); // Read in year if ( year % 4 == 0 ) leap = true; else leap = false; if ( year % 100 == 0 ) leap = false; if ( year % 400 == 0 ) leap = true; System. out. println("Year is leap year ? " + leap); } }

Programming example: leap year (cont. ) • Example Program: (Demo above code) – Prog

Programming example: leap year (cont. ) • Example Program: (Demo above code) – Prog file: http: //mathcs. emory. edu/~cheung/Courses/170/Syllabus/06/Progs/ Leap. Year 01. java • How to run the program: • Right click on link and save in a scratch directory • To compile: javac Leap. Year 01. java • To run: java Leap. Year 01

Common errors in if-else-statements • Common error 1: bogus semicolon after the if-condition Example:

Common errors in if-else-statements • Common error 1: bogus semicolon after the if-condition Example: if ( a >= b ) ; // Bogus ; max = a; else max = b;

Common errors in if-else-statements (cont. ) • Compiler message: Error 01. java: 18: 'else'

Common errors in if-else-statements (cont. ) • Compiler message: Error 01. java: 18: 'else' without 'if' else ^

Common errors in if-else-statements (cont. ) • because the compiler "reads" the program as

Common errors in if-else-statements (cont. ) • because the compiler "reads" the program as follows: if ( a >= b ) // A correct if-statement ; // Note: the if-statement ended here ! max = a; // A correct assignment statement else // else ? Where is the if ? ? ? max = b;

Common errors in if-else-statements (cont. ) • Common error 2: forgetting to use statement

Common errors in if-else-statements (cont. ) • Common error 2: forgetting to use statement block in then-part Example: if ( Det >= 0 ) System. out. println( (-b + Math. sqrt( Det ) ) / (2*a) ); System. out. println( (-b - Math. sqrt( Det ) ) / (2*a) ); else re = -b/(2*a); // Compute real part im = Math. sqrt( -Det )/(2*a); // Compute imaginary part System. out. println( re + "+" + im + "i" ); System. out. println( re + "-" + im + "i" );

Common errors in if-else-statements (cont. ) • The Java compiler will report the error

Common errors in if-else-statements (cont. ) • The Java compiler will report the error 'else' without 'if' because syntactically, the program is read as follows: if ( Det >= 0 ) System. out. println( (-b + Math. sqrt( Det ) ) / (2*a) ); // If-statement System. out. println( (-b - Math. sqrt( Det ) ) / (2*a) ); // Print else // Else without if re = -b/(2*a); // Compute real part im = Math. sqrt( -Det )/(2*a); // Compute imaginary part System. out. println( re + "+" + im + "i" ); System. out. println( re + "-" + im + "i" );

Common errors in if-else-statements (cont. ) • Common error 3: missing semicolon after then-part

Common errors in if-else-statements (cont. ) • Common error 3: missing semicolon after then-part Example: if ( a >= b ) max = a // Missing semicolon !!! else max = b;

Common errors in if-else-statements (cont. ) • Compiler message: Error 03. java: 17: ';

Common errors in if-else-statements (cont. ) • Compiler message: Error 03. java: 17: '; ' expected max = a ^

Common errors in if-else-statements (cont. ) • Reason: • The then-part is ONE statement

Common errors in if-else-statements (cont. ) • Reason: • The then-part is ONE statement • A statement must be ended with a semicolon

Common errors in if-else-statements (cont. ) • Common error 4: bogus semicolon after the

Common errors in if-else-statements (cont. ) • Common error 4: bogus semicolon after the block in then-part • Example: if ( a >= b ) { max = a; }; // bogus semicolon !!! else { max = b; }

Common errors in if-else-statements (cont. ) • Compiler message: Error 04. java: 20: 'else'

Common errors in if-else-statements (cont. ) • Compiler message: Error 04. java: 20: 'else' without 'if' else ^

Common errors in if-else-statements (cont. ) • Reason: • The then-part is ONE statement

Common errors in if-else-statements (cont. ) • Reason: • The then-part is ONE statement • A statement must be ended with a semicolon

Common errors in if-else-statements (cont. ) • Common error 4: bogus semicolon after the

Common errors in if-else-statements (cont. ) • Common error 4: bogus semicolon after the block in then-part Example: if ( a >= b ) { max = a; }; // bogus semicolon !!! else { max = b; }

Common errors in if-else-statements (cont. ) • Compiler message: Error 04. java: 20: 'else'

Common errors in if-else-statements (cont. ) • Compiler message: Error 04. java: 20: 'else' without 'if' else ^

Common errors in if-else-statements (cont. ) • Because syntactically, the program is read as

Common errors in if-else-statements (cont. ) • Because syntactically, the program is read as follows: if ( a >= b ) // An if-statement { max = a; } ; // An empty statement !!! else // Else without if. . . { max = b; }

Programming advice • As you can see, forgetting a "; " and adding an

Programming advice • As you can see, forgetting a "; " and adding an extra "; " can cause serious syntax errors The best thing is to stick to one useful form: use block !!!

Programming advice (cont. ) • I always write if-statements and if-else-statements using blocks first:

Programming advice (cont. ) • I always write if-statements and if-else-statements using blocks first: if (. . . ) { (leave empty first) } ----------------------- if (. . . ) { (leave empty first) } else { (leave empty first) }

Programming advice (cont. ) I fill in the statements in then-part and else-part later.

Programming advice (cont. ) I fill in the statements in then-part and else-part later. So even when the if-part and/or else-part consist of 1 statement, I write them as blocks.