Slide 1

Slide 1 text

CSE110 Principles of Programming with Java Lecture 08: Conditional Statements Javier Gonzalez-Sanchez [email protected] javiergs.engineering.asu.edu | javiergs.com Office Hours: By appointment

Slide 2

Slide 2 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 2 Announcement Assignment 1 is due today (11:59 PM AZ Time) Midterm Exam (Monday June 8, 6:00PM to 7:15PM AZ time)

Slide 3

Slide 3 text

Previously

Slide 4

Slide 4 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 4 Input

Slide 5

Slide 5 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 5 Input import javax.swing.JOptionPane; public class Lecture06 { public static void main(String [] args) { String test1= JOptionPane.showInputDialog(”write here:"); int value1 = Integer.parseInt(test1); double value2 = Double.parseDouble(test1); System.out.println(value1); System.out.println(value2); } }

Slide 6

Slide 6 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 6 Expressions class global variables methods statements local variables instructions expressions arithmetic expression relational expression logical expression

Slide 7

Slide 7 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 7 Relational expressions A condition uses relational operators, which all return boolean results: == equal to != not equal to < less than > greater than <= less than or equal to >= greater than or equal to Note the difference between the equality operator (==) and the assignment operator (=)

Slide 8

Slide 8 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 8 Boolean (logical) expressions A condition uses relational operators, which all return boolean results: && AND || OR ! NOT

Slide 9

Slide 9 text

Control Statements

Slide 10

Slide 10 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 10 Topics class global variables methods statements instructions local variables expressions

Slide 11

Slide 11 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 11 Topics class global variables methods statements instructions local variables expressions conditional Statements loop Statements

Slide 12

Slide 12 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 12 Flow of Control Unless specified otherwise, the order of statement execution through a method is linear: one statement after the other in sequence (top down order). public static void main (String [] args) { System.out.println(“one”); System.out.println(“two”); System.out.println(“three”); } The order of statement execution is called the flow of control

Slide 13

Slide 13 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 13 Flow of Control Some programming statements modify that order (flow of control), allowing us to: • decide whether to execute a statement, or • perform a statement over and over, repetitively These decisions are based on a boolean expression (also called a condition) that evaluates to true or false.

Slide 14

Slide 14 text

Conditional Statements

Slide 15

Slide 15 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 15 Conditional Statements • A conditional statement lets us choose which statement will be executed next • Java's conditional statements are o the if statement o the if-else statement o the switch statement o the operator ?

Slide 16

Slide 16 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 16 if statement • The if statement has the following syntax:

Slide 17

Slide 17 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 17 Example int MAX = 5; int sum = 30; int delta = 0; if (sum > MAX) { delta = sum - MAX; } System.out.println("The delta is " + delta);

Slide 18

Slide 18 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 18 Flow Chart of an if statement

Slide 19

Slide 19 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 19 if-else statement An else clause can be added to an if statement to make an if-else statement if ( condition ) { statement1; } else { statement2; } • If the condition is true, statement1 is executed; if the condition is false, statement2 is executed • One or the other will be executed, but not both

Slide 20

Slide 20 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 20 Flow chart of an if-else statement

Slide 21

Slide 21 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 21 Nested if Statements • The statement executed as a result of an if statement or else clause could be another if statement • These are called nested if statements • An else clause is matched to the last unmatched if (no matter what the indentation implies) • Braces can be used to specify the if statement to which an else clause belongs

Slide 22

Slide 22 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 22 if-else if-else You can also have multiple conditions to be verified: if (temp > 100) { System.out.println("It is hot!"); } else if (temp > 80) { System.out.println("It is warm"); } else if (temp > 50) { System.out.println("It is chilly"); } else { System.out.println("It is cold!"); }

Slide 23

Slide 23 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 23 if-else if-else The code from the previous page is equivalent to: if (temp > 100) { System.out.println("It is hot!"); else { if (temp > 80) { System.out.println("It is warm"); } else { if (temp > 50){ System.out.println("It is chilly"); } else { System.out.println("It is cold!"); } } }

Slide 24

Slide 24 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 24 Reference Textbook – Section 3.1, 3.2, and 3.3

Slide 25

Slide 25 text

CSE110 - Principles of Programming Javier Gonzalez-Sanchez [email protected] Summer 2020 Disclaimer. These slides can only be used as study material for the class CSE110 at ASU. They cannot be distributed or used for another purpose.