
While Loops in Programming
Learn about while loops, a fundamental looping structure in programming. Explore the syntax, examples, and applications of while loops. Understand how to avoid infinite loops and create efficient loops for tasks like computing factorials.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
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 Condition is true Condition is false Condition is true line-2 line-3 line-1 line-4 Condition is false
Example a = 5 while a > 0: print(a) a = a 1 Prints 5 4 3 2 1
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 Loop control: counter counts down from n down to 1 n = int(input("Enter a number ")) counter = n answer = 1 while counter > 0: answer = answer * counter counter = counter - 1 print("n factorial is ", answer) Sanity check: what if n is 0 ? n is 1 ? n is -1?
Factorial Example Calculating the answer. n = int(input("Enter a number ")) counter = n answer = 1 while counter > 0: answer = answer * counter counter = counter - 1 print("n factorial = ", answer)
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 The second one is the inner outer loop inner loop. a = 0 while a < 5: b = 0 while b < 4: print(b) b = b + 1 a = a + 1
Nested Loops Loop control for outer loop: a = 0 while a < 5: b = 0 while b < 4: print(b) b = b + 1 a = a + 1
Nested Loops Loop control for inner loop: a = 0 while a < 5: b = 0 while b < 4: print(b) b = b + 1 a = a + 1
Nested Loops Loop control for outer and inner loops: a = 0 while a < 5: b = 0 while b < 4: print(b) b = b + 1 a = a + 1