Selection Statement ( if )

Similar documents
Lab1. Introduction to Python. Lab 4: Selection Statement. Eng. Mai Z. Alyazji

Variable and Data Type I

Variable and Data Type I

Variable and Data Type 2

Introduction to Python

Computer Programming: C++

Loops and Conditionals. HORT Lecture 11 Instructor: Kranthi Varala

Flow Control. So Far: Writing simple statements that get executed one after another.

Lecture 3 (02/06, 02/08): Condition Statements Decision, Operations & Information Technologies Robert H. Smith School of Business Spring, 2017

Hacettepe University Computer Engineering Department. Programming in. BBM103 Introduction to Programming Lab 1 Week 4. Fall 2018

Chapter 5 Conditional and Iterative Statements. Statement are the instructions given to the computer to perform any kind of action.

Conditional Expressions and Decision Statements

Chapter 7: Programming in MATLAB

Algorithms and Data Structures

Computer Programming: C++

Python review. 1 Python basics. References. CS 234 Naomi Nishimura

Eng. Mohammed S. Abdualal

Assembly Language Lab # 4 Data Transfer & Arithmetic (1)

CS100: CPADS. Decisions. David Babcock / Don Hake Department of Physical Sciences York College of Pennsylvania

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 4111: Digital Systems Design Lab. Lab # 5. Concurrent Statements

GE PROBLEM SOVING AND PYTHON PROGRAMMING. Question Bank UNIT 1 - ALGORITHMIC PROBLEM SOLVING

Flow of Control Branching 2. Cheng, Wei COMP May 19, Title

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

Object Oriented Programming Lecture (2.1) Supervisor Ebtsam AbdelHakam Department of Computer Science Najran University

Lab # 6. Using Subqueries and Set Operators. Eng. Alaa O Shama

Procedural Language Structured Query Language (PL/SQL)

Computer Programming: C++

Assembly Language LAB

Assembly Language Lab # 6

Python Boot Camp. Day 3

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

SQL Retrieving Data from Multiple Tables

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

Decision Making in C

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Basic Python Revision Notes With help from Nitish Mittal

cs1114 REVIEW of details test closed laptop period

Control flow statements

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Lab # 6. Data Manipulation Language (DML)

Algorithms and Data Structures

Relational & Logical Operators, Selection Statements

CSE 115. Introduction to Computer Science I

CHAPTER 2: Introduction to Python COMPUTER PROGRAMMING SKILLS

Computer Programming ECIV 2303 Chapter 6 Programming in MATLAB Instructor: Dr. Talal Skaik Islamic University of Gaza Faculty of Engineering

CSCE 110: Programming I

Python The way of a program. Srinidhi H Asst Professor Dept of CSE, MSRIT

Lecture 02 Making Decisions: Conditional Execution

Computer Programing. for Physicists [SCPY204] Class 02: 25 Jan 2018

Selection statements. CSE 1310 Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington

