While Loops CMSC 201. Overview Today we will learn about: Looping Structures While loops.

Slides:



Advertisements
Similar presentations
Do-while Loops Programming. COMP102 Prog Fundamentals I: do-while Loops /Slide 2 The do-while Statement l Syntax do action while (condition) l How it.
Advertisements

CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
Introduction to Computing Science and Programming I
CHAPTER 5: LOOP STRUCTURES Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
Repeating Actions While and For Loops
Computer Science 1620 Loops.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 6, 2005.
COMP 14 Introduction to Programming Miguel A. Otaduy May 21, 2004.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Week 10 Recap CSE 115 Spring For-each loop When we have a collection and want to do something to all elements of that collection we use the for-each.
1 Parts of a Loop (reminder) Every loop will always contain three main elements: –Priming: initialize your variables. –Testing: test against some known.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Loops – While, Do, For Repetition Statements Introduction to Arrays
General Computer Science for Engineers CISC 106 Lecture 13 Roger Craig Computer and Information Sciences 3/13/2009.
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.
An Object-Oriented Approach to Programming Logic and Design Chapter 6 Looping.
1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.
Lecture Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Nested Loops. Nesting Control Structures One if statement inside another one An if statement inside a loop A loop inside an if statement Control structures.
CPS120 Introduction to Computer Science Iteration (Looping)
Programming Logic and Design Fifth Edition, Comprehensive
Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
 Wednesday, 9/18/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/18/02  QUESTIONS?? HW #1 due today at 5!!  Today: Loops, and two new data types.
C++ for Engineers and Scientists, Third Edition1 Objectives In this chapter, you will learn about: Basic loop structures while loops Interactive while.
Python Tricks CMSC 201. Overview Today we are learning some new tricks to make our lives easier! Slicing and other tricks Multiple return values Global.
Nesting Quantifiers And Their Manipulation Copyright © Curt Hill.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Decision Making CMSC 201. Overview Today we will learn about: Boolean expressions Decision making.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
CPS120 Introduction to Computer Science Iteration (Looping)
Printing with for Loops To print a character multiple times, use a for loop. for (int j = 1; j
GCSE Computing: Programming GCSE Programming Remembering Python.
Decision Making CMSC 201 Chang (rev ).
Tutorial 9 Iteration. Reminder Assignment 8 is due Wednesday.
While ( number
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
Loops CMSC 201. Overview Today we will learn about: Looping Structures While loops For Loops.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Loops ( while and for ) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan 1.
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Follow up from lab See Magic8Ball.java Issues that you ran into.
More about Iteration Victor Norman CS104. Reading Quiz.
FOP: While Loops.
CMSC201 Computer Science I for Majors Lecture 07 – While Loops
Chapter 4 Repetition Statements (loops)
( Iteration / Repetition / Looping )
Chapter 5 Structures.
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.
LESSON 11 – WHILE LOOPS UNIT 5 – 1/10/17.
Outline Altering flow of control Boolean expressions
A mini TRACS.
Iteration: Beyond the Basic PERFORM
LOOPS BY: LAUREN & ROMEO.
Chapter 5: Control Structure
Conditional and iterative statements
Building Java Programs
Console.WriteLine(“Good luck!”);
The for loop suggested reading:
CHAPTER 6: Control Flow Tools (for and while loops)
CHAPTER 5: Control Flow Tools (if statement)
Repetition Statements (Loops) - 2
Problem Input a number and print its table using while loop. In the end print Good Bye only once.
Just how far from zero are you?
Presentation transcript:

While Loops CMSC 201

Overview Today we will learn about: Looping Structures While loops

Looping Loops let us do something until a boolean condition is met. The simplest loop is called a while loop.

The while loop The syntax for a while loop is as follows: line-1 while someCondition: line-2 line-3 line-4

The while loop line-1 while someCondition: line-2 line-3 line-4 line-1 Condition is true line-2 line-3 Condition is false line-4 Condition is false Condition is true

Example a = 5 while a > 0: print(a) a = a – 1 Prints

Notes If the condition is false to begin with, the loop never executes! a = 4 b = 5 while a == b: print("Hello") print("Goodbye") This will only print goodbye.

Notes It is very important that the variable in the condition be altered at some point during the loop. a = 5 while a > 0: print(a) This is called an infinite loop, since a will always be greater than zero.

Factorial Example Computing factorial with a while loop. n = int(input("Enter a number ")) counter = n answer = 1 while counter > 0: answer = answer * counter counter = counter - 1 print("n factorial is ", answer)

Factorial Example

Exercise Use a while loop to print out every number between 0 and 100 that is divisible by three.

Loops + If Statements Now that you know how to use loops, if statements, and variables, you can create fairly complicated algorithms. However, figuring out how to combine them can be difficult! In the next few homeworks and exercises, you will start needing to figure out when to use each tool you’ve been given so far. These are the basic elements of programming!

Nested Loops Nested loops are loops inside loops. The first loop is the outer loop The second one is the inner loop. a = 0 while a < 5: b = 0 while b < 4: print(b) b = b + 1 a = a + 1

Nested Loops