Lecture 3 Selection Statements

Slides:



Advertisements
Similar presentations
L5:CSC © Dr. Basheer M. Nasef Lecture #5 By Dr. Basheer M. Nasef.
Advertisements

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4: Selections.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Topic 03 Control Statements Programming II/A CMC2522 / CIM2561 Bavy Li.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 3 Control Statements.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Introduction to Java Programming, 4E Y. Daniel Liang.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Control Statements.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
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.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved.1 Chapter 3 Selections.
COMP 110: Introduction to Programming Tyler Johnson Feb 2, 2009 MWF 11:00AM-12:15PM Sitterson 014.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
CSCI 1100/1202 January 28, The switch Statement The switch statement provides another means to decide which statement to execute next The switch.
Chapter 3 Selections Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Introduction to Control Statements JavaScript – Part 3 George Mason University June 3, 2010.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. CHAPTER 3: SELECTIONS 1.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
More Selection Executing Statements Selectively Chap. 7 (Read § & Part of Picture: Boolean Logic and Digital Design) 1.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Control statements Mostafa Abdallah
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.1 Chapter 3 Selections.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using 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.
Introduction to Control Statements IT108 George Mason University.
Chapter 3 Selections Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved
Chapter 3 Selection Statements
Chapter 4: Control Structures I
Chapter 3 Selections Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Chapter 3 Control Statements
Chapter 4: Control Structures I
Selections Java.
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
CHAPTER 4 Selection CSEG1003 Introduction to Computing
Lecture 3- Decision Structures
Chapter 3 Selections ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH Introduction to Java Programming, Liang (Pearson 2014)
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Chapter 3 Selections Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Chapter 3 Selections Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1.
Chapter 4: Control Structures I
Chapter 3 Selections Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Chapter 4: Control Structures I (Selection)
© Copyright 2016 by Pearson Education, Inc. All Rights Reserved.
Control Structure Chapter 3.
Lecture Notes – Week 2 Lecture-2
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Comparing Data & the ‘switch’ Statement
Comparing Data & the ‘switch’ Statement
CprE 185: Intro to Problem Solving (using C)
Announcements Program 1 due noon Lab 1 due noon
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
Control Structure.
Presentation transcript:

Lecture 3 Selection Statements Boolean Logic boolean variables can have only 2 possible values -- true or false boolean flag = true; Relational Operators Ordering < <= > >= Equality == != Warning: Don’t use = to compare! Evaluate: (7 == 7) (7 != 7) (4 >= 5) (4 <= (5 + 3)) (‘A’ == ‘a’) (‘B’ >= ‘A’)

IF Statements Syntax: if (BooleanExpression) Single Java Statement; where Statement is any Java statement. Semantics: If BooleanExpression is true, then execute the Single Java Statement. (Otherwise, skip the Statement.) a = 4; b = 5; if (a > b) a = a + b; System.out.println(a); System.out.println(b);

What if we want to execute several statements in the then-part? Make a compound statement by using { and } if (num1 > num2){ num1 = num1 + 1; num2 = num2 - 1; } //if What is the output of these Java statements? (i) int x = 7; if(x > 8){ x = x * 2; System.out.println(x); } System.out.println(“Bye!”); (ii) int x = 7; if(x > 8)

Write code that reads in 2 numbers, and then prints the larger number, followed by the smaller number. System.out.println("Enter two integers:"); num1 = keyboard.nextInt(); num2 = keyboard.nextInt(); if (num1 > num2) { System.out.println("Larger one:" + num1); System.out.println("Smaller one:" + num2); } if (num1 <= num2)

if (BooleanExpression) Statement1; else Statement2; Can you write code to just print out the larger of the two? if … else if (BooleanExpression) Statement1; else Statement2; If BooleanExpression is true, execute Statement1; otherwise, execute Statement2. if (num1 > num2) System.out.println("Larger one:" + num1); System.out.println("Larger one:" + num2);

Boolean Operators Truth Table Examples AND (&&) P Q (P && Q) True True True True False False False True False False False False OR (||) P Q (P || Q) True False True False True True NOT (!) P !(P) True False False True

Operator Precedence Chart First: the unary operators: +, -, and ! Second: the binary arithmetic operators: *, /, % Third: the binary arithmetic operators: +, - Fourth: the comparison operators: <, >, =<, >= Fifth: the Boolean operators: ==, != Sixth: the Boolean operator && Seventh: the Boolean operator || Eighth: the assignment operator = TIP: use the ()s for an expression to make the meaning of the expression perfectly clear.

What is the output of these Java statements with the given values of x and y? if ((x >= y) || (x == 3)) { x = x - 1; y = y + 1; } //if else { x = x + 1; y = y - 1; } //else System.out.println(x); System.out.println(y); Case 1. x = 5 and y = 3 Case 2. x = 3 and y = 5 Case 3. x = 4 and y = 5

Nested if…else Statements if (cond1) statement1; else if (cond2) statement2; else if (cond3) statement3; else statement4; if (temp >= 100) ans = “Very Hot”; else if (temp >= 90) ans = “Hot”; else if (temp >= 70) ans = “Warm”; else if (temp >= 50) ans = “Cool”; else ans = “Cold”; Write code that assigns a letter grade for a student, based on his/her score.

if (gender == ‘M’) if (age < 21) premium = 800.00; else premium = 600.00; IMPORTANT: an else is matched with the most recent non-matched if!!! { } //if

switch statements The switch statement provides an alternative to nested if/else statements. Syntax: switch (switch-expression) { case value1: statement(s)1; break; case value2: statement(s)2; … case valueN: statement(s)N; default: statement(s)-for-default; }

Rules of switch statement The switch-expression must yield a value of int (byte, long) or char type since each case evaluates an == test. The value1, …, and valueN must have the same data type as the value of the switch-expression. They cannot contain variables in the expression. The keyword break is optional. The break statement immediately ends the switch statement. The case statements are checked in sequential order, but the order of the cases (including the default case) does not matter. However, it is good programming style to follow the logical sequence of the cases and place the default case at the end. See pages 82-83 for more detail.

int score = ???; switch (score/10) { case 10: case 9: letterGrade = ‘A’; break; case 8: letterGrade = ‘B’; case 7: letterGrade = ‘C’; case 6: letterGrade = ‘D’; default: letterGrade = ‘F’; } //switch System.out.println(“Your letter grade is “ + letterGrade); //Rewrite using nested if-else.

Write an equivalent nested if-else. //suppose score is out of 100 ans = score / 10; switch (ans) { case 0: case 1: case 2: case 3: case 4: case 5: System.out.println(“Grade is F”); break; case 6: System.out.println(“Grade is D”); case 7: System.out.println(“Grade is C”); case 8: System.out.println(“Grade is B”); case 9: case 10: System.out.println(“Grade is A”); default: System.out.println(“invalid grade!”); } // switch Write an equivalent nested if-else.