Control Statements.

Slides:



Advertisements
Similar presentations
Conditional Execution
Advertisements

Logic & program control part 3: Compound selection structures.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
School of Computing Science CMT1000 Ed Currie © Middlesex University 1 CMT1000: Introduction to Programming Ed Currie Lecture 5B: Branch Statements - Making.
Multi-alternative Selection Both the if clause and the else clause of an if...else statement can contain any kind of statement, including another selection.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 4: Conditional Execution.
INTRODUCTION TO PYTHON PART 3 - LOOPS AND CONDITIONAL LOGIC CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
Lab 8 Shell Script Reference:
Control Structures I (Selection)
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 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
1 Advanced Programming IF. 2 Control Structures Program of control –Program performs one statement then goes to next line Sequential execution –Different.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
Lecture 4: C/C++ Control Structures Computer Programming Control Structures Lecture No. 4.
CHAPTER#3 PART1 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc.
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.
Shell Script2 Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
Chapter#3 Part1 Structured Program Development in C++
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Copyright 2008 by Pearson Education Building Java Programs Chapter 4 Lecture 4-1: if and if/else Statements reading: 4.2 self-check: #4-5, 7, 10, 11 exercises:
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.
Shell script – part 2 CS 302. Special shell variable $0.. $9  Positional parameters or command line arguments  For example, a script myscript take 2.
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.
1 Advanced Programming IF. 2 Control Structures Program of control –Program performs one statement then goes to next line Sequential execution –Different.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101.
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 Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
C++ Programming Control Structures I (Selection).
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.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C 1 A.AlOsaimi King Saud University College of Applied studies and Community Service Csc 1101.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
Decision making If.. else statement.
Introduction to Decision Structures and Boolean Variables
Control Flow (Python) Dr. José M. Reyes Álamo.
Branching statements.
Chapter 4: Control Structures I
The if…else Selection Statement
Building Java Programs
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Chapter 4: Control Structures
The order in which statements are executed is called the flow of control. Most of the time, a running program starts at the first programming statement,
Lecture 3 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz
SELECTION STATEMENTS (1)
Selection By Ramin && Taimoor
Conditional Execution
Control Statement Examples
Arithmetic operations, decisions and looping
Python - Conditional Execution
And now for something completely different . . .
Building Java Programs
Executes a block of statements only if a test is true
Conditional Execution
3 Control Statements:.
Conditional Execution
Decision making If statement.
3. Decision Structures Rocky K. C. Chang 19 September 2018
Visual Basic – Decision Statements
Chapter 5: Control Structure
Topics The if Statement The if-else Statement Comparing Strings
Building Java Programs
CS2011 Introduction to Programming I Selections (I)
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
CHAPTER 5: Control Flow Tools (if statement)
Building Java Programs
Building Java Programs Chapter 4
Decision Structures Zelle - Chapter 7
REPETITION Why Repetition?
Agenda Warmup Review Finish 1.2 Assignments Lesson 1.3 (If Statements)
Presentation transcript:

Control Statements

4 Major Versions of Python “Python” or “CPython” is written in C/C++ - Version 2.7 came out in mid-2010 - Version 3.1.2 came out in early 2010 “Jython” is written in Java for the JVM “IronPython” is written in C# for the .Net environment Go To Website

Conditional Steps Yes Program: x = 5 if x < 10: print 'Smaller‘ print 'Bigger' print 'Finis' print 'Smaller' X > 20 ? Yes print 'Bigger' print 'Finis'

if if statement: Executes a set of commands only if a certain condition is True. Otherwise, the commands are skipped. Syntax: if condition: statements Example: gpa = input("What is your GPA? ") if gpa > 2.0: print "Your application is accepted."

Range Test if (3 <= Time <= 5): print “Office Hour"

The IF Statement Yes No x = 5 if x == 5 : print 'Is 5‘ print 'Is Still 5' print 'Third 5’ print 'Still 5' print 'Third 5' The IF Statement

Two-way branch using else : X = 4 x = 4 if x > 2 : print 'Bigger' else : print 'Smaller' print 'All done' no x > 2 yes print 'Smaller' print 'Bigger' print 'All Done'

if/else if/else statement: Executes one set of statements if a certain condition is True, and a second set if it is False. Syntax: if condition: statements else: Example: gpa = input("What is your GPA? ") if gpa > 2.0: print "Welcome to Mars University!" print "Your application is denied." Multiple conditions can be chained with elif

greater than or equal to Logic Logical expressions can be combined using logical operators: Operator Meaning Example Result == equals 1 + 1 == 2 True != does not equal 3.2 != 2.5 < less than 10 < 5 False > greater than 10 > 5 <= less than or equal to 126 <= 100 >= greater than or equal to 5.0 >= 5.0 Operator Example Result and (9 != 6) and (2 < 3) True or (2 == 3) or (-1 < 5) not not (7 > 0) False

Nested Decisions x = 42 if x > 1 : print 'More than one' yes print 'More than one' no x = 42 if x > 1 : print 'More than one' if x < 100 : print 'Less than 100' print 'All done' x < 100 yes no print 'Less than 100' print 'All Done'

Chained Conditional if x < 2 : print 'Small' elif x < 10 : print 'Medium' elif x < 20 : print 'Big' elif x< 40 : print 'Large' elif x < 100: print 'Huge' else : print 'Ginormous' # No Else x = 5 if x < 2 : print 'Small' elif x < 10 : print 'Medium' print 'All done'

Exercise Write a pay computation program that gives the employee 1.5 times the hourly rate for hours worked above 40 hours (and regular 1.0 rate for less than 40 hours) Enter Hours: 45 Enter Rate: 10 Pay: 475.0

Example Your city classifies a pollution index less than 35 as “Pleasant”, 35 through 60 as “Unpleasant”, and above 60 as “Health Hazard.” Display the correct description of the pollution index value.

Example Display one word to describe the integer value of number as “Positive”, “Negative”, or “Zero”

Example Write a program to ask a student for his grades in 3 exams ( each out of 50 ) , get their total and inform the student whether he passed or failed the course.