CHAPTER 5: Control Flow Tools (if statement)

Slides:



Advertisements
Similar presentations
Chapter 4 - Control Statements
Advertisements

Conditional Statements Introduction to Computing Science and Programming I.
School of Computing Science CMT1000 Ed Currie © Middlesex University 1 CMT1000: Introduction to Programming Ed Currie Lecture 5B: Branch Statements - Making.
Lecture 11. Today’s topic Conditional statement –Relational operators –if statement –if-else statement –If-elseif statement.
The If/Else Statement, Boolean Flags, and Menus Page 180
Chapter 4 MATLAB Programming Logical Structures Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Decisions in Python elif. A new keyword elif A contraction of “else if” Used to tie two if statements (or more) together into one structure Syntax – elif,
Lecture Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition.
Lecture 11 – if … else, if... elif statements, nested ifs COMPSCI 1 1 Principles of Programming.
Chapter 9 IF Statement Bernard Chen. If Statement The main statement used for selecting from alternative actions based on test results It’s the primary.
Line Continuation, Output Formatting, and Decision Structures CS303E: Elements of Computers and Programming.
Branching and Conditions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Python – Making Decisions Lecture 02. Control Structures A program that only has one flow is useful but limited. We can use if statements to make these.
Lecture 10 – Boolean expressions, if statements COMPSCI 101 Principles of Programming.
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Computer Science 111 Fundamentals of Programming I Making Choices with if Statements.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Lecture 3 – Selection. Outline Recall selection control structure Types of selection One-way selection Two-way selection Multi-selection Compound statement.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 7 Conditionals and Loops 4/18/09 Python Mini-Course: Day 2 - Lesson 7.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Conditional Execution Chapter 3 Python for Informatics: Exploring Information
Decision Making CMSC 201. Overview Today we will learn about: Boolean expressions Decision making.
Chapter 3: Branching and Program Flow CSCI-UA 0002 – Introduction to Computer Programming Mr. Joel Kemp.
Python Conditionals chapter 5
Decision Structures, String Comparison, Nested Structures
8. DECISION STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Decision Making CMSC 201 Chang (rev ).
3.1.3 Program Flow control Structured programming – SELECTION in greater detail.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
PH2150 Scientific Computing Skills Control Structures in Python In general, statements are executed sequentially, top to bottom. There are many instances.
Control Flow (Python) Dr. José M. Reyes Álamo.
Control Structures I Chapter 3
Chapter 4 Selections © Copyright 2012 by Pearson Education, Inc. All Rights Reserved.
Selection: Non-linear programs
Line Continuation, Output Formatting, and Decision Structures
IF statements.
Operator Precedence Operators Precedence Parentheses () unary
Chapter 4: Control Structures
Topics The if Statement The if-else Statement Comparing Strings
Control Structures.
Conditional Execution
Logical Operators and While Loops
Topics The if Statement The if-else Statement Comparing Strings
Line Continuation, Output Formatting, and Decision Structures
ICS 3U Tuesday, September 21st.
Introduction to Programming
Python - Conditional Execution
And now for something completely different . . .
Conditional Execution
Introduction to Programming
Conditional Execution
3. Decision Structures Rocky K. C. Chang 19 September 2018
Selection Statements.
Comparing Strings Strings can be compared using the == and != operators String comparisons are case sensitive Strings can be compared using >, =, and.
Conditional and iterative statements
Logical Operators and While Loops
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Control Statements.
Introduction to Programming
Decision Structures Zelle - Chapter 7
Python Conditionals: The if statement
CMPT 120 Lecture 6 – Unit 1 – Chatbots
Control Structures.
Presentation transcript:

CHAPTER 5: Control Flow Tools (if statement) COMPUTER PROGRAMMING SKILLS 1439-1440

Outline Selection Structure Conditional operators Simple if statement If … else statement Elif statement Common Mistakes Exercises

Selection Structure Statement Description if statement if statement consists of a Boolean expression followed by one or more statements. if...else statement if statement can be followed by an optional else statement, which is executed when the Boolean expression is false. nested if...else statements (elif) You can use one if or else if statement inside another if or else if (elif) statement(s).

