Flow Control Presented by: Md Fashiar Rahman.

Slides:



Advertisements
Similar presentations
What type of data can a variable hold?
Advertisements

Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 8 Fruitful Functions 05/02/09 Python Mini-Course: Day 2 - Lesson 8 1.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Structured programming
Loops – While, Do, For Repetition Statements Introduction to Arrays
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 3: Control Structures (Part 1) – Exercises 1 Xiang Lian The University of Texas – Pan American Edinburg,
Geography 465 Assignments, Conditionals, and Loops.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
The switch Statement, DecimalFormat, and Introduction to Looping
Computer Science 111 Fundamentals of Programming I Iteration with the for Loop.
Python Control of Flow.
Python quick start guide
The University of Texas – Pan American
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
More Looping Structures
Python Control Flow statements There are three control flow statements in Python - if, for and while.
Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
If statements while loop for loop
Xin Liu Feb 11, * Part 1 1. What value does s have, after this code is run? s = '*' s = s + s s = s + s + s (A) '**' (B) '***' (C) '****' (D) '*****'
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
Xin Liu Feb 4, * Use midterm questions for previous 231/217 midterms for practices * ams
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.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Last Week Modules Save functions to a file, e.g., filename.py The file filename.py is a module We can use the functions in filename.py by importing it.
Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import.
1 CS 177 Week 6 Recitation Slides Review for Midterm Exam.
September 7, 2004ICP: Chapter 3: Control Structures1 Introduction to Computer Programming Chapter 3: Control Structures Michael Scherger Department of.
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
Control Flow Statements
CSI 3125, Preliminaries, page 1 Control Statements.
Python Basics  Functions  Loops  Recursion. Built-in functions >>> type (32) >>> int(‘32’) 32  From math >>>import math >>> degrees = 45 >>> radians.
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.
COMP Loop Statements Yi Hong May 21, 2015.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1.
Python Flow of Control CS 4320, SPRING Iteration The ‘for’ loop is good for stepping through lists The code below will print each element in the.
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
INTRO2CS Tirgul 3 1. What we will cover today?  Boolean and logical expressions  Truth values  Conditional statements  while loop  for loop  range.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
PH2150 Scientific Computing Skills Control Structures in Python In general, statements are executed sequentially, top to bottom. There are many instances.
More about Iteration Victor Norman CS104. Reading Quiz.
G. Pullaiah College of Engineering and Technology
The switch Statement, and Introduction to Looping
Python: Control Structures
CiS 260: App Dev I Chapter 4: Control Structures II.
Outline Altering flow of control Boolean expressions
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
3 Control Statements:.
Chapter 3 – Control Structures
Repetition Control Structure
3.5- The while Statement The while statement has the following syntax:
Suggested Layout ** Designed to be printed on white A3 paper.
Nate Brunelle Today: Conditional Decision Statements
CHAPTER 6: Control Flow Tools (for and while loops)
Nate Brunelle Today: Conditional Decision Statements
COMPUTER PROGRAMMING SKILLS
CSE 231 Lab 2.
Nate Brunelle Today: Conditional Decision Statements
LOOP Basics.
Introduction to Python
Presentation transcript:

Flow Control Presented by: Md Fashiar Rahman

Arithmetic/Logic Unit If…else statement if if True: print('Hello world') Statement 1 Statement 2 Statement 3 if False: print('Hello world') Control Unit Arithmetic/Logic Unit if False: print('Hello world') print('Bye') Statement 7 Statement 8 Statement 9 Statement 4 Statement 5 Statement 6 Memory Unit if False: print('Hello world') print('Bye') How we get True or False?

Evaluate from an expression If…else statement How we get True or False? Evaluate from an expression x = 3 r = x % 2 if r == 0: print('Even') print('Bye') x = 3 r = x % 2 if r == 0: print('Even') if r == 1: print('Odd') print('Bye') x = 8 r = x % 2 if r == 0: print('Even') else: print('Odd') print('Bye') If test expression: Statement (s) If in if ?

If…else statement Nested if if…elif…else x = 8 r = x % 2 if r == 0: print('Even') if x > 5: print('Great') else: print('Not so great') print('Odd') print(‘Bye') x = 4 if x == 1: print('One') if x == 2: print('Two') if x == 3: print('Three') if x == 4: print('Four') x = 1 if x == 1: print('One') elif x == 2: print('Two') elif x == 3: print('Three') elif x == 4: print('Four') x = 5 if x == 1: print('One') elif x == 2: print('Two') elif x == 3: print('Three') elif x == 4: print('Four') else: print(‘Wrong Input')

While loop While Or For 100 times 1000 times print('Hello world') Initialization Initialization Condition i = 0 while i < 5: print('Hello world') i = i + 1 Condition i = 5 while i > 0: print('Hello world') i = i - 1 Increment Decreament

While loop Nested while loop i = 1 j = 1 while i <= 5: print('Hello world:‘ , end=“ “) while j <=3: print('Seminar‘ , end = “ ”) j = j + 1 i = i + 1 print() i = 1 j = 1 while i <= 5: print('Hello world:', end = " ") while j <=3: print('Seminar', end = " ") j = j + 1 i = i + 1 print() Hello world: Seminar Seminar Seminar

For loop for val in sequence: Body of for for i in range(1,11): if i % 5 != 0: print(i) for i in range(11,21,2): print(i) for i in range(21,10,-2): print(i) x = ['pop', 'rock', 'jazz'] I like pop I like rock I like jazz x = ['pop', 'rock', 'jazz'] for i in x: print("I like", i) x = ['pop', 'rock', 'jazz'] for i in range(len(x)): print("I like", x[i])

For loop # # # # for j in range(4): print("# ", end = "") for i in range(4): for j in range(4): print("# ", end = "") print() # # # # # # # #

For loop for i in range(4): for j in range(i): print("# ", end = "") # # # # # # # # # # # # # # # # # # # # # # # # #

Break, Continue & Pass 1 3 5 7 9 11 13 15 17 19 Bye for i in range(5): if i == 3: break print("Hello", i) print(‘Bye’) for i in range(5): if i == 3: continue print("Hello", i) print(‘Bye’) Hello 0 Hello 1 Hello 2 Hello 4 Bye for i in range(1,21): if i % 2 == 0: pass else: print(i) print('Bye') Hello 0 Hello 1 Hello 2 Bye for i in range(1,11): if i % 3 == 0 or i % 5 == 0: continue print(i) print('Bye') def fun(): pass Class Student: pass

Quiz for i in [1, 0]: print(i + 1) 2 1 [2, 1] 2 [2, 0]

Quiz i = sum = 0 while i <= 4: sum += i i = i + 1 10 print(sum) 4 10 4 None of the above

Quiz 4 is printed once while 4 == 4: print('4') 4 is printed four times 4 is printed infinitely until program closes Syntax error

Quiz for char in 'PAYTHON STRING': if char == ' ': break print(char, end = '') if char == 'O': continue PYTHON for char in 'PAYTHON STRING': if char == ' ': break if char == 'O': continue print(char, end = '') PYTHONSTRING PYTHN STRING

Quiz for i in range(1,11): if i % 2 == 0 and i % 3 == 0: continue print(i) print('Bye') 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 7 8 9 10 1 5 7 9 1 4 5 7 8 9 10

Quiz for i in range(4): for j in range(i+1,5): print(j , end = " ") 1 2 3 4 1 2 3 4 2 3 4 3 4 4 1 2 3 4 1 2 3 1 2 1 0 1 2 3 4 5

Any Question ??