Agenda Warmup Review Finish 1.2 Assignments Lesson 1.3 (If Statements)

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming
Advertisements

Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Introduction to Computer Programming Decisions If/Else Booleans.
Introduction to Computer Programming Loops N Decisions If/Else Counting Loops.
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
Flow of Control Java Programming Mrs. C. Furman January 5, 2009.
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
Agenda Exam #1 Review Modulus Conditionals Boolean Algebra Reading: Chapter Homework #5.
Week 4 - Monday.  What did we talk about last time?  Wrapper classes  if statements.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
If Statements If statements: allow the computer to make a decision Syntax: if (some condition) { then do this; } else { then do that; } no semicolons on.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
1 2. Program Construction in Java. 01 Java basics.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
CPSC 233 Tutorial January 21 st /22 nd, Linux Commands.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures Lecture 10.
CS0007: Introduction to Computer Programming
Decision making If.. else statement.
Branching statements.
A variable is a name for a value stored in memory.
Chapter 3 Control Statements
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Agenda Warmup Lesson 1.4 (double precision, String methods, etc)
Selections Java.
Agenda Warmup Finish 2.4 Assignments
2.5 Another Java Application: Adding Integers
CHAPTER 4 Selection CSEG1003 Introduction to Computing
Lecture 3- Decision Structures
Week 4 - Monday CS 121.
Introduction to Programming
Introduction to Scripting
Chapter 4: Making Decisions.
Agenda Warmup Lesson 2.5 (Ascii, Method Overloading)
SELECTION STATEMENTS (1)
Building Java Programs
SELECTION STATEMENTS (2)
Logical assertions assertion: A statement that is either true or false. Examples: Java was created in The sky is purple. 23 is a prime number. 10.
Building Java Programs
AP Java Review If else.
Decision making If statement.
Introduction to Computer Programming
Visual Basic – Decision Statements
Chapter 5: Control Structure
JavaScript Part 2.
SELECTIONS STATEMENTS
Welcome to AP Computer Science A!
CS2011 Introduction to Programming I Selections (I)
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
AP Java Review If else.
Agenda Warmup Lesson 2.6 (Constructors, Encapsulation)
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Agenda Warmup Lesson 1.6 (Do-while loops, sentinels, etc)
CHAPTER 5: Control Flow Tools (if statement)
Agenda Warmup Lesson 1.4 (double precision, String methods, etc)
Other types of variables
Building Java Programs
Agenda Warmup Lesson 1.9 (random #s, Boolean variables, etc)
Control Statements.
Unit 3: Variables in Java
Agenda Warmup Lesson 2.2 (parameters, etc)
Announcements Lab 3 was due today Assignment 2 due next Wednesday
Agenda Warmup Lesson 1.6 (Do-while loops, sentinels, etc)
Agenda Warmup Lesson 2.8 (Overloading constructors, etc)
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
Agenda Warmup Review Finish 1.2 Assignments Lesson 1.3 (If Statements)
Agenda Warmup Lesson 1.9 (random #s, Boolean variables, etc)
Presentation transcript:

Agenda Warmup Review Finish 1.2 Assignments Lesson 1.3 (If Statements) Guided Practice (1.3 Assignments) Closure Activity Students will be able to: know how to write an if statement use the "and" & "or" operators within an if statement See how today's lesson fits into the unit and the course as a whole

Warmup 67 % 10 = 67 / 10 = 3 / 5 = 3 / 5.0 = 5) int x = 7; int y = 2; double z = (double)(x/y); // what does z equal now? 6) What is byte code? 7) What file extension does a source code file have? 8) Scanner sc = new Scanner(System.in); // what is sc called?

If Statements If statements: allow the computer to make a decision Syntax: if (some condition) { then do this; } else then do that; no semicolons on the If or Else lines!

Comparing variables When comparing variables (except Strings), use the double-equals ( = = ), NOT the equals sign! example: if (GPA = = 3.2)… “Does not equals:” ! = Greater than > Greater than or equal to >= Less than < Less than or equal to <=

If-else if If there are more than 2 possibilities, you must use the “else if” Your final possibility should be the only “else,” although sometimes there is no “else.” Syntax: if (condition_1) { Statement_1; } else if (condition_2) Statement_2; else Statement_3;

And vs. Or “and”  && (Statements will execute only if both conditions are true) “or”  | | (Statements will execute if one condition is true) if ( score >= 80 && score < 90 ) { grade = ‘B’; }

You must put a variable before each <,>,==, <=, >=, etc….. A common mistake: if (score >= 80 && < 90) You must put a variable before each <,>,==, <=, >=, etc….. So the correct code would be: if (score >= 80 && score < 90)

When an and statement is false… Very important: when && is used, once the compiler finds a part of the condition that is false, it ignores the rest of the condition, as well as whatever is inside the attached if-statement. Example: int x = 0; int y = 0; if (x = = 1 && y = = 0) { System.out.println(“Hi”); } In this example, once the compiler sees that x does not equal 1, it ignores everything that follows the &&, including y = = 0. If this had been an || instead of a &&, then the condition would be true, and the if-statement would execute (“Hi” would be displayed).

When comparing Strings in an if statement, you DO NOT use = = Instead, you must use .equals(“ “) or .equalsIgnoreCase(“ “) String dog = “Fido”; if (dog == “Fido”) // false if (dog.equals(“Fido”)) // true if (dog.equalsIgnoreCase(“fido”)) if (dog.equals(“fido”)) ***to use “does not equal” with a String: if (!(dog.equalsIgnoreCase(“fido”)))

Demo: DemoIf

Java uses order of operations (PEMDAS), and modulus division is at the same level as multiplication and division. So, Java really uses PEMMDAS. Example: int x = 5 + 8 / 5 * 3 – 10 % 6 * 2; what would this equal? = 5 + 1 * 3 – 4 * 2 = 5 + 3 – 8 = 0

Assignments (IfAndOr) (Numbers) Prompt the user to enter his or her first name, then (separately) their last name. If the first OR last name is Joe, display “hi”. If the first name is Joe and the last name is Smith, display “bye.” **Test your program: only one of the above results should display, never both.** If the names are the same, display “same,” otherwise, “different.” If both names are “Sirhan,” display “Oh no.” (Numbers) Prompt the user to enter a test score between 0 and 100. Display what letter grade the score corresponds to. (90-100 = A, 80-89 = B, etc) Display whether the score is even or odd. (Use a nested if) If it is even, display whether or not it is prime. Display whether or not the score is divisible by 7. If so, display whether or not it is a multiple of 14. If the score is 97, display “Yay” If the score is not between 0 and 100, display “invalid score”.