Iterations Programming Condition Controlled Loops (WHILE Loop)

Slides:



Advertisements
Similar presentations
CS0004: Introduction to Programming Repetition – Do Loops.
Advertisements

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
True BASIC Ch. 6 Practice Questions. What is the output? PRINT X LET X = -1 PRINT X FOR X = 4 TO 5 STEP 2 PRINT X NEXT X PRINT X END.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
PROGRAMMING In Lesson 5. RECAP  Complete the starter activity.
Control Structures FOR Statement Looping.
While Loops Indefinite Iteration. Last lesson we looked at definite loops using the ‘For’ statement. The while loop keeps going while some condition is.
By the end of this session you should be able to...
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
For loops in programming Assumes you have seen assignment statements and print statements.
More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:
CS 100 Introduction to Computing Seminar October 7, 2015.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
GCSE Computing: Programming GCSE Programming Remembering Python.
Intro to Loops 1.General Knowledge 2.Two Types of Loops 3.The WHILE loop 1.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
BO65: PROGRAMMING ITERATION 1. Starter  Write down what you think the code below outputs on screen: Dim intCounter as integer intCounter = 0 Do while.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Microsoft® Small Basic Conditions and Loops Estimated time to complete this lesson: 2 hours.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
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.
Controlling Program Structures. Big Picture We are learning how to use structures to control the flow of our programs Last week we looked at If statements.
Loops ( while and for ) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan 1.
Loop. 3.1Introduction to Repetition Structures Loop – a block of code that, under certain conditions will be executed repeatedly. Do Prompt for and input.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
Infinite for Loop If you omit the test condition, the value is assumed to be TRUE so the loop will continue indefinitely unless you provide some other.
Lesson #5 Repetition and Loops.
Whatcha doin'? Aims: To start using Python. To understand loops.
Control Flow Constructs: Conditional Logic
Control Structures II Chapter 3
Repetition (While-Loop) version]
CS161 Introduction to Computer Science
Lesson #5 Repetition and Loops.
Lesson 05: Iterations Class Chat: Attendance: Participation
Think What will be the output?
While Loops (Iteration 2)
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Chapter 4 Control structures and Loops
For -G7 programing language Teacher / Shamsa Hassan Alhassouni.
CPS120: Introduction to Computer Science
Lesson #5 Repetition and Loops.
Lesson 05: Iterations Topic: Introduction to Programming, Zybook Ch 4, P4E Ch 5. Slides on website.
For -G7 programing language Teacher / Shamsa Hassan Alhassouni.
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Looping and Random Numbers
Iteration: Beyond the Basic PERFORM
IPC144 Introduction to Programming Using C Week 3 – Lesson 1
Loops CIS 40 – Introduction to Programming in Python
Algorithms Take a look at the worksheet. What do we already know, and what will we have to learn in this term?
Coding Concepts (Basics)
IPC144 Introduction to Programming Using C Week 3 – Lesson 2
Repetition In today’s lesson we will look at:
3.1 Iteration Loops For … To … Next 18/01/2019.
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
Conditional and iterative statements
Week 6 CPS125.
Computer Science Core Concepts
M150: Data, Computing and Information
Looping III (do … while statement)
For Loops (Iteration 1) Programming Guides.
CprE 185: Intro to Problem Solving (using C)
A LESSON IN LOOPING What is a loop?
CHAPTER 6: Control Flow Tools (for and while loops)
CPS125 Week
Java Programming Loops
Lesson #5 Repetition and Loops.
CprE 185: Intro to Problem Solving (using C)
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

Iterations Programming Condition Controlled Loops (WHILE Loop)

Iterations – understanding WHILE loops Learning Objectives: To understand… …the use of the three basic programming constructs used to control the flow of a program: sequence selection iteration (count and condition controlled loops) Introduction We were introduced to iterations last lesson and saw how we could program a count controlled loop in python (FOR Loop). Today we shall look at another type of iteration. While loops will repeated whilst a certain condition is true. It is therefore known as a condition control loop. We shall now look to see how these can be programmed in Python.

While Loops in Python Learning Objectives: To understand… …the use of the three basic programming constructs used to control the flow of a program: sequence selection iteration (count and condition controlled loops) ‘While Loops’ in Python While Loops are set up using the following statement: while x 0: Lets take a look more closely… Start X = 0 <> >= <= == > < != Execute Command These are called conditions Does ‘x’ = 5? No Yes

While Loops in Python ‘While Loops’ in Python while x n: Learning Objectives: To understand… …the use of the three basic programming constructs used to control the flow of a program: sequence selection iteration (count and condition controlled loops) ‘While Loops’ in Python while x n: The x is simply a variable. It could have any name. It is however a special kind of variable known as the ‘most recent value’ We must finish the statement with a colon == != > < Start X = 0 Execute Command The n is represents a value that we want x to either equal, not equal, be greater than, etc. depending on the condition we want to use. E.g. n=5 and the condition while x != 5 (not equal to 5) then the loop would repeat until x equals 5. Does ‘x’ = 5? No Yes

While Loops in Python Learning Objectives: To understand… …the use of the three basic programming constructs used to control the flow of a program: sequence selection iteration (count and condition controlled loops) ‘While Loops’ in Python x = 0 while x == 0: print(“Hello World”) Hello World Hello World Hello World Hello World Hello World Hello World Hello World

While Loops in Python ‘While Loops’ in Python x = 0 while x != 5: Learning Objectives: To understand… …the use of the three basic programming constructs used to control the flow of a program: sequence selection iteration (count and condition controlled loops) ‘While Loops’ in Python x = 0 while x != 5: x = int(input(“Please type in a number”)) print(“Loop has ended”) Please type in a number: 1 Please type in a number: -5 Please type in a number: 122 Remember: If you create a condition where a variable is being checked against an integer, you must remember to convert the variable’s input into an integer (e.g. int()) Please type in a number: 5 Loop has ended

Demonstrations

#Starting a line with a hash will turn your text into a comment. Commenting #Starting a line with a hash will turn your text into a comment. (It will be ignored by the program)

Tasks WHILE LOOP TASKS: 1.Write a program that asks the user to input a series of numbers and adds them to a total until the user enters zero. (This stopping value is often called a rogue value). 2.Write a program which asks the user to set a password and then asks the user to enter the password again. Program it so that if the second password doesn’t match the one set, the request to enter the password is repeated until it does match the set password. 3.Write a program that asks the user for a number between 10 and 20 and validates (which means ‘tests’) that the input is within the correct range. It should repeatedly ask the user for a number from this range until the input is within the valid range.