tailieunhanh - Lecture Introduction to programming with Java - Chapter 4: Control statements

The following will be discussed in this chapter: 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. | 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 1 Conditions Throughout this chapter, you’ll see if statements and loop statements where conditions appear within a pair of parentheses, like this: if () { . } while () { . } Typically, each condition involves some type of comparison and the comparisons use comparison operators . 2 Conditions Here are Java's comparison operators: ==, !=, , = Each comparison operator evaluates to either true or false. == 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 , = operators work as expected. 3 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. 4 if Statement pseudocode syntax if by itself: if if, else: if else Java syntax if by itself: if () { } if, else: if () { } else { } 5 if Statement pseudocode syntax if, else if: if else if . . . else Java syntax if, else if, else: if () { } else if () { } . . . else { } more else if's here (optional) optional more else if's here (optional) optional 6 | 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 1 Conditions Throughout this chapter, you’ll see if statements and loop statements where conditions appear within a pair of parentheses, like this: if () { . } while () { . } Typically, each condition involves some type of comparison and the comparisons use comparison operators . 2 Conditions Here are Java's comparison operators: ==, !=, , = Each comparison operator evaluates to either true or false. == 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 , = operators work as expected. 3 if Statement Use