ICS LAB 5 (C# - C sharp)

Python Activity 5: Boolean Expressions and Selection Statements

Unit 3. Operators. School of Science and Technology INTRODUCTION

Arrays. Eng. Mohammed Abdualal

1 Truth. 2 Conditional Statements. Expressions That Can Evaluate to Boolean Values. Williams College Lecture 4 Brent Heeringa, Bill Jannen

Here n is a variable name. The value of that variable is 176.

Loops / Repetition Statements

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

Loops. Eng. Mohammed Abdualal. Islamic University of Gaza. Faculty of Engineering. Computer Engineering Department

LAB 4.1 Relational Operators and the if Statement

1. What is the minimum number of bits needed to store a single piece of data representing: a. An integer between 0 and 100?

PRG PROGRAMMING ESSENTIALS. Lecture 2 Program flow, Conditionals, Loops

LAB 5: SELECTION STATEMENTS

Comp 151. Control structures.

CMSC201 Computer Science I for Majors

Lecture 10: Boolean Expressions

egrapher Language Reference Manual

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

TUPLES AND RECURSIVE LISTS 5

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

ENGR 102 Engineering Lab I - Computation

CS 112: Intro to Comp Prog

To begin this textbook, we need to start with a refresher of the topics of numbers and numbering systems.

Module 3: New types of data

Faculty of Engineering Computer Engineering Department Islamic University of Gaza Network Lab # 5 Managing Groups

Introduction to Programming

Text Input and Conditionals

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Downloaded from Chapter 2. Functions

Computer Programming, I. Laboratory Manual. Experiment #3. Selections

Boolean Logic & Branching Lab Conditional Tests

Python for Informatics

Any Integer Can Be Written as a Fraction

THE IF STATEMENT. The if statement is used to check a condition: if the condition is true, we run a block

Fundamentals of Programming (Python) Control Structures. Sina Sajadmanesh Sharif University of Technology Fall 2017

Decision Making -Branching. Class Incharge: S. Sasirekha

Types, lists & functions

SE 3S03. Zahra Ali. Week of Feb 22, 2016

9/18/11. Type casting, logical operators and if/else statement. Explicit Type Conversion. Example of Type Casting

Conditionals: Making Choices

Intermediate Representation (IR)

Overview: Programming Concepts. Programming Concepts. Names, Values, And Variables

Overview: Programming Concepts. Programming Concepts. Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript

Introduction to Computers. Laboratory Manual. Experiment #3. Elementary Programming, II

Introduction to Bioinformatics

Algorithms and Conditionals

XQ: An XML Query Language Language Reference Manual

There are four numeric types: 1. Integers, represented as a 32 bit (or longer) quantity. Digits sequences (possibly) signed are integer literals:

Transcription:

Islamic University Of Gaza Faculty of Engineering Computer Engineering Department Lab 4 Selection Statement ( if ) Eng. Ibraheem Lubbad October 10, 2016

In this lab we will constructs program that allow statements to be optionally executed, depending on the selection execution. Boolean Data Type and Operations: The python values True and False are special inbuilt objects which is Boolean type. You can give variable the value directly or via Boolean expressions. Boolean expression: an expression that computes to True or false. It may contain relational (Comparison) operators and logical operators, the simplest Boolean expressions in Python are True and False. Comparison Operators: Operator Description == Equal to!= Not equal to <> Not equal to > Greater than < Less than >= Greater than or equal to <= Less than or equal to Logical Operators: We combined more complex Boolean expressions using the logical operators and, or, and not. Operator and or not Description If both the operands are true then condition becomes true If any of the two operands are non-zero then condition becomes true Used to reverse the logical state Logical operators e1 and e2 are Boolean expressions e1 e2 e1 and e2 e1 or e2 not e1 True True True True False True False False True False False True False True True False False False False True Membership Operators: Python s membership operators test for membership in a sequence, such as strings, lists, Tuples or dictionary. For example list= [2, 6, 9, 7] Operator Description Example in Check element belong to sequence 5 in list------ False not in Check element not belong to sequence 5 not in list---true

Decision structures evaluate multiple expressions which produce True or False as outcome. You need to determine which action to take and which statements to execute if outcome is true or false. Following is the general form of a typical decision making structure: If Statement: if Boolean Expression: statements Syntax of if statement Condition False True Statements Example 1 a = 24 if a > 0: print "a is positive "

If else Statement: if Boolean Expression: statements(1) statements(2) Syntax of if / else statement Condition False True Statements(1) Statements(2) Example 2 a = 24 if a > 0: print "a is positive " print "a is non - positive"

The elif Statement: The elif statement allows you to check multiple expressions for True and execute a block of code as soon as one of the conditions evaluates to True. if expression1 : Syntax of elif statement statements(1) elif expression2: statements(2) statements(3) Example 3: a = 24 if a == 0: print "a is zero " elif a < 0: print "a is negative " print "a is positive " If a=24 a=0 a=-3

Single Statement: If the suite of an if statement consists only of a single line, it may go on the same line. Here is an example of a one-line if statement: Example 4: Single statement var = 100 if var == 100 : print "Value of expression is 100" print "Done!" Nested Conditionals: The statements in the block of the if or the else may be any Python statements, including other if/else statements. Determines if a number is between 0 and 10, inclusive value = input("please enter an integer value in the range 0...10:") if value >= 0: # First check if value <= 10: # Second check print "In range" print "Out of range" print("done") The program can be rewritten to behave the same way with only one if statement: Another way value = input("please enter an integer value in the range 0...10:") if value >= 0 and value <= 10: # Only one print("in range") else: print "Out of range" print("done")

Conditional Expressions: Python provides an alternative to the if/else construct called a conditional expression. A conditional expression evaluates to one of two values depending on a Boolean condition The general form of the conditional expression is: expression1 if condition else expression2 For example here variable c is assigned one of two possible values: if a!= b: c = d c = e If/else example Rewritten as c = d if a!= b else e The form of the conditional expression For Example: write a program to compute the absolute value of entered number from user. Example 5: n = input ("Enter a number: ") absvalue=(-n if n < 0 else n) print ' ', n, ' = ', absvalue Output:

Lab work: Example 6: check number is even or odd n = input ("Enter a number: ") if n%2==0 : print "Even number" print "Odd number" print "Done" Output: Example 7: Check entered number in the range value = input("please enter an integer in the range 0...5: ") if value < 0: print "Too small" elif value == 0: print "zero" elif value == 1: print "one" elif value == 2: print "two" elif value == 3: print "three" elif value == 4: print "four" elif value == 5: print "five" else: print "Too large" print "Done" Output:

Q) Write a program that requests from user to enter an integer and checks whether the number is divisible by both 5 and 6, or neither of them, or just one of them. For example, here are some sample runs for inputs 10, 30, and 23: 10 is divisible by 5 or 6, but not both 30 is divisible by both 5 and 6 23 is not divisible by either 5 or 6 Sol: Solution num=input("enter number : ") if num%5==0 and num%6==0: print num,"is divisible by both 5 and 6" elif num%5==0 or num%6==0: print num, "is divisible by 5 or 6, but not both " else: print num,"is not divisible by either 5 or 6" Output:

Exercises: 1) Write a Python program that requests three integer values from the user. It then prints one of two things: if any of the values entered are duplicates, it prints "DUPLICATES"; otherwise, it prints "ALL UNIQUE". 2) Write a Python program that use an if statement to find the smallest of three given integers without using the min function 3) Write a Python program that sorts three integers. The integers are entered from user using input function. For example if the user enters: 7, 3, 5 then the output should be 3<=5 <=7. 6, 2, 6 then the output should be 2<=6 <=6