While loop, Python Programming

Assignment Help:

 

You should use for whenever you can, because  it creates  the structure of your  loops clear. Sometimes, however, you require to do an operation various times, but you don't want to know in advance how many  times it requires  to be done.  In such conditions, you can use a while instruction statement, of the form:

 

while :

...

 

In  order  to  evaluate a while statement, the  interpreter  evaluates , placing a Boolean  value.  If the value  is 0, it skips  all the statements and  calculation moves  on to the next  statement in the code.  If the value  is 1 or true, then  the instructions are performed, and  the

 

is calculated again.  If it is False, execution of the loop is ended, and if it is

 

True, it goes around again.

It will generally be the case that you initialize a variable before the while statement, modify that variable in the course of executing the loop, and check some property of that variable in the Boolean expression. Consider that you need to write  a procedure that  takes an argument n and  returns the largest  power of 2 that is shorter  than  n. You may do it like this:

 

def pow2Smaller(n):

p = 1

while p*2 < n:

p = p*2

return p

 


Related Discussions:- While loop

Algorithms, how to calculate area of a square in square metres

how to calculate area of a square in square metres

Common Vulnerabilities, 1 Low Level Exploits 1.1 Savegames Jimmy is becomi...

1 Low Level Exploits 1.1 Savegames Jimmy is becoming increasingly frustrated at the computer game hes playing. He has a save right before the levels boss but he needs either more

Normalize a vector, Normalize a vector Let's imagine we want  to norma...

Normalize a vector Let's imagine we want  to normalize a vector  of three  variables;  that  is to compute a new  vector  of three values,  such that its size  is 1. Here is o

Python implementation of a solver for the desert crossing, Assume you have ...

Assume you have a truck which has to travel across a desert from the base camp at position 0 (left) to the target camp at position 4 (right). The intermediate positions 1,2, and 3

Bit Stuffing, How to write program on bit stuffing using python?

How to write program on bit stuffing using python?

Psuedocode, #ques Write the pseudocode (use a word processor please) and th...

#ques Write the pseudocode (use a word processor please) and the Python 3.0 program for the following problem. A monkey is being fed some food. Read in the starting weight in lbs:o

Non-local references, Non-local references So far, whenever we needed ...

Non-local references So far, whenever we needed to compute a variable, there  was  a binding for that  variable in the 'local' environment (the environment in which  we were e

While loop, You should use for whenever you can, because  it creates  the s...

You should use for whenever you can, because  it creates  the structure of your  loops clear. Sometimes, however, you require to do an operation various times, but you don't want t

Environments in python, Environments in Python Generally, Python estab...

Environments in Python Generally, Python establishes the following binding environments: 1.  builtin     : the mother of all environments: it contains the de?nitions of

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd