SELECTION STATEMENTS (1)

Slides:



Advertisements
Similar presentations
INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter4: Control Statements Part I.
Advertisements

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
Introduction to Computer Programming Decisions If/Else Booleans.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
If statements Chapter 3. Selection Want to be able to do a statement sometimes, but not others if it is raining, wear a raincoat. Start first with how.
CSC 1051 M.A. Papalaskari, Villanova University Repetition CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing.
Flow of Control Java Programming Mrs. C. Furman January 5, 2009.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Chapter 5 Loops.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
Chapter 4: Control Structures SELECTION STATEMENTS.
1 Chapter 3 Selections. 2 Outline 1. Flow of Control 2. Conditional Statements 3. The if Statement 4. The if-else Statement 5. The Conditional operator.
Chapter 4: Control Structures II
CHAPTER#3 PART1 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc.
CHAPTER#3 PART1 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc.
Topics Logical Operators (Chapter 5) Comparing Data (Chapter 5) The conditional operator The switch Statement The for loop Nested Loops.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
Chapter#3 Part1 Structured Program Development in C++
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
SELF STUDY. IF STATEMENTS SELECTION STRUCTURE if selection statement if … else selection statement switch selection statement.
CONTROL STRUCTURE. 2 CHAPTER OBJECTIVES  Learn about control structures.  Examine relational and logical operators.  Explore how to form and evaluate.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
Control Statements: Part1  if, if…else, switch 1.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Java Fundamentals 4. Java Programming: From Problem Analysis to Program Design, Second Edition2 Parsing Numeric Strings  Integer, Float, and Double are.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C 1 A.AlOsaimi King Saud University College of Applied studies and Community Service Csc 1101.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CompSci 230 S Programming Techniques
Decision making If.. else statement.
Java Fundamentals 4.
Chapter 4: Control Structures I
CSC111 Quick Revision.
Chapter 4: Control Structures I
Introduction to programming in java
Chapter 6 More Conditionals and Loops
Chapter 5: Control Structures II
Chapter 4: Control Structures
Chapter 3 Selections Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Repetition-Counter control Loop
Chapter 5: Control Structures II
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
INPUT STATEMENTS GC 201.
Control Statement Examples
Building Java Programs Chapter 4
Chapter 4: Control Structures I
SELECTION STATEMENTS (2)
AP Java Review If else.
Control Statements Loops.
Decision making If statement.
Self study.
Building Java Programs
Control Structure Chapter 3.
Building Java Programs
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
AP Java Review If else.
Control Statements Loops.
CSC 1051 – Data Structures and Algorithms I
Building Java Programs
Repetition CSC 1051 – Data Structures and Algorithms I Course website:
Presentation transcript:

SELECTION STATEMENTS (1) if if…else GC 201

Outline 1. Introduction 2. One-way selection: The if statement 2.1 With a single statement 2.2 With a block of statements 3. Two-way selection: The if…else statement 3.1 With a single statement 3.2 With a block of statements

Selection

1. Introduction By default, the flow of a program is sequential. Selection statements alters the flow of execution depending on one or more specific conditions. The condition(s) is/are specified by the programmer. In Java, the selection statements include: The if statement With a single statement With a block of statements The if…else statement The nested if The conditional operator ? The switch statement

With a single statement 2. The if Statement With a single statement SYNTAX if (logical expression) statement1; FLOWCHART 1 Expression referred to as decision maker. Statement referred to as action statement. Logical Expression? True If the logical expression is true: Statement 1 Statement 1 is executed False Execution continues at point 2 If the logical expression is false: Statement 1 is NOT executed 2 Execution continues at point 2

If G more than or equal to 60 Write a program that read the grade of the student and if it is more than or equal 60; print “Pass” Algorithm Start Read the grade G If G more than or equal to 60 Print “Pass” End Start Read G If G >= 60 False True Print “pass” End

Code import java.util.*; public class grade { static Scanner console = new Scanner (System.in); public static void main(String[] args) { double grade; System.out.println("Enter Grade"); grade = console.nextDouble(); if (grade >= 60) System.out.println(“PASS"; } }

With a single statement 3.1 The if…else Statement With a single statement SYNTAX if (logical expression) Statement1; else Statement2; FLOWCHART 1 Logical Expression? If the logical expression is true: True False Statement 1 is executed Execution continues at point 2 Statement 1 Statement 2 If the logical expression is false: Statement 2 is executed Execution continues at point 2 2

With a single statement – PROGRAM 2: ANALYSIS 3.1 The if…else Statement With a single statement – PROGRAM 2: ANALYSIS Write a program that read two numbers and print the largest one Number1 : (variable: num1, type: int) Number2 : (variable: num2, type: int) INPUT OUTPUT Largest number PROCESS if (num1 > num2) print num1; if (num2 > num1) print num1;

With a single statement – PROGRAM 3: Algorithm - Flowchart 3.1 The if…else Statement With a single statement – PROGRAM 3: Algorithm - Flowchart Start Read first number (num1) Read second number (num2) If num1 is larger than num2 Print num1 else Print num2 Start Read num1 Read num2 If num1 > num2 False True Print num1 Print num2 End

With a single statement - PROGRAM 3: CODE 3.1 The if…else Statement With a single statement - PROGRAM 3: CODE // import necessary libraries import java.util.*; //contains the class Scanner public class ifElseStatement1 { // instantiate the object console from the class Scanner static Scanner console = new Scanner (System.in); public static void main (String[] args) // Declaration section: to declare needed variables int num1, num2; // Input section: to enter values of used variables System.out.println (“Enter number1”); //prompt num1 = console.nextInteger(); System.out.println (“Enter number2”); //prompt num2 = console.nextInteger(); // Processing section: processing statements if (num1 > num2) System.out.println (num1); else System.out.println (num2); // Output section: display program output } // end main } // end class 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

Two-Way Selection Example 4-14 //The following statement shows an example of a syntax error… WHY ? if (hours > 40.0); //Line 1 wages = 40.0 * rate + 1.5 * rate *(hours - 40.0); //Line 2 else //Line 3 wages = hours * rate; //Line 4 Because a semicolon follows the closing parenthesis of the if statement (Line 1), the else statement stands alone. The semicolon at the end of the if statement (see Line 1) ends the if statement. The statement at Line 2 separates the else clause from the if statement. That is, else is by itself. Because there is no separate else statement in Java, this code generates a syntax error. Java Programming: From Problem Analysis to Program Design, D.S. Malik

With a block of statements 2.2 The if Statement With a block of statements SYNTAX if (logical expression) { statement 1; statement 2; … statement n; } FLOWCHART 1 Logical Expression? True If the logical expression is true: Statements 1 & 2 are executed Statement 1 Execution continues at point 2 Statement 2 False If the logical expression is false: Execution continues at point 2 2

If G more than or equal to 60 Write a program that read the grade of the student and if it is more than or equal 60; print “Pass” and the grade Algorithm Start Read the grade G If G more than or equal to 60 Print “Pass” Print G End Start Read G If G >= 60 False True Print “pass” Print G End

Code import java.util.*; public class grade { // Class start static Scanner console = new Scanner (System.in); public static void main(String[] args) { // method start double grade; System.out.println("Enter Grade"); grade = console.nextDouble(); if (grade >= 60) { // if block start System.out.println(“PASS“); System.out.println(grade); } // if block end } // method end } // Class end

With a block of statements 3.2 The if…else Statement With a block of statements SYNTAX if (logical expression) { Statement i; Statement i+1; … Statement m; } else Statement j; Statement j+1; Statement n; FLOWCHART 1 Logical Expression? True False Statement i Statement j Statement i+1 Statement j+1 2

With a block of statements – PROGRAM 4: ANALYSIS 3.2 The if…else Statement With a block of statements – PROGRAM 4: ANALYSIS Write a program that assigns a special discount to an item according to its price, then calculates the net price as follows: If the price is greater than 1,000 SR then the customer is given a VIP status and the item is given a 15% discount; otherwise, the item is given an 18% discount. The program should also display the customer status if VIP. INPUT Price of the item (variable: price, type: double) OUTPUT Discount (variable: discount, type: double) Net Price (variable: netPrice, type: double) Customer Status (variable: vip, type: boolean) PROCESS if (price > 1000)  1) discount = 0.15 2) vip = true if (price <= 1000)  1) discount = 0.18 2) vip = false netPrice = (1 – discount) * price

With a block of statements - PROGRAM 4: CODE 3.2 The if…else Statement With a block of statements - PROGRAM 4: CODE // import necessary libraries import java.util.*; //contains the class Scanner public class ifElseStatementN { // instantiate the object console from the class Scanner static Scanner console = new Scanner (System.in); public static void main (String[] args) // Declaration section: to declare needed variables double price, discount, netPrice; boolean vip; // Input section: to enter values of used variables System.out.println (“Enter the price of the item”); //prompt price = console.nextDouble(); // Processing section: processing statements if (price > 1000.0) // price is double discount = 0.15; vip = true; } //end if(price > 1000.0) else discount = 0.18; vip = false; } //end else if(price > 1000) netPrice = (1 – discount) * price; // Output section: display program output System.out.printf (“Discount = %.2f, Net price = %.2f, vip = %5s“, discount, netPrice, vip); } // end main } // end class 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

(LogicalExpression1) ? Expression2: Expression3; 2. CONDITIONAL OPERATOR ? Also known as the ternary operator. SYNTAX Variable = (LogicalExpression1) ? Expression2: Expression3; If expression1 = true, then the result of the condition is expression2. Otherwise, the result of the condition is expression3. int x = 3, y = 5, min; min = (x <= y) ? x : y; max = (x >= y) ? x: y; System.out.println (min); System.out.print (max); Example 3 5_ 1 2 Output

2. CONDITIONAL OPERATOR ? Example int x = 3, y = 5, min; min = (x <= y) ? x : y; System.out.print (min); The above example is equivalent to the following code: if (x <= y) min = x; else min = y; 1 2 3 4

Self-Check Exercises Write a complete Java program that identifies the students whose grades are B. Write a complete Java program that reads two integer numbers num1 & num2, and performs a division if num2 is not equal to zero. Write a complete Java program that reads two integer numbers num1 & num2, and prints the smaller one. Write a complete Java program that identifies a negative number. Write a complete Java program that identifies an even number. W5.1 if + if…else