Conditional operators The comparison operators are ==, >, <, >=, <=, and !=. The last one is for not equals. There are three additional operators used to construct more complicated conditions: and, or, and not. Here are some examples: grade>=80 and grade<90 score>1000 or time>=20 not (score >=80)

Conditional operators Order of operations In terms of order of operations, and is done before or, so if you have a complicated condition that contains both, you may need parentheses around the or condition. Think of and as being like multiplication and or as being like addition. Examples: Suppose that: Grade=97 , x=8 , y=15 Grade >=90 and Grade<100 (is true) x>5 and y<11 (is false) x>5 or y <11 (is true) x>5 and (y!=10 or x>7) (is true) not(x>20) (is true)

Simple If statement - Overview We will start with the simple if statement, which will evaluate whether a statement is true or false, and run code only in the case that the statement is true.

Simple If statement – Syntax The syntax of a simple if statement in python is:

Simple If statement – Example Let’s look at this example where we would use conditional statements: X=5 if X == 5 : print ('is 5') print ('Still 5') X == 5 ? Yes print 'is 5' print 'Still 5' No is 5 Still 5 print 'Still 5'

Simple If statement – Example Let’s look at this example where we would use conditional statements: Suppose the passing grade on an exam is 60. The following code determines whether the condition “student’s grade is greater than or equal to 60” is true or false. If the condition is true, then “Passed” is printed, and the next statement in order is performed. If the condition is false, the printing is ignored, and the next statement in order is performed. grade =97 if grade >= 60: print('Passed') print ('End') grade =45 if grade >= 60: print('Passed') print ('End') Passed End End

if ... else statement - Overview if selection statement performs an indicated action only when the condition is true; otherwise the action is skipped. if...else selection statement allows you to specify that different actions are to be performed when the condition is true and when it is false.

if ... else statement - Syntax The syntax of an if … else statement in python is:

if ... else statement - Example Let’s look at this example where we would use if … else statements: The following code prints “Passed” or “failed” depends on the grade: If the condition is true (grade>=60), then “Passed” is printed, and the next statement (after else) in order is performed. If the condition is false (grade<60), then “Failed” is printed, and the next statement in order is performed. grade =97 if grade >= 60: print('Passed') else: print ('Failed') print ('End') grade =48 if grade >= 60: print('Passed') else: print ('Failed') print ('End') Passed End Failed End

if ... else statement - Example An other example where we would use if … else statements: x = 4 if x > 2 : print ('Bigger') else: print ('Smaller') print ('All done') x = 4 no x > 2 yes print 'Smaller' print 'Bigger' Bigger All Done print 'All Done'

Elif statement The nested if...else (elif) statement is used when the program requires more than one test expression.

Elif statement - Example Let’s look at this example where we would use if … else statements: grade =84 if grade >= 90: print('A') if grade >= 80 and grade <90: print('B') if grade >= 70 and grade <80: print('C') if grade >= 60 and grade <70: print('D') if grade < 60: print('Failed') grade =84 if grade >= 90: print('A') elif grade >= 80: print('B') elif grade >= 70: print('C') elif grade >= 60: print('D') else: print('Failed') B B

Common Mistakes Never miss!!!: Colons Indentations

Common Mistakes Mistake 1: The operator for equality consists of two equals signs Mistake 2: To use and where or is needed or vice-versa. Consider the following if statements Mistake 3:

Exercises Exercise 1: Write a program that asks the user to enter a length in centimetres. If the user enters a negative length, the program should tell the user that the entry is invalid. Otherwise, the program should convert the length to inches and print out the result. There are 2.54 centimetres in an inch. Exercise 2: Write a program that asks the user for two numbers and prints Close if the numbers are within 20 of each other and Not close otherwise.

Exercises Exercise 3: Ask the user for a temperature. Then ask them what units, Celsius or Fahrenheit, the temperature is in. Your program should convert the temperature to the other unit. The conversions are: F = 9 5 C + 32 C = 5 9 (F - 32).