06_More_Conditonals_and_Loops

06_More_Conditonals_and_Loops - 6. More Conditionals and...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
COMP 1210 – Fundamentals of Computing I Slide 5 - 1 6. More Conditionals and Loops • Objectives - when we have completed this chapter, you should be familiar with: switch statements the conditional (ternary) operator do statement for loop for-each loop
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COMP 1210 – Fundamentals of Computing I Slide 5 - 2 Switch Statements • Consider the following if statement, where input is a char value: String answer; if (input == 't' ) { answer = "true" ; } else if (input == 'f' ) { answer = "false" ; } else { answer = "invalid" ; }
Background image of page 2
COMP 1210 – Fundamentals of Computing I Slide 5 - 3 Switch Statements • The switch statement is very similar to the if statement (assume input is a char and answer is a String) : if (input == 't' ) { answer = "true" ; } else if (input == 'f' ) { answer = "false" ; } else { answer = "invalid" ; } switch (input) { case 't' : answer = "true" ; break ; case 'f' : answer = "false" ; break ; default : answer = "invalid" ; }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COMP 1210 – Fundamentals of Computing I Slide 5 - 4 Switch Statements • Now that you know the syntax, let’s look a little more closely. Expression in the switch is evaluated. Its value is matched to one of the cases. Suppose input is equal to ‘f’… answer will be set to “false” The break statement breaks out of the switch switch (input) { case 't' : answer = "true" ; break ; case 'f' : answer = "false" ; break ; default : answer = "invalid" ; } TrueOrFalse.java
Background image of page 4
COMP 1210 – Fundamentals of Computing I Slide 5 - 5 Switch Statements • What happens when there is no break statement? Suppose input is now true. It will jump to the appropriate case… And then move to every other case under it until a break or the end of the switch statement. In this case, answer will
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/02/2012 for the course COMP 1210 taught by Professor Cross during the Winter '07 term at Auburn University.

Page1 / 19

06_More_Conditonals_and_Loops - 6. More Conditionals and...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online