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
Selection Victor Norman CS104 Calvin College. Reading Quiz Counts toward your grade.
Advertisements

Python Programming Language
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.
Overview Program flow Decision / branching / selection structures True & False in Python Comparison operators & Boolean expressions if … if … else if …
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Conditions and if/else. Conditions score > 90 Evaluates to true (1) or false (0) Generally … variable operator variable variable operator constant.
Control Structures 4 Control structures control the flow of execution of a program 4 The categories of control structures are: –Sequence –Selection –Repetition.
1 9/26/08CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
Boolean Types & Compound Conditionals CSC 1401: Introduction to Programming with Java Lecture 4 – Part 3 Wanda M. Kunkle.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
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 –
true (any other value but zero) false (zero) expression Statement 2
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
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.
Nested if statements When one if/else structure is contained inside another if/else structure is called a nested if/else. if (grade > 60) if (grade > 70)
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
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.
Chapter 4 Making Decisions
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.
Selection in C.
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.
Decision Structures and Boolean Logic
CPS 125: Digital Computation and Programming Selection Structures: if and switch Statements.
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.
Rational Expressions and selection structures Relational operators Logical operators Selection structures.
Flow of Control Part 1: Selection
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Computer Science 111 Fundamentals of Programming I Making Choices with if Statements.
Chapter 3:Decision Structures.  3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical.
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater.
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
The If Statement There are no switch statements in Python. You need to use just if statements. There are no switch statements in Python. You need to use.
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.
Control Statements: Part1  if, if…else, switch 1.
Today… Operators, Cont. Operator Precedence Conditional Statement Syntax. Winter 2016CISC101 - Prof. McLeod1.
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.
 2006 Pearson Education, Inc. All rights reserved if…else Double-Selection Statement if – Performs action if condition true if…else – Performs.
 Type Called bool  Bool has only two possible values: True and False.
Lecture 3 Selection Statements
Chapter 3 Selection Statements
Operators and Expressions
Making Choices with if Statements
Upsorn Praphamontripong CS 1110 Introduction to Programming Fall 2016
Chapter 4: Control Structures
If statement.
Bools & Ifs.
Practice with loops! What is the output of each function below?
Python Programming Language
CHAPTER 5: Control Flow Tools (if statement)
CS 101 First Exam Review.
Lecture 9: Implementing Complex Logic
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”