Control Structures FOR Statement Looping.

Slides:



Advertisements
Similar presentations
Nested Loops and the Break Statement. What are Nested Loops? Nested loops are: Loops which run inside another loop When would you use nested loops? Performing.
Advertisements

Week 5 - Friday.  What did we talk about last time?  Repetition  while loops.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
Main task -write me a program
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Chapter 4: Looping CSCI-UA 0002 – Introduction to Computer Programming Mr. Joel Kemp.
Fundamentals of Python: From First Programs Through Data Structures
Fundamentals of Python: First Programs
Noadswood Science,  To know the basics of Python coding and decoding Monday, September 07, 2015.
Hands on Projects Dr. Bernard Chen Ph.D. University of Central Arkansas July 9 th 2012
COIT29222 Structured Programming Slide 1 COIT29222-Structured Programming Lecture Week 06  Reading: Study Guide Book 2, Modules 9 & 10 Textbook (4 th.
Module 3 Fraser High School. Module 3 – Loops and Booleans import statements - random use the random.randint() function use while loop write conditional.
If statements while loop for loop
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.
Python Programming Using Variables and input. Objectives We’re learning to build functions and to use inputs and outputs. Outcomes Build a function Use.
CPS120: Introduction to Computer Science Decision Making in Programs.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
PROGRAMMING In Lesson 2. STARTER ACTIVITY Complete the starter activity in your python folder – lesson 2 Now we will see how you got on and update your.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Course A201: Introduction to Programming 09/16/2010.
Chapter 3: Branching and Program Flow CSCI-UA 0002 – Introduction to Computer Programming Mr. Joel Kemp.
Fundamental Programming Fundamental Programming for Loops.
Introduction to Loops For Loops. Motivation for Using Loops So far, everything we’ve done in MATLAB, you could probably do by hand: Mathematical operations.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Decision Structures, String Comparison, Nested Structures
Count Controlled Loops Look at the little children … Why is the sun’s face features orange …
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
Iteration. Iteration: Review  If you wanted to display all the numbers from 1 to 1000, you wouldn’t want to do this, would you? Start display 1 display.
GCSE Computing: Programming GCSE Programming Remembering Python.
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
Python Basics  Functions  Loops  Recursion. Built-in functions >>> type (32) >>> int(‘32’) 32  From math >>>import math >>> degrees = 45 >>> radians.
9. ITERATIONS AND LOOP STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Control Structures WHILE Statement Looping. S E Q C E N U E REPITITION …a step or sequence of steps that are repeated until some condition is satisfied.
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
Loops Brent M. Dingle Texas A&M University Chapter 6 – Section 6.3 Multiway Branches (and some from Mastering Turbo Pascal 5.5, 3 rd Edition by Tom Swan)
More about Iteration Victor Norman CS104. Reading Quiz.
Lab 4: Loops and Iteration Graham Northup
Week 4 Computer Programming Gray , Calibri 24
Loops BIS1523 – Lecture 10.
Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will.
CHAPTER 5A Loop Structure
Chapter 5: Repetition Structures
Repeating code We could repeat code we need more than once: i = 1 print (i) i += 1 print (i) #… stop when i == 9 But each line means an extra line we might.
Iterations Programming Condition Controlled Loops (WHILE Loop)
While Loops (Iteration 2)
Writing Functions( ) (Part 5)
Writing Functions( ) (Part 5)
For -G7 programing language Teacher / Shamsa Hassan Alhassouni.
Python Primer 2: Functions and Control Flow
For -G7 programing language Teacher / Shamsa Hassan Alhassouni.
Count Controlled Loops
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Loops CIS 40 – Introduction to Programming in Python
IST256 : Applications Programming for Information Systems
Week 4 Computer Programming Year 9 – Unit 9.04
Python programming exercise
For Loops (Iteration 1) Programming Guides.
CHAPTER 6: Control Flow Tools (for and while loops)
Iteration – While Loops
GCSE Computing.
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Introduction to Python
Presentation transcript:

Control Structures FOR Statement Looping

S E Q U REPITITION …or Iteration E N …a step or sequence of steps that are repeated until some condition is satisfied. C E

Sometimes we want to repeat a bit of code many times Sometimes we want to repeat a bit of code many times. We use loops to achieve this! For example, if you wanted to output the same statement 3 times, there are 2 ways we could do this. METHOD 1 print(‘Hello World’)

WHILE LOOP FOR LOOP Method 2: Use a Loop In Python, there are 2 looping mechanisms… WHILE LOOP We will study later FOR LOOP LOOPS for a specified number of times! For example, output HELLOW WORLD 3 times

>>> for x in range (0,3): print('Hello World') The Range Function is useful here as it enables us to specify a start value and an end value. Range ( <start>, <stop>) ((Note: start is optional, otherwise it starts at 0)) Pay careful attention to the SYNTAX. Remember the amount of compile issues you had with the colon in our IF…ELSE statement ???...well, note that the FOR statement uses one too.

>>> for x in range (1,5): print('Hello World') 4 times (it STOPS at the 5th count) How many times will Hello World be output? Every time a loop is made, we call this an iteration. There were 4 iterations in the command above. Try entering the following into your interpreter: >>> for x in range (5000): print(x)

Look at the following examples together: What will be the output of the following? 1. 2. 3.

When learning looping, it’s useful to count the number of iterations (loops). As this FOR loop iterates, the variable ‘x’ increments by 1. We can simply output the value of ‘x’ for each iteration! For example, For x in range(5): print(‘This is iteration number: ‘, x) This will output… This is iteration number: 0 This is iteration number: 1 This is iteration number: 2 This is iteration number: 3 This is iteration number: 4

What do you think the output will be for this block? Sometimes, we can put a loop inside a another loop! WOW! These are known as Nested Loops. Things seem to start getting a little complicated, but the mechanism is exactly the same. for x in range(1,3): print(‘This is OUTER loop statement ’, x) for y in range(1,4): print(‘This is INNTER loop statement - - ‘,y) What do you think the output will be for this block? This FOR loop will iterate (1,3) i.e. twice This FOR loop will iterate (1,4) i.e. thrice

Look at the following source code. for x in range(1,4): print('This is OUTER loop number: ', x) for y in range(1,3): print('This is INNER loop number: ',x,' , ',y) On a separate sheet of paper (file paper will do) write down the output expected from Python for this statement. Be exact as you can in your answer. (Remember that range(1,4) means ‘starting at 1 and stop at 4’ …giving you 3 iterations)

Look at the following source code. for y in range(5): print(y * '*') On your same separate sheet of paper (file paper will do) write down the output expected from Python for this statement. Be exact as you can in your answer.

for y in range(5): print(y * '*') for y in range(5,0,-1): Look carefully at the following source code. for y in range(5): print(y * '*') for y in range(5,0,-1): Explain to your teacher what you think is going to be output here… Tip: in the second block of code, note the starting value of ‘y’! This means, decrement by 1 after each iteration.

Try this for fun – change the number of iterations to 50 for both ranges.

Try this for fun! ((Double-space used here and in the 1st FOR loop also))

The break statement Sometimes you might want to stop iterating through a loop. We can place a break statement where we want our programs to terminate.

TASK Guess the secret number! Prompt the user for their name. Ask them to guess a number between 1 to 10. Give them 5 attempts to guess the correct number! REFINEMENT 1: Improve your program by stopping the iterations if they guess correctly (You will need to use a break statement) REFINEMENT 2: Improve your program by giving them their total guess count along with their name. For example, Well done Jude! You guessed correctly! Unfortunately it took you [guess_count] guesses!

my_number = 7 their_guess = 8 for x in range(1,5): their_guess = int(input('Please enter a guess between 1 and 10')) if their_guess == my_number: print('Well done') break else: print('Hard luck')

from random import randint Using the random number generator… We can ‘import’ very useful ‘modules’ which can give extra functionality to our programs. One such module is the ‘random’ module. This is how we import it…. from random import randint …then we can ask it to generate a random number for us between 1 and 100… from random import randint randint(1,100)

Save this as ‘Guess Random Secret Number’. Using the random number generator… Copy your ‘Guess Secret Number’ game so that it uses the randomiser to pick a random number between 1 and 10, rather than you setting it at design time. Save this as ‘Guess Random Secret Number’.