1 Introduction to Computers and Programming in JAVA

  • Slides: 28
Download presentation
1 Introduction to Computers and Programming in JAVA: V 22. 0002 Control structures: if-else

1 Introduction to Computers and Programming in JAVA: V 22. 0002 Control structures: if-else statements and switch statements 2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.

2 Relational Operators (revisited) Operator Meaning > Greater than < Less than >= Greater

2 Relational Operators (revisited) Operator Meaning > Greater than < Less than >= Greater than or equal to <= Less than or equal to == Equal to != Not Equal to 2003 Prentice Hall, Inc. All rights reserved (Modified).

3 Strings: example 1 public class string_example //String concatenation { public static void main(String[]

3 Strings: example 1 public class string_example //String concatenation { public static void main(String[] args) { //String is a class String s 1 = " The Cat "; String s 2 = " in the Hat"; String s 3 = s 1 + s 2; //concatenation example System. out. println(s 3); } } 2003 Prentice Hall, Inc. All rights reserved (Modified).

4 Strings -- example 2 import javax. swing. JOption. Pane; //not in java. lang

4 Strings -- example 2 import javax. swing. JOption. Pane; //not in java. lang public class string_example 2 //concatenate two strings read from input { public static void main(String[] args) { String s 1 = JOption. Pane. show. Input. Dialog("type 1 st input"); String s 2 = JOption. Pane. show. Input. Dialog("type 2 nd input"); String s 3 = s 2 + s 1; System. out. println(s 3); System. exit(0); //required for JOption. Pane } } 2003 Prentice Hall, Inc. All rights reserved (Modified).

// less than five!! import javax. swing. JOption. Pane; public class less_than_5{ public static

// less than five!! import javax. swing. JOption. Pane; public class less_than_5{ public static void main(String args[]) { // Prompt the user to enter a number: String num. String = JOption. Pane. show. Input. Dialog(null, "Enter a number from 1 to 10: ", "Input Window Demo", JOption. Pane. QUESTION_MESSAGE); // Convert the string into an int value int num = Integer. parse. Int(num. String); // Display the result in a message dialog box if ( num < 5 ) System. out. println(" The number " + num + " is less than five. "); else if (num == 5) System. out. println(" The number " + num + " is equal to five. "); else System. out. println(" The number " + num + " is greater than five. "); System. exit(0); } 2000 Prentice Hall, Inc. All rights reserved. (modified) 5

Another example: writing a program in class about people’s ages: 6 • Program should

Another example: writing a program in class about people’s ages: 6 • Program should ask user to input his or her age • Program should determine and print the following based on the age: – A person is a teenager if their age is 13 through 19 – You are too old to be a teenager – You are too young to be a teenager 2000 Prentice Hall, Inc. All rights reserved. (modified)

// ages import javax. swing. JOption. Pane; Nested if-else public class age { public

// ages import javax. swing. JOption. Pane; Nested if-else public class age { public static void main(String args[]) { // Prompt the user to enter a number: String num. String = JOption. Pane. show. Input. Dialog(null, "Enter your age: ", "Input Window Demo", JOption. Pane. QUESTION_MESSAGE); int age = Integer. parse. Int(num. String); System. out. println(" Your age is " + age + ". "); // Convert the string into an int value /* A person is a teenager if their age is 13 thu 19 */ if (age > 12) if (age <20) System. out. println("You are a teenager!n"); else System. out. println("You are too old to be a teenager!n"); else System. out. println("You are too young to be a teenager!n"); System. exit(0); } } 2003 Prentice Hall, Inc. All rights reserved (Modified). 7

// grades: example #2 import javax. swing. JOption. Pane; Nested if-else public class grades_example

// grades: example #2 import javax. swing. JOption. Pane; Nested if-else public class grades_example 2 { public static void main(String args[]) { // Prompt the user to enter a number: String num. String = JOption. Pane. show. Input. Dialog(null, "Enter your grade (0 -100): ", "Input Window Demo", JOption. Pane. QUESTION_MESSAGE); // Convert the string into an int value int grade = Integer. parse. Int(num. String); System. out. println("Your grade is " + grade + ". "); if (grade >= 90) System. out. println("You got an "A". n"); else if (grade >= 80) System. out. println("You got a "B". n"); else if (grade >= 70) System. out. println("You got a "C". n"); else if (grade >= 60) System. out. println("You passed but you need a tutor. n"); else System. out. println("You failed. n"); System. exit(0); } } 2003 Prentice Hall, Inc. All rights reserved (Modified). 8

9 switch Multiple-Selection Structure • Used when testing a variable or expression for EQUALITY:

