chapter4 - Chapter4ControlStatements 1 1 Conditions...

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

View Full Document Right Arrow Icon
1 Chapter 4 - Control Statements Conditions if  Statement  Logical Operator ||  Logical Operator !  Logical Operator switch  Statement while  Loop do  Loop for  Loop Loop Comparison Nested Loops Boolean Variables Input Validation Boolean Logic Expression Evaluation Practice In this chapter, GarageDoor.java is demonstrated. 1
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Conditions Throughout this chapter, you’ll see if statements and  loop statements where  conditions  appear within a pair  of parentheses, like this: if ( <condition> ) {   . .. } while ( <condition> ) {   . .. } Typically, each condition involves some type of  comparison and the comparisons use comparison  operators…. (1. next slide) 1
Background image of page 2
3 Conditions Here are Java's comparison operators: ==, !=, <, >, <=, >= Each comparison operator evaluates to either true or  false. ==   (1. Java uses the single equals sign for assignment; for comparison, use ==.) Tests two operands for equality. 3 == 3 evaluates to true 3 == 4 evaluates to false Note that == uses two equal signs, not one! != Tests two operands for inequality. The != operator is pronounced “not equal.” The <, >, <=, and >= operators work as expected. 1
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 if  Statement Use an  if  statement if you need to ask a question in  order to determine what to do next. There are three forms for an  if  statement: if  by itself Use for problems where you want to do something or nothing. if ,   else Use for problems where you want to do one thing or another  thing. if ,   else if Use for problems where you want to do one thing out of three or  more choices. 1. We’ve already covered the if statement using pseudocode, so  this should be review 1
Background image of page 4
5 if  Statement pseudocode syntax if  by itself: if <condition> <statement(s)> if ,   else : if <condition> <statement(s)> else <statement(s)> Java syntax The Java syntax has parentheses around the  condition and uses braces.  if  by itself: if ( <condition> ) { <statement(s)> } if ,   else : if ( <condition> ) { <statement(s)> } else { <statement(s)> } 1 2 3 4
Background image of page 5

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

View Full DocumentRight Arrow Icon
6 if  Statement pseudocode syntax if else if : if <condition> <statement(s)> else if <condition> <statement(s)> . . . else <statement(s)> Java syntax if else if else : if ( <condition> ) { <statement(s)> } else if ( <condition> ) { <statement(s)> } . . . else { <statement(s)> } more else if's here (optional) optional more else if's here (optional) optional 1 2 3
Background image of page 6
7 if  Statement Write a complete program that prompts the user to  enter a sentence and then prints an error message if  the last character is not a period. sample session
Background image of page 7

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

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

Page1 / 47

chapter4 - Chapter4ControlStatements 1 1 Conditions...

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

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