Introduction to Programming in Java – Decisions - Review | CSIS 110, Study notes of Javascript programming

Material Type: Notes; Professor: Hanks; Class: Intro to Programming in Java; Subject: Computer Science Info Systems; University: Fort Lewis College; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 08/05/2009

koofers-user-lji-1
koofers-user-lji-1 🇺🇸

10 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSIS 110 – Lecture 12
Quiz next Wednesday.
Reading Assignment
Chapter 5, sections 5.5 through 5.8.
Programming Assignment
The next programming assignment is on-line. Although it is not due until next Monday
the 14th, I strongly recommend working on it before the quiz, as it will increase your
understanding of conditional logic.
Chapter 5: Decisions
Review
boolean variables
relational operators
boolean operaters
conditional statements
- if
- if else
- nested if
- if else if
If statement "gotcha's"
Let's look at the following code (Example1.java):
int value = stdin.nextInt();
if ( value > 7 );
System.out.println("more than 7");
What does the above program print when x = 8? When x = 6?
A semicolon by itself is considered to be statement! It is called the empty statement. It
can really cause your program to behave oddly.
Let's look at another example (Example2.java):
int first = stdin.nextInt();
int second = stdin.nextInt();
if ( first > second )
System.out.println("first is bigger");
pf3
pf4
pf5

Partial preview of the text

Download Introduction to Programming in Java – Decisions - Review | CSIS 110 and more Study notes Javascript programming in PDF only on Docsity!

CSIS 110 – Lecture 12

Quiz next Wednesday. Reading Assignment Chapter 5, sections 5.5 through 5.8. Programming Assignment The next programming assignment is on-line. Although it is not due until next Monday the 14th, I strongly recommend working on it before the quiz, as it will increase your understanding of conditional logic. Chapter 5: Decisions Review boolean variables relational operators boolean operaters conditional statements

  • if
  • if else
  • nested if
  • if else if If statement "gotcha's" Let's look at the following code (Example1.java): int value = stdin.nextInt(); if ( value > 7 ); System.out.println("more than 7"); What does the above program print when x = 8? When x = 6? A semicolon by itself is considered to be statement! It is called the empty statement. It can really cause your program to behave oddly. Let's look at another example (Example2.java): int first = stdin.nextInt(); int second = stdin.nextInt(); if ( first > second ) System.out.println("first is bigger");

System.out.println("second is smaller"); What does the above print when the user enters 7 and 3? How about when they enter 4 and 6? Make sure you include the {}! Remember, the compiler doesn't care about indentation. Don't let the indentation fool you. And another example (Example3.java): if ( x > 1 ) if ( y < 2 ) S.O.PL("Here"); else S.O.PL("There"); What does the above print when x=2 and y = 1? What does it print when x = 0 and y = 1? What does it print when x = 2 and y = 3? This is called a 'dangling-else'. It's not clear which 'if' the 'else' goes with. In Java, the else matches the previous unmatched 'if' in the same block. Once again, note that the indentation has nothing to do with the actual program logic - indent carefully! Use {} to make the matching explicit. Switch statement Let's say we wanted to write a little calculator program, and we use different keystrokes to represent different operations: '', '/', '+', '-', '^'. We could write the code to handle these commands using the if else-if construct: int firstValue = stdin.nextInt(); int secondValue = stdin.nextInt(); int result; boolean error = false; char command = stdin.nextChar(); if (command == '+' ) { result = firstValue + secondValue; else if (command == '') result = firstValue * secondValue; else if (command == '-') result = firstValue - secondValue; else if ( command == '/' ) result = firstValue / secondValue;

Let's look at another example: Assume dayOfWeek is a variable where 1 = Sunday, 7 = Saturday. Then we want to go to CSIS110 on MWF, study on Tuesday and Thursday, and enjoy the weekend on Friday and Saturday. How do we do this with a switch statement? Let's look at another example of if else-if (Month1.java): if ( month==4 || month==6 || month==9 || month==11 ) { daysInMonth = 30; else if ( month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12 ) daysInMonth = 31; else if ( month == 2 ) daysInMonth = 28; // Assume not a leap year else daysInMonth = 0; How could we write this using switch (Month2.java)? switch ( month ) { case 4: case 6: case 9: case 11: daysInMonth = 30; break; case 1: case 3: case 5: case 7: case 8: case 10 case 12: daysInMonth = 31; break; case 2: daysInMonth = 2; break; default: daysInMonth = 0; } Note that I have to specify each case separately. I can't say something like 'case 4,6,9,11:'. Note that the keyword for the situation where the value does not match is 'default', not 'else'

break statement is important - if you leave it out, your program won't work! Case choices must be integral values - you can't put Strings, or floats, or boolean expressions here. Equality Testing of Objects If we want to see if two ints have the same value, we can use the == operator: int value1 = stdin.nextInt(); int value2 = stdin.nextInt(); if ( value1 == value2 ) S.O.PL( "value1 is equal to value2"); else S.O.PL( "value1 is not equal to value2" ); Tempting to do the same thing with Strings - Let's look at StringCompare.java String firstString = stdin.nextLine(); String secondString = stdin.nextLine(); if ( firstString == secondString ) S.O.PL( "The Strings are the same"); else S.O.PL( "The Strings are different" ); Let's look at another example (modify StringCompare to add thirdString): String firstString = stdin.nextLine(); String secondString = stdin.nextLine(); String thirdString = firstString; if ( firstString == secondString ) S.O.PL( "The Strings are the same"); else S.O.PL( "The Strings are different" ); if ( firstString == thirdString ) S.O.PL( "The Strings are the same"); else S.O.PL( "The Strings are different" ); So, what is == testing? It is checking to see if the references are the same? Do the Strings refer to the same memory location? How do we check to see if the String contents are the same?