9 switch Multiple-Selection Structure • Used when testing a variable or expression for EQUALITY: • ( >, <, >=, <=) – tests separately for each of the constant integral values it may assume. • Preferred over if else in situations: – where you are testing the same expressions for equality with many different values. • Allows you to perform different actions for each test. 2003 Prentice Hall, Inc. All rights reserved (Modified).

10 switch Multiple-Selection Structure switch (expression) { case value 1: action(s); break; keyword switch

10 switch Multiple-Selection Structure switch (expression) { case value 1: action(s); break; keyword switch expression can be a variable or a more complicated expression case value 2: action(s); break; … default: actions(s); break; could use more than one case; if the same actions are required actions within a single case do not need brackets the default case will be executed in the event that no other case is } 2003 Prentice Hall, Inc. All rights reserved (Modified).

switch Multiple-Selection Flow. Chart True Case a action(s) break Case b action(s) break Default

switch Multiple-Selection Flow. Chart True Case a action(s) break Case b action(s) break Default action(s) Case a false True false 2003 Prentice Hall, Inc. All rights reserved (Modified). 11

12 beware of “fall through” • If you forget to use the break keyword

12 beware of “fall through” • If you forget to use the break keyword between cases, unexpected things may happen. • Once a case tests true, all the statements following that case, will be executed until the next break. 2003 Prentice Hall, Inc. All rights reserved (Modified).

Nested if-else //dice_using_if import javax. swing. *; public class dice_using_if { public static void

Nested if-else //dice_using_if import javax. swing. *; public class dice_using_if { public static void main( String args[] ) { // Prompt the user to enter a number: String num. String = JOption. Pane. show. Input. Dialog(null, "Roll the die!! (1 -6): ", "Input Window Demo", JOption. Pane. QUESTION_MESSAGE); // Convert the string into an int value int roll = Integer. parse. Int(num. String); System. out. println(" You rolled a " + roll + ". "); if (roll == 1 ) System. out. println("You rolled a one! Try again! n"); else if (roll == 2 ) System. out. println("You rolled a two! Too bad! n"); else if (roll == 3 ) System. out. println("You rolled a three! Better luck next time! n"); else if (roll == 4 ) System. out. println("You rolled a four! Sorry! n"); else if (roll == 5 ) System. out. println("You rolled a five! Way to go!!n"); else if (roll == 6 ) System. out. println("You rolled a six! I guess you win. . . n"); else System. out. println("This is some wierd die! n"); System. exit( 0 ); // terminate application }} 2003 Prentice Hall, Inc. All rights reserved (Modified). 13

Switch statements // dice_using_switch import javax. swing. *; public class dice_using_switch { public static

Switch statements // dice_using_switch import javax. swing. *; public class dice_using_switch { public static void main( String args[] ) { // Prompt the user to enter a number: String num. String = JOption. Pane. show. Input. Dialog(null, "Roll the die!! (1 -6): ", "Input Window Demo", JOption. Pane. QUESTION_MESSAGE); // Convert the string into an int value int roll = Integer. parse. Int(num. String); System. out. println(" You rolled a " + roll + ". "); switch ( roll ) { case 1: System. out. println("You rolled a one! Try again! n"); break; case 2: System. out. println("You rolled a two! Too bad! n"); break; case 3: System. out. println("You rolled a three! Better luck next time! n"); break; case 4: System. out. println("You rolled a four! Sorry! n"); break; case 5: System. out. println("You rolled a five! Way to go!!n"); break; case 6: System. out. println("You rolled a six! I guess you win. . . n"); break; default: System. out. println("This is some wierd die! n"); } // end switch System. exit( 0 ); // terminate application } // end main } 2003 Prentice Hall, Inc. All rights reserved (Modified). 14

Switch statements // switch example #2 import javax. swing. *; public class switch_example 2

Switch statements // switch example #2 import javax. swing. *; public class switch_example 2 { public static void main( String args[] ) { // Prompt the user to enter a number: String num. String = JOption. Pane. show. Input. Dialog(null, "Pick a number from 1 -6: ", "Input Window Demo", JOption. Pane. QUESTION_MESSAGE); int pick = Integer. parse. Int(num. String); // Convert the string into an int value System. out. println(" You picked a " + pick + ". "); switch (pick) { case 1: System. out. println("You entered 1!n"); break; case 2: case 3: System. out. println("You entered a 2 or a 3!n"); break; case 4: case 5: case 6: System. out. println("You entered a 4, 5, or a 6!n"); break; default: System. out. println("You did not enter an 1, 2, 3, 4, 5 or 6!n"); break; } /* end switch */ System. exit( 0 ); } } 2003 Prentice Hall, Inc. All rights reserved (Modified). 15

Nested if-else // grades: example #1 import javax. swing. JOption. Pane; public class grades_example

Nested if-else // grades: example #1 import javax. swing. JOption. Pane; public class grades_example 1 { public static void main(String args[]) { // Prompt the user to enter a number: String num. String = JOption. Pane. show. Input. Dialog(null, "Enter your grade (0 -100): ", "Input Window Demo", JOption. Pane. QUESTION_MESSAGE); // Convert the string into an int value int grade = Integer. parse. Int(num. String); System. out. println(" Your grade is " + grade + ". "); if (grade > 60) if (grade > 70) System. out. println("You passed. n"); else System. out. println("You passed but you need a tutor. n"); else System. out. println("You failed. n"); System. exit(0); } } 2003 Prentice Hall, Inc. All rights reserved (Modified). 16

17 The boolean Type and Operators boolean lights. On = true; boolean lights. On

17 The boolean Type and Operators boolean lights. On = true; boolean lights. On = false; boolean b = (1 > 2); • && (and) • || (or) • ! (not) (1 < x) && (x < 100) (1 < x) ||(x < 100) !(1<x) 2003 Prentice Hall, Inc. All rights reserved (Modified).

18 Comparison Operators Operator Name < less than <= less than or equal to

18 Comparison Operators Operator Name < less than <= less than or equal to > greater than >= greater than or equal to == equal to != not equal to 2003 Prentice Hall, Inc. All rights reserved (Modified).

19 Boolean Operators Operator Name ! not && and || or 2003 Prentice Hall,

19 Boolean Operators Operator Name ! not && and || or 2003 Prentice Hall, Inc. All rights reserved (Modified).

20 Truth Table for Operator ! Operand !Operand true false true 2003 Prentice Hall,

20 Truth Table for Operator ! Operand !Operand true false true 2003 Prentice Hall, Inc. All rights reserved (Modified).

21 Truth Table for Operator && Operand 1 Operand 2 Operand 1 && Operand

21 Truth Table for Operator && Operand 1 Operand 2 Operand 1 && Operand 2 false false true true 2003 Prentice Hall, Inc. All rights reserved (Modified).

22 Truth Table for Operator || Operand 1 Operand 2 Operand 1 || Operand

22 Truth Table for Operator || Operand 1 Operand 2 Operand 1 || Operand 2 false true false true 2003 Prentice Hall, Inc. All rights reserved (Modified).

23 Increment and Decrement Operators 2003 Prentice Hall, Inc. All rights reserved (Modified).

23 Increment and Decrement Operators 2003 Prentice Hall, Inc. All rights reserved (Modified).

Increment and Decrement Operators • Post. Decrement Operator (x--): – use the current value

Increment and Decrement Operators • Post. Decrement Operator (x--): – use the current value of x in the expression. Then, decrease by 1. • Pre. Decrement Operator (--x): – Decrease x by 1. Then, use the new value of x in the expression. 2003 Prentice Hall, Inc. All rights reserved (Modified). 24

25 Increment and Decrement Operators, cont. 2003 Prentice Hall, Inc. All rights reserved (Modified).

25 Increment and Decrement Operators, cont. 2003 Prentice Hall, Inc. All rights reserved (Modified).

26 Increment and Decrement Operators, cont. FUsing increment and decrement operators makes expressions short

26 Increment and Decrement Operators, cont. FUsing increment and decrement operators makes expressions short Fbut it also makes them complex and difficult to read. FAvoid using these operators in expressions that modify multiple variables, or the same variable for multiple times such as this: int k = ++i + I; 2003 Prentice Hall, Inc. All rights reserved (Modified).

27 What's the output of this program? public class shortcut_operators { public static void

27 What's the output of this program? public class shortcut_operators { public static void main(String[] args) { // declare variables int x = 10; int y = 5; int z = 3; System. out. println("x = "+x+", y = "+y+", z = "+z+ "n"); x++; ; y += x; z *= x; System. out. println("Now x = "+x+", y = "+y+", z = "+z+ "n"); x--; y *= x; z %= x; System. out. println("And now x = "+x+", y = "+y+", z = "+z+ "n"); System. exit(0); } } 2003 Prentice Hall, Inc. All rights reserved (Modified).

28 What's the output of this program? public class shortcut_operators { public static void

28 What's the output of this program? public class shortcut_operators { public static void main(String[] args) { // declare variables int x = 10; int y = 5; int z = 3; x = 10, y = 5, z = 3 Now x = 11, y = 16, z = 33 System. out. println("x = "+x+", y = "+y+", z = "+z+ "n"); And now x = 10, y = 160, z = 3 x++; ; y += x; z *= x; Press any key to continue. . . System. out. println("Now x = "+x+", y = "+y+", z = "+z+ "n"); x--; y *= x; z %= x; System. out. println("And now x = "+x+", y = "+y+", z = "+z+ "n"); System. exit(0); } } 2003 Prentice Hall, Inc. All rights reserved (Modified).