CSIS 110 Lecture 13: Decisions and Switch Statements - Prof. Brian F. Hanks, Study notes of Javascript programming

A part of the lecture notes for csis 110, covering topics related to decisions and switch statements. It includes information on 'if statement gotcha's', switch statements, and equality testing of objects. Students are reminded of an upcoming quiz and programming assignment.

Typology: Study notes

Pre 2010

Uploaded on 08/05/2009

koofers-user-hn4-1
koofers-user-hn4-1 🇺🇸

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSIS 110 – Lecture 13
Reminder: Quiz 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
If statement "gotcha's"
1) Extra semicolon
2) Missing braces
3) Dangling if
Switch statement
- Use when you have a lot of 'if - else if' statements in sequence. Makes it more
clear that you are selecting one choice from a set of alternatives.
-
switch ( <integral expression> ) {
case CaseExpr1:
statements 1
case CaseExpr2:
statements 2
...
default:
default statements
}
Let's write a switch statement that examines a variable dayOfWeek and prints one of the
following messages:
"Go to CSIS 110" on Monday, Wednesday, and Friday
"Study" on Tuesday and Thursday
"Have fun!" on Saturday and Sunday
What happens if we leave out the break statement?
pf3

Partial preview of the text

Download CSIS 110 Lecture 13: Decisions and Switch Statements - Prof. Brian F. Hanks and more Study notes Javascript programming in PDF only on Docsity!

CSIS 110 – Lecture 13

Reminder: Quiz 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 If statement "gotcha's"

  1. Extra semicolon
  2. Missing braces
  3. Dangling if Switch statement
  • Use when you have a lot of 'if - else if' statements in sequence. Makes it more clear that you are selecting one choice from a set of alternatives.

switch ( ) { case CaseExpr1: statements 1 case CaseExpr2: statements 2 ... default: default statements } Let's write a switch statement that examines a variable dayOfWeek and prints one of the following messages: "Go to CSIS 110" on Monday, Wednesday, and Friday "Study" on Tuesday and Thursday "Have fun!" on Saturday and Sunday What happens if we leave out the break statement?

Case choices must be integral values - you can't put Strings, or floats, or boolean expressions here. *** End of Review *** 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?