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.

Slides:



Advertisements
Similar presentations
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures.
Advertisements

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
CS0007: Introduction to Computer Programming
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Chapter 6 Horstmann Programs that make decisions: the IF command.
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.
1 Conditionals In many cases we want our program to make a decision about whether a piece of code should be executed or not, based on the truth of a condition.
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
C++ for Engineers and Scientists Third Edition
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Computer Programming 2 Lab(1) I.Fatimah Alzahrani.
Agenda Exam #1 Review Modulus Conditionals Boolean Algebra Reading: Chapter Homework #5.
CS102 Introduction to Computer Programming Chapter 4 Making Decisions.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
9/6: Variable Types, Arithmetic Operators, Comparison Operators Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic.
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.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Chapter 5: Structured Programming
Conditions CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
CHAPTER#3 PART1 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Selection Statements. Introduction Today we learn more about learn to make decisions in Turing ▫Nested if statements, ▫case statements.
COMP 110: Spring Announcements Lab 1 was due at noon Lab 2 on Friday (Bring Laptops) Assignment 1 is online TA Office hours online 30-min quiz at.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Chapter#3 Part1 Structured Program Development in C++
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
1 CS161 Introduction to Computer Science Topic #8.
COMP Loop Statements Yi Hong May 21, 2015.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Catie Welsh February 9,  Friday - No Lab! ◦ Bring questions on Project 2  Lab 3 due on Friday 2.
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
USING CONDITIONAL CODE AMIR KHANZADA. Conditional Statement  Conditional statements are the set of commands used to perform different actions based on.
CPSC 233 Tutorial 5 February 2 th /3 th, Java Loop Statements A portion of a program that repeats a statement or a group of statements is called.
Introduction to Programming Python Lab 7: if Statement 19 February PythonLab7 lecture slides.ppt Ping Brennan
1 2. Program Construction in Java. 01 Java basics.
Arithmetic You can perform arithmetic with numbers and/or variables. Java follows mathematical order of operations (PEMDAS). Example: / 2  this.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Lesson 4: Introduction to Control Statements 4.1 Additional Operators Extended Assignment Operators –The assignment operator can be combined with the.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Midterm preview.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures Lecture 10.
Decision making If.. else statement.
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
CHAPTER 4 Selection CSEG1003 Introduction to Computing
IF statements.
SELECTION STATEMENTS (1)
Chapter (3) - Looping Questions.
AP Java Review If else.
Decision making If statement.
Selection Statements.
SELECTIONS STATEMENTS
CS2011 Introduction to Programming I Selections (I)
AP Java Review If else.
Relational Operators.
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
CHAPTER 5: Control Flow Tools (if statement)
Control Statements.
Agenda Warmup Review Finish 1.2 Assignments Lesson 1.3 (If Statements)
Agenda Warmup Review Finish 1.2 Assignments Lesson 1.3 (If Statements)
Presentation transcript:

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” 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’; }

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”)) // true if (dog.equals(“fido”)) // false ***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 * 3 – 10 % 6; what would this equal? = * 3 – 4 = – 4 = 4

Assignments 1.(SayMyName)  Prompt the user to enter his or her first name, then (separately) their last name.  If the first OR last name is Walter, give copious praise to the user.  If the first name is Walter and the last name is White, display “Heisenberg.”  **Test your program: only one of the above results should display, never both.** 2.(Numbaz)  Prompt the user to enter a test score between 0 and 100.  Display what letter grade the score corresponds to. ( = A, = B, etc)  Display whether the score is even or odd.  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, shame the user.