[PDF] [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



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

1

Building Java Programs

Chapter 4:

Conditional Execution

These lecture notes are copyright (C) Marty Stepp and Stuart Reges, 2007. They may not be rehosted, sold, or modified without expressed permission from the authors. All rights reserved. 2

Lecture outline

Lecture 9■

conditional execution■ the ifstatement and the if/elsestatement relational expressions nested if/elsestatements

Lecture 10■

subtleties of conditional execution■ factoring if/elsecode fencepost loops methods with conditional execution■ revisiting return values 3 if/else statements suggested reading: 4.2 4

The if statement

ifstatement: A Java statement that executes a block of statements only if a certain condition is true.■ If the condition is not true, the block of statements is skipped.

General syntax:

if () { ; ; ;

Example:

double gpa = console.nextDouble(); if (gpa >= 2.0) { System.out.println("Your application is accepted."); 5 if statement flow diagram 6

The if/else statement

if/elsestatement: A Java statement that executes one block of statements if a certain condition is true, and a second block of statements if it is false.■

General syntax:

if () { ; } else { ;

Example:

double gpa = console.nextDouble(); if (gpa >= 2.0) { System.out.println("Welcome to Mars University!"); } else { System.out.println("Your application is denied."); 7 if/else flow diagram 8

Relational expressions

The used in an ifor if/elsestatement is the same kind seen in a forloop. for (int i = 1; i <= 10; i++) { The conditions are actually of type boolean, seen in Ch. 5. These conditions are called relational expressions and use one of the following six relational operators: true5.0 >= 5.0 greater than or equal to >=false126 <= 100 less than or equal to <=true10 > 5 greater than >false10 < 5 less than ValueExampleMeaningOperator 9

Evaluating rel. expressions

Relational operators have lower precedence than math operators.■

Example:5 * 7 >= 3 + 5 * (7 - 1)

5 * 7>= 3 + 5 * 6

35 >= 3 + 30

35 >= 33

true Relational operators cannot be "chained" as they can in algebra.■

Example:2 <= x<= 10

true <= 10 error! 10 if/else question Write code to read a number from the user and print whether it is even or odd using an if/elsestatement.■

Example executions:

Type a number: 42Your number is evenType a number: 17Your number is odd 11

Loops with if/else

Loops can be used with if/elsestatements: int nonnegatives = 0, negatives = 0;for (int i = 1; i <= 10; i++) {

int next = console.nextInt(); if (next >= 0) { nonnegatives++; } else { negatives++; public static void printEvenOdd(int max) { for (int i = 1; i <= max; i++) { if (i % 2 == 0) {

System.out.println(i + " is even");

} else {

System.out.println(i + " is odd");

12

Nested if/else statements

Nested if/else statement: A chain of if/elsethat can select between many different outcomes based on several conditions.■

General syntax:if (

} else if ( } else {

Example:

if (number > 0) {

System.out.println("Positive");

} else if (number < 0) {

System.out.println("Negative");

} else {

System.out.println("Zero");

13

Nested if/else variations

A nested if/elsecan end with an ifor an else.■ If it ends with else, one of the code paths must be taken. If it ends with if, the program might not execute any path.

Example ending with else:

if (place == 1) {

System.out.println("You win the gold medal!");

} else if (place == 2) {

System.out.println("You win a silver medal!");

} else if (place == 3) {

System.out.println("You earned a bronze medal.");

Are there any cases where this code will not print a message? How could we modify it to print a message to non-medalists? 14

Nested if/else flow diagram

if () { ; } else if () { ; } else { ; 15

Nested if/else/if flow diagram

if () { ; } else if () { ; } else if () { ; 16

Sequential if flow

if () { ; if () { ; if () { ; 17

Structures of if/else code

Choose 0, 1, or many of many paths:(conditions/actions are independent of each other)if ( if ( if ( Choose 0 or 1 of many paths:(conditions are mutually exclusive and any action is optional)if ( } else if ( } else if ( Choose 1 of many paths:(conditions are mutually exclusive)if ( } else if ( } else { 18

Which nested if/else to use?

Which if/else construct is most appropriate to perform each of the following tasks?■ Reading the user"s GPA and printing whether the student is on the dean"s list (3.8 to 4.0) or honor roll (3.5 to 3.8).

Printing whether a number is even or odd.

Printing whether a user is lower-class, middle-class, or upper- class based on their income. Reading a number from the user and printing whether it is divisible by 2, 3, and/or 5. Printing a user"s grade of A, B, C, D, or F based on their percentage in the course. 19

Which nested if/else to use?

Which if/else construct is most appropriate to perform each of the following tasks?■ Reading the user"s GPA and printing whether the student is on the dean"s list (3.8 to 4.0) or honor roll (3.5 to 3.8). ■nested if / else if

Printing whether a number is even or odd.

■simple if / else Printing whether a user is lower-class, middle-class, or upper- class based on their income. ■nested if / else if / else Reading a number from the user and printing whether it is divisible by 2, 3, and/or 5. ■sequential if / if / if Printing a user"s grade of A, B, C, D, or F based on their percentage in the course. ■nested if / else if / else if / else if / else 20

How to comment: if/else

Comments on an if statement don"t need to describe exactly what the if statement is testing.■ Instead, they should describe why you are performing that test, and/or what you intend to do based on its result. Bad example:// Test whether student 1"s GPA is better than student 2"sif (gpa1 > gpa2) {

// print that student 1 had the greater GPASystem.out.println("The first student had the greater GPA.");

quotesdbs_dbs14.pdfusesText_20