{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

06_More_Conditonals_and_Loops

# 06_More_Conditonals_and_Loops - 6 More Conditionals and...

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

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

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

View Full Document
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" ; }
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" ; }

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

View Full Document
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
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

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### 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
Ask a homework question - tutors are online