DOC PREVIEW
TAMU CSCE 110 - What is a while function?
Type Lecture Note
Pages 3

This preview shows page 1 out of 3 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CSCE 110 1nd EditionLecture 5Outline of Last Lecture:I. Definition of an if/else functionA. How to write an if/else function correctlyB. Examples of different if/else functionsII. Output of an if/else programA. How different inputs can affect the outputB. Order of line execution in the programIII. If/elif/else functionA. Examples of an if/elif/else functionB. How different inputs can affect the outputC. Order of line execution in the programOutline of Current Lecture:I. What is a while function?a) Example of a while functionII. Execution order of the lines of code in a while functionIII. Using our new knowledge to write different types of while functionsCurrent Lecture:I. Definition: The while function is a way to keep executing a command/expression whilea given Boolean/condition is satisfied. It is very important to keep track of variables that change values within the loop. Here is an example of a program that uses the while function:1 >>> x = 02 >>> while x < 4:3 >>> x = x + 14 >>> print xII. Here's what happens when the program is run:1. Line 1 is read, and the variable is set2. Line 2 is read3. Line 3 is read because the expression 0<4 is true, and the value of x changes from 0 to 0+1= 14. Line 2 is read againThese notes represent a detailed interpretation of the professor’s lecture. GradeBuddy is best used as a supplement to your own notes, not as a substitute.5. Line 3 is read because the expression 1<4 is true, and the value of x changes from 1 to 1+1= 26. Line 2 is read again7. Line 3 is read because the expression 2<4 is true, and the value of x changes from 2 to 2+1= 38. Line 2 is read again9. Line 3 is read because the expression 3<4 is true, and the value of x changes from 3 to 3+1= 410. Line 2 is read again11. Line 3 is skipped, because the Boolean in Line 2 is now false, and Line 4 is read, and we receive the output:>>> 4The loop will keep running until Line 2's expression is false. In this case, the loop was executed four times total.What if the while loop expresses a condition that is always true? This will produce an "infinite loop," and the program will never stop running unless you manually stop it. Here's an example:1 >>> x = 52 >>> while x > 4:3 >>> x = x + 14 >>> print xThe program would never be able to leave the loop. It would never read line 4, sothere would never be any output. The program would constantly be running and increasing x by 1. III. Different ways to use while functions:Let's write a program that adds up the sum of all the numbers from 1 to 100:1 >>> total = 02 >>> x = 13 >>> while x <= 100:4 >>> total = total + x5 >>> x = x + 16 >>> print totalThe output of this program will be:>>> 5050Now let's write a program that adds up the sum of all numbers of a range that the user inputs.1 >>> start = int(raw_input("Starting point: "))2 >>> end = int(raw_input("Ending point: "))3 >>> sums = 04 >>> i = start5 >>> while i <= end:6 >>> sums = sums + i 7 >>> i = i + 18 >>> print sumsSo let's say start = 2 and end = 6. The output received will be:>>> 15Let's write a more complicated program now. We will create a guessing game that allowsthe user to input guesses for a number between 1 and 100 that the computer randomly selects. Depending on the guess of the user, the computer will respond with "Higher!" or"Lower!" until the user is able to narrow down his guesses to the final answer. For this game, the computer picks a random number within a given range. To do this, wetype:>>> import random From then on, the random number we wish to reference will be called:random.randint(1,100) Now we are ready to write the program.1 >>> print "Let's play a guessing game!"2 >>> x = 03 >>> import random4 >>> number = random.randint(1,100)5 >>> while x != number:6 >>> x = int(raw_input("Guess a number! "))7 >>> if number = x:8 >>> print "You got it!"9 >>> elif x < number:10 >>> print "Higher!"11 >>> else:12 >>> print "Lower!"Another helpful tip: If we want to explain how our program works to someone who is reading our code, we can start a line with a # and insert any amount of text after that. The text added will have no function other than to be read by the


View Full Document

TAMU CSCE 110 - What is a while function?

Type: Lecture Note
Pages: 3
Documents in this Course
06-IO

06-IO

29 pages

21-OOP

21-OOP

8 pages

key

key

6 pages

21-OOP

21-OOP

8 pages

Load more
Download What is a while function?
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view What is a while function? and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view What is a while function? 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?