If statement.  It is made up of three main components:  The keyword itself,  an expression that is tested for its truth value,  and a code suite to.

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

Selection Victor Norman CS104 Calvin College. Reading Quiz Counts toward your grade.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 4 Making Decisions in a Program.
 Control structures  Algorithm & flowchart  If statements  While statements.
Conditional Statements Introduction to Computing Science and Programming I.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
The If/Else Statement, Boolean Flags, and Menus Page 180
Chapter 4: Control Structures: Selection
Intro to Robots Conditionals and Recursion. Intro to Robots Modulus Two integer division operators - / and %. When dividing an integer by an integer we.
INTRODUCTION TO PYTHON PART 3 - LOOPS AND CONDITIONAL LOGIC CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
Chapter 9 IF Statement Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
Looping While-continue.
Python Control Flow statements There are three control flow statements in Python - if, for and while.
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.
Selection Control Structures. Simple Program Design, Fourth Edition Chapter 4 2 Objectives In this chapter you will be able to: Elaborate on the uses.
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.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
CHAPTER#3 PART1 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc.
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.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Chapter#3 Part1 Structured Program Development in C++
8. DECISION STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Decision Making CMSC 201 Chang (rev ).
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 3 Decision Trees Conditionals.
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.
3.1.3 Program Flow control Structured programming – SELECTION in greater detail.
C++ Programming Lecture 5 Control Structure I (Selection) – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Python Basics  Values, Types, Variables, Expressions  Assignments  I/O  Control Structures.
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.
 Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do.
 Type Called bool  Bool has only two possible values: True and False.
Control Flow (Python) Dr. José M. Reyes Álamo.
Control Structures I Chapter 3
The if…else Selection Statement
- Standard C Statements
Introduction to Programming
The Selection Structure
Chapter 4: Control Structures
Topics The if Statement The if-else Statement Comparing Strings
Programming Fundamentals
SELECTION STATEMENTS (1)
Topics The if Statement The if-else Statement Comparing Strings
Introduction to Programming
And now for something completely different . . .
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
Introduction to Programming
Control flow : if statements
Selection Control Structure
3. Decision Structures Rocky K. C. Chang 19 September 2018
Conditionals.
Chapter 5: Control Structure
Topics The if Statement The if-else Statement Comparing Strings
Types, Truth, and Expressions (Part 2)
CHAPTER 5: Control Flow Tools (if statement)
Control Statements.
Types, Truth, and Expressions
Introduction to Programming
Dale Roberts, Lecturer IUPUI
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
Structural Program Development: If, If-Else
Presentation transcript:

if statement

 It is made up of three main components:  The keyword itself,  an expression that is tested for its truth value,  and a code suite to execute if the  expression evaluates to non-zero or true.  The syntax for an if statement is: if expression: expr_true_suite

 The suite of the  if clause, expr_true_suite, will be executed only if the above conditional expressionresults in a Boolean true value.  Otherwise, execution resumes at the next statement following the suite.

Example: if make_hard_copy: send_data_to_printer()  Single line statements such as the above are valid syntax-wise; however, although it may be convenient, it may make your code more difficult to read, so it is recommended that you indent the suite on the next line as below. if make_hard_copy: send_data_to_printer()

if test_marks >= 50: print “Passed\n”

 Python features an else statement that can be paired with an if statement.  The else statement identifies a block of code to be executed if the conditional expression of the if statement resolves to a false Boolean value.  The syntax : if expression: expr_true_suite else: expr_false_suite

if x > y : print (“x is greater than y”) else: print (“ x is smaller than y”)

if test_marks >= 50: print “Passed\n” else: print “Failed\n”

 elif is the Python else-if statement. If the original statement is false and the elif statement is True, then the block of statements after elif will be executed.  Like the else, the elif statement is optional  You may specify as many elif headers as you need. Take note that each elif header also requires a condition.

if expression1: expr1_true_suite elif expression2: expr2_true_suite : elif expressionN: exprN_true_suite else: none_of_the_above_suite

test_marks = input(“Enter marks: ”) if test_marks >= 80: grade = “A” elif test_marks >= 70: grade = “B” elif test_marks >= 60: grade = “C” elif test_marks >= 50: grade = “D” else: grade = “F” print “Grade: ”, grade …

 The Boolean operators  and,  or,  and not can be used to provide multiple conditional expressions or  perform negation of expressions in the same if statement. example: if not warn and (system_load >= 10): print "WARNING: losing resources”

 An employee earns RM 5 per hour for the first 30 hours and RM 10 per hour for each hour over 30. Write a program that able to compute an employee’s weekly wage. Example output: 33 hours wage RM 180 Provide your solution with flowchart. Transform the flowchart to Python code.

 Shirts are on sale for RM 4 each if more than two are purchased and RM 6 each otherwise. Write a program that able to read in an input number of shirts purchased and display the total cost. Analyze the problem: Identify the input, output and formula. Design your solution with a pseudocode and flowchart. Transform the design to Python code.

 Write a program that reads an integer and determines and prints whether it is odd or even. (Hint: use the modulus operator. An even number is a multiple of two. Any multiple of two leaves a remainder of zero when divided by 2. Analyze the problem: Identify the input, output and formula. Design your solution with a pseudocode and flowchart. Transform the design to Python code.