While Loops (Iteration 2)

Slides:



Advertisements
Similar presentations
Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
Advertisements

Announcements The first graded lab will be posted Sunday 2/15 and due Friday 2/27 at midnight It is brand new, so please don’t hand in last semester’s.
Do Loops A Do..Loop terminates based on a condition that is specified Execution of a Do..Loop continues while a condition is True or until a condition.
Loops – While, Do, For Repetition Statements Introduction to Arrays
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Sentinel Logic Assumes while loops and input statements.
Repetition Statements Repeating an Action A specified number of times While a Condition is True Until a Condition is True.
Control Structures FOR Statement Looping.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Python Repetition. We use repetition to prevent typing the same code out many times and to make our code more efficient. FOR is used when you know how.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
For loops in programming Assumes you have seen assignment statements and print statements.
Variables When programming it is often necessary to store a value for use later on in the program. A variable is a label given to a location in memory.
Decision Making CMSC 201. Overview Today we will learn about: Boolean expressions Decision making.
CS 100 Introduction to Computing Seminar October 7, 2015.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
Looping ROBERT REVAES. Logical Operators  && AND  Both have to be true for it to evaluate to be true.  || OR  One or the other has to be true for.
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.
CPSC 217 T03 Week V Part #1: Iteration Hubert (Sathaporn) Hu.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Introduction to Computers and Programming Lecture 7:
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.
Design Document Sample Given two concentrated circles with different radii, calculate the area which falls inside the big circle but outside the small.
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.
Python: Iteration Damian Gordon. Python: Iteration We’ll consider four ways to do iteration: – The WHILE loop – The FOR loop – The DO loop – The LOOP.
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.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
CMSC201 Computer Science I for Majors Lecture 07 – While Loops
Whatcha doin'? Aims: To start using Python. To understand loops.
Repetition (While-Loop) version]
CS161 Introduction to Computer Science
IF statements.
Think What will be the output?
While Loops in Python.
Iterations Programming Condition Controlled Loops (WHILE Loop)
Lecture 4B More Repetition Richard Gesick
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.
CMSC 120: Visualizing Information 3/25/08
© A+ Computer Science - What is a LOOP? © A+ Computer Science -
IPC144 Introduction to Programming Using C Week 3 – Lesson 1
Loops CIS 40 – Introduction to Programming in Python
We’re moving on to more recap from other programming languages
Coding Concepts (Basics)
IPC144 Introduction to Programming Using C Week 3 – Lesson 2
LOOPS BY: LAUREN & ROMEO.
For Loops.
Module 4 Loops.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Repetition In today’s lesson we will look at:
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
Inputs and Variables Programming Guides.
Conditional and iterative statements
We are starting JavaScript. Here are a set of examples
For Loops (Iteration 1) Programming Guides.
CprE 185: Intro to Problem Solving (using C)
A LESSON IN LOOPING What is a loop?
Chapter 4: Repetition Structures: Looping
Types of loops definite loop: A loop that executes a known number of times. Examples: Repeat these statements 10 times. Repeat these statements k times.
While Loops in Python.
CprE 185: Intro to Problem Solving (using C)
Iteration – While Loops
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

While Loops (Iteration 2) Programming Guides

While Loops (Iteration 2) Think about when you log in… “While the password you enter, doesn’t equal the password on your account, the program will repeatedly ask you to enter your password.” This is an example of a while loop in action!

While Loops (Iteration 2) ‘While Loops’ in Python While Loops are set up using the following statement: while x 0: Lets take a look more closely… <> <= == >= > < != These are called conditions

While Loops (Iteration 2) ‘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 == != > < The n is simply representing a value that we want x to either equal, not equal, be greater than etc depending on the while loop condition. If n=5 and the condition was while x != 5 (not equal to 5) then the loop would repeat until x equals 5.

While Loops (Iteration 2) ‘While Loops’ in Python x = 0 while x == 0: print(“Hello World”) Hello World Hello World Hello World Hello World Here is an example of a while loop that will never end. Because there is never a situation when x can change, the loop will repeat forever. Hello World Hello World Hello World

While Loops (Iteration 2) ‘While Loops’ in Python x = 0 while x != 5: x = int(input(“Please type in a number”)) print(“Loop has ended”) 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()) 122 -5 5 1 Please type in a number: 1 Please type in a number: -5 Please type in a number: 122 Please type in a number: 5 Here is an example of a while loop that will stop. Because the statement in the loop is asking for a new value for x, it means that x can be changed and so can stop if the correct value is entered. Loop has ended

While Loops (Iteration 2) Example of a while loop in action: