[PDF] [PDF] Conditional Statements Conditional statements The if statement The

15-110 (Reid-Miller) 4 The if Flowchart boolean_expression statement true false The statement in the if statement can be any Java The if-else Flowchart



Previous PDF Next PDF





[PDF] Conditional Statements Conditional statements The if statement The

15-110 (Reid-Miller) 4 The if Flowchart boolean_expression statement true false The statement in the if statement can be any Java The if-else Flowchart



[PDF] If Else Statement Java Example - AWS

above you can visit the statements execute the flowchart Comes in other value of new to perform another if else if statements using java that is the one code?



[PDF] if-else statement in java - Tutorialspoint

Flow Diagram Example: public class Test { public static void main(String args[]){ int x = 30; if( x < 20 ){ System out print("This is if statement"); }else{



[PDF] CASCADING IF-ELSE

A cascading if-else is a composite of if-else statements where the false path path 1 after which it branches to the statement below the cascading if-else structure a Java code fragment that inputs a donor's name and contribution and prints 



[PDF] Chapter 3 Selection Statements

Java provides selection statements that let you choose actions with two or more alternative courses • Selection o if Statements, if else statements, nested if statements o switch statement(s); } // execution flow chart is shown in Figure (A )



[PDF] Chapter 3

execution flow chart is shown in Figure (A) Example if (radius > The statement in an if or if else statement can be any legal Java statement, including another  



[PDF] if / else - Building Java Programs

if statement: A Java statement that executes a block of statements If the condition is not true, the block of statements is skipped Nested if/else/if flow diagram



[PDF] Statements and Control Flow

flow of instructions in Java is to execute the statements of the program in This flowchart is the same as the one for the if statement, with the addition of an



[PDF] Chapter 3: Decision Structures

If statements can be modeled as a flow chart Wear a coat Nested if Statement Flowcharts Wear a jacket Java provides two binary logical operators (&



[PDF] Conditional Programming

A conditional statement lets us choose which statement will be Java's conditional statements are the if statement, the if- if-else Statement Flow Diagram 7

[PDF] flower dictionary with pictures book

[PDF] flower emoji meanings

[PDF] flower encyclopedia pdf

[PDF] flower meaning gratitude

[PDF] flower names and meanings

[PDF] flower names and pictures pdf

[PDF] flowers and their meanings in literature

[PDF] flowers name pdf download

[PDF] flowers of bengal

[PDF] flowers pdf download

[PDF] flu cases world map

[PDF] flu deaths 2018 2019

[PDF] flu deaths 2019

[PDF] flu deaths 2019 usa by age group

[PDF] flu deaths 2019 worldwide chart

Conditional Statements

15-110 Summer 2010

Margaret Reid-Miller

Summer 2010 15-110 (Reid-Miller) 2

Conditional statements

• Within a method, we can alter the flow of control (the order in which statements are executed) using either conditionals or loops.

• The conditional statements if, if-else, and switch allow us to choose which statement will be executed next. • Each choice or decision is based on the value of a boolean expression (also called the condition).

Summer 2010 15-110 (Reid-Miller) 3

The if statement

• If we have code that we sometimes want to execute and sometimes we want to skip we can use the if statement.

• The form of the if statement is if (boolean_expression) statement • If boolean_expression evaluates to true, then statement is executed. • If boolean_expression evaluates to false, then statement is skipped. • Note that the boolean_expression enclosed in parentheses must evaluate to true or false.

Summer 2010 15-110 (Reid-Miller) 4

The if Flowchart

boolean_expression statement true false

Summer 2010 15-110 (Reid-Miller) 5

if-Statement Examples !if (count > 0) !

average = total / count;!if (age >= 26) !!if (hasLicense == true)!!System.out.println("You may rent a car.");!daysInFeb = 28;!! if (isLeapYear) {!daysInFeb = 29;!System.out.println(year + " is a leap year.");!}!Or simply

hasLicense

Summer 2010 15-110 (Reid-Miller) 6

The if Statement

• The statement in the if statement can be any Java statement:

• A simple statement • A compound statement, such as an if statement • A block statement, a group of statements

enclosed in braces {} if (zipcode == 15213) {!city = "Pittsburgh";!state = "PA";!}! Proper indentation becomes essential!

Summer 2010 15-110 (Reid-Miller) 7

The if-else Statement

• If we want to choose between two alternative we use the if/else statement: if (boolean_expression)!

statement1!else !! statement2! • If boolean_expression evaluates to true, then statement1 is executed. • If boolean_expression evaluates to false, then statement2 is executed.

Summer 2010 15-110 (Reid-Miller) 8

The if-else Flowchart

boolean_expression statement1 true false!statement2

Summer 2010 15-110 (Reid-Miller) 9

if-else Statement Examples !if (temperature <= 32.0) {!

! forecast = "SNOW"; !!} ! else {! forecast = "RAIN";!!} !if (count > 0) { !! average = total / count; !!} ! else {!! System.out.println("No data to average.");!!}

The then clause The else clause

Summer 2010 15-110 (Reid-Miller) 10

Common Error 1

• When you want to test if the value of a variable is in a range. !if (0 < temperature < 100) { ! ! state = "LIQUID"; !!}!WRONG!! if (0 < temperature && temperature < 100) { ! !state = "LIQUID"; !}!Correct

Summer 2010 15-110 (Reid-Miller) 11

Common Error 2

• When you want to test if the value of a variable is one of two alternates. !if (choice == 'M' || 'L') { ! ! System.out.println("You're correct!"); !!}!WRONG!! if (choice == 'M' || choice == 'L') { ! ! System.out.println("You're correct!"); !}!Correct

Summer 2010 15-110 (Reid-Miller) 12

The Dangling else Problem

• When an if statement is nested inside the then clause of another if statement, the else clause is paired with the closest if statement without an else clause. if (x > 0) ! if (y > 0)! color = "red"; !else ! color = "blue"; Misleading indentation

Summer 2010 15-110 (Reid-Miller) 13

The Dangling else Problem

• In reality it is if (x > 0) ! if (y > 0)! color = "red"; ! else ! color = "blue"; y x y x

Summer 2010 15-110 (Reid-Miller) 14

The Dangling else Problem

• Use braces to pair else with the outer if if (x > 0) {! if (y > 0)! color = "red"; !} !else {! color = "blue";!}! • Compare flowcharts! y x

Summer 2010 15-110 (Reid-Miller) 15

Multiple Alternatives

• Determine if a number is positive, negative, or zero: if (value < 0) {!!System.out.println("Value is negative.");!} !if (value == 0) {!!System.out.println("Value is zero.");!}!if (value > 0) {!!System.out.println("Value is positive.");!}!

Computer thinks any combination of the three statements can be executed.

Summer 2010 15-110 (Reid-Miller) 16

Multiple Alternatives

• Determine if a number is positive, negative, or zero

if (value < 0) {!! System.out.println("Value is negative.");!} !else {!! if (value == 0) {!!! System.out.println("Value is zero.");!! } ! else {!!! if (value > 0) {!!!! System.out.println("Value is positive.");!!! }!! }!}!At most one statement is executed. Leads to lots of indentation.

Summer 2010

• Determine if a number is positive, negative, or zero if (value < 0) {!! System.out.println("Value is negative.");!} ! else {! ! if (value == 0) {!!! System.out.println("Value is zero.");!! } ! else {! !! if (value > 0) {!!!! System.out.println("Value is positive.");!!! }!

15-110 (Reid-Miller) 17

Multiple Alternatives

Remove unnecessary brackets and re-indent

Summer 2010 15-110 (Reid-Miller) 18

Multiple Alternatives

• Determine if a number is positive, negative, or zero:

if (value < 0) {!! System.out.println("Value is negative.");!} !else if (value == 0) {!!!System.out.println("Value is zero.");!} !else if (value > 0) {!!!System.out.println("Value is positive.");!}!

At most one statement is executed. Each choice, however, is at same indentation.

Summer 2010 15-110 (Reid-Miller) 19

Multiple Alternatives

• Determine if a number is positive, negative, or zero:

if (value < 0) {!!System.out.println("Value is negative.");!} !else if (value == 0) {!!!System.out.println("Value is zero.");!} !else { // value must be positive!!!System.out.println("Value is positive.");!}!

It is clear, exactly one statement is executed.

Summer 2010 15-110 (Reid-Miller) 20

Multiple Alternatives: Assignments

• Determine the fare: $2 for a child (no more than 11 years), $3 for a senior (at least 65 years), or $5 for an adult.

int fare;!if (age _______) {!!fare = 2;!} !else if (age __________) { // _____________________!!fare = 5;!} !else { // ________________!!fare = 3;!}!System.out.println("Your fare is $" + fare);!fare must be defined before the if statement

last clause must be else with no if

Exercise

• Write a method that prints how many of n1, n2, and n3 are odd: public void printNumOdd(int n1, int n2, int n3) {!}!

Summer 2010 15-110 (Reid-Miller) 21

Exercise

• Write a method that print whether die1 and die2 are doubles, cat's eyes (two 1's) or neither of these. public void printDoubles(int die1, int die2) {!

Summer 2010 15-110 (Reid-Miller) 22 Summer 2010 15-110 (Reid-Miller) 23

Programming Style

• Single-line if statement: if (y > 0) color = "red";! • Multi-line if statement: if (zipcode == 15213) {! city = "Pittsburgh";! state = "PA";!} • The if-else statement: if (temperature <= 32.0) {! forecast = "SNOW"; !} !else {! forecast = "RAIN";!}! • Multiple alternatives:

if (value < 0) {! valueType = "negative";!} !else if (value == 0) {! valueType = "zero";!} !else { // no if here!!! valueType = "positive";!}!

Summer 2010 15-110 (Reid-Miller) 24

Testing For Equality

• For primitive values use == for equality testing. • For objects, use the equals method for testing equal contents. • The argument must be the same type as the object on which

equals() is called. The method returns true or false depending on whether both objects are "equal" or not.

• For example, let day be an int variable and month be a String variable. ! if (day == 1 && month.equals("APRIL")) {! ! System.out.println("It's April Fool's Day");! Two String objects are equal if they have exactly the same characters, including case and number of characters.

Summer 2010 15-110 (Reid-Miller) 25

Testing for Equality with doubles

• Which statement will Java print?

double x = Math.sqrt(2.0);!double y = x * x;!if (y == 2.0) {!!System.out.println("sqrt(2) * sqrt(2) is 2");!} !else {!!System.out.println("sqrt(2) * sqrt(2) "!!!!!+ "is not 2. It is " + y);!}

Never test for exact equality with floating point numbers!

Summer 2010 15-110 (Reid-Miller) 26

Testing for Equality with doubles

• Because of round-off errors, you should test if the numbers are close.

double tolerance = 1.0e-10;double x = Math.sqrt(2.0);!double y = x * x; !if (Math.abs(y - 2.0) < tolerance) {!!System.out.println("sqrt(2) * sqrt(2) is 2");!} !else {!!System.out.println("sqrt(2) * sqrt(2) "!!!!!+ "is not 2. It is " + y);!}!

Summer 2010 15-110 (Reid-Miller) 27

Short-Circuit Evaluation

• Short circuit evaluation (or lazy evaluation) : If the first conditional in an && expression is false, Java does not execute the second conditional.

Example:

! if (liters > 0 && total/liters > threshold) {!! System.out.println("WARNING: Exceeds threshold");! }

What if the expression was an || expression?

Summer 2010 15-110 (Reid-Miller) 28

The switch statement

• If an if/else statement with multiple alternatives compares an int or char variable or expression against several constants you can use a switch statement.

Example: !switch (suitAsChar) {!

!case 'C': suitAsName = "Clubs"; break;!!case 'D': suitAsName = "Diamonds"; break;!!case 'H': suitAsName = "Hearts"; break;!!case 'S': suitAsName = "Spades"; break;!!default: suitAsName = "Unknown";!}!

quotesdbs_dbs5.pdfusesText_9