Conditions and if/else. Conditions score > 90 Evaluates to true (1) or false (0) Generally … variable operator variable variable operator constant.

Slides:



Advertisements
Similar presentations
Python Programming Language
Advertisements

CSCI 51 Introduction to Programming Dr. Joshua Stough February 10, 2009.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
 2002 Prentice Hall. All rights reserved Control Structures 3 control structures –Sequential structure Built into Python –Selection structure The.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
CS107 Introduction to Computer Science Loops. Instructions Pseudocode Assign values to variables using basic arithmetic operations x = 3 y = x/10 z =
Conditions and if/else. Conditions score > 90 Evaluates to true (1) or false (0) Generally … variable operator variable variable operator constant.
1 9/24/07CS150 Introduction to Computer Science 1 Relational Operators and the If Statement.
1 9/26/08CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
Conditions and Decisions. Assignments Reading – Chapter 4 –
1 Selection in C. 2 If / else if statement:  The else part of an if statement can be another if statement. if (condition) … else if (condition) … else.
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
School of Computing Science CMT1000 Ed Currie © Middlesex University 1 CMT1000: Introduction to Programming Ed Currie Lecture 5B: Branch Statements - Making.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
1 9/25/06CS150 Introduction to Computer Science 1 Nested Ifs, Logical Operators, exit() Page 194.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
Decision Making George Mason University. Today’s topics 2 Review of Chapter 2: Decision Making Go over exercises Decision making in Python.
1 9/28/07CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Control Statements.
Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators.
Conditions and if/else. Conditions score > 90 Evaluates to true (1) or false (0) Generally … variable operator variable variable operator constant.
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007.
Intro to Robots Conditionals and Recursion. Intro to Robots Modulus Two integer division operators - / and %. When dividing an integer by an integer we.
Programming in C++ Lecture Notes 2 – Choice Statements Andreas Savva.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/33 Conditionals and Loops Now we will examine programming statements.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
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
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
Homework Assignment #3 J. H. Wang Oct. 29, 2007.
Chapter 3 Selections Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Rational Expressions and selection structures Relational operators Logical operators Selection structures.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Computer Science 111 Fundamentals of Programming I Making Choices with if Statements.
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Python Conditionals chapter 5
Flow of Control Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are written.
Control statements Mostafa Abdallah
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Python Basics  Values, Types, Variables, Expressions  Assignments  I/O  Control Structures.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
Copyright © Curt Hill The C++ IF Statement More important details More fun Part 3.
 Type Called bool  Bool has only two possible values: True and False.
Principles of Programming - NI July Chapter 4: Basic C Operators In this chapter, you will learn about: Assignment operators Arithmetic operators.
Lecture 3 Selection Statements
Operators and Expressions
Making Choices with if Statements
Upsorn Praphamontripong CS 1110 Introduction to Programming Fall 2016
More important details More fun Part 3
Decisions Chapter 4.
CMPT 201 if-else statement
Chapter 4: Control Structures
SELECTION STATEMENTS (1)
Arithmetic operations, decisions and looping
Practice with loops! What is the output of each function below?
Relational, Logical, and Equality Operators
CS 101 First Exam Review.
CprE 185: Intro to Problem Solving (using C)
Lecture 9: Implementing Complex Logic
REPETITION Why Repetition?
Presentation transcript:

Conditions and if/else

Conditions score > 90 Evaluates to true (1) or false (0) Generally … variable operator variable variable operator constant

Comparison Operators < > <= >= == –NOT the same as = !=

Examples x=5 y=8 z=5 MAX=10 initial=‘s’ x<y y>MAX x<=z z>=MAX initial==‘r’ x!=z

Logical Operators and or not x=5 y=8 z=5 MAX=10 initial=‘s’ x MAX not(x>y)

Precedence function calls unary operators and binary power (-, **) * / % + - = > == != not and or

Short-Circuit Evaluation Stop evaluation when true/false value is determined x=6 y=9 x>2 or y > 13 x 13

Logical Assignment and Negation in_range = (x>0 and x<=10) # 1 if x between 1-10, 0 otherwise in_range = 0<x<=10 #Java does not allow this!!! same_initials = (first_initial==‘S’and last_initial==‘R’) not_same_initials = not(first_initial==‘S’and last_initial==‘R’) not_same_initials = (first_initial!=‘S’ or last_initial!=‘R’)

DeMorgan’s Theorem not(a and b) => (not(a) or not(b)) not(a or b) => (not(a) and not(b))

Exercises 1.Determine the results of the following statements given a=6 b=9 c=12 d=-7 e=0 f=12 : 1.print a > d 2.print c <= f 3.print d > e 4.print c = f 5.print c == f 6.print c > b and e > f 7.print c > b or e > f 8.print a or e 9.print e and a

if Statement Statements MUST be indented if condition: statements if age >= 16: print “You can get a driver’s license.” if age > 21: print “You can purchase alcohol.” print “You can gamble.” if age >= 16 and age < 21: print “You can drive but you cannot gamble.”

if/else Statement if condition: statements else: statements if grade > 60: print “You passed the class.” print “Next up, CS112.” else: print “Sorry, you did not pass.” print “Try again next semester.”

Nested if Statements if condition: statement else: statement else: statement if grade > 60: print "You passed the class." if grade > 90: print "You passed with an A!" else: print "Sorry, you did not pass."

Example if num > 0 and num <= 10: print “Your number is between 1 and 10” else: if num > 10: print “Your number is too high” else: print “Your number is too low”

Chained Conditionals if num > 0 and num <= 10: print “Your number is between 1 and 10” else: if num > 10: print “Your number is too high” else: print “Your number is too low” if num > 0 and num <= 10: print “Your number is between 1 and 10” elif num > 10: print “Your number is too high” else: print “Your number is too low”

Example if grade > 60: print "You passed the class." if grade > 90: print "You passed with an A!" else: print "Sorry, you did not pass.” #Does this work??? if grade > 60: print "You passed the class." elif grade > 90: print "You passed with an A!" else: print "Sorry, you did not pass."

Using Functions def getGrade(score): if score > 90: return “A” elif score > 80: return “B” elif score > 70: return “C” elif score > 60: return “D” else: return “F”

Exercises 1.Write an if statement that compares two integer variables x and y and prints the largest. For example, your program would print “X is larger than Y” or “Y is larger than X”. 2.Modify your program above so that it compares three integers x, y, and z and prints the largest. 3.Write a function that takes as input a year and returns true if the year is a leap year and false otherwise. A year is a leap year if it is divisible by four, except that any year divisible by 100 is a leap year only if it is divisible by 400 as well. -Problem Solving and Program Design in C Hanly and Koffman