While loop, Python Programming

 

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

 

Posted Date: 8/9/2012 7:19:47 AM | Location : United States







Related Discussions:- While loop, Assignment Help, Ask Question on While loop, Get Answer, Expert's Help, While loop Discussions

Write discussion on While loop
Your posts are moderated
Related Questions
Environments in Python Generally, Python establishes the following binding environments: 1.  builtin     : the mother of all environments: it contains the de?nitions of

Procedures as First-class objects In Python, unlike  many  other  languages, methods are behave in much  the same way as num­ bers:  they  can be stored as values  of variable

Perimeter of a polygon Now, let's consider the problem of computing the length  of the perimeter of a polygon. The input is a structure of vertices,  encoded as a list of list

Worked example 1   Let's examine what  happens when  we compute the following Python code:   def p(x, y): z = x*x - y return z + 1/z   >>> p(1.0, 2.0) -2.0

Modules As you start to write larger programs, you will want  to save the function de?nitions in multiple ?les, collected together according to what  they  do.  So, for exampl

For this assignment, you are to: 1.    Modify the output format of your Python script from Assignment 1 to match the input format of your C program from Assignment 2. 2.    W

how to use re.sub to seperate distint words and phrases when translating text from English to Spanish or vis versa in python programming

for simple interest and compound interest

For and While   If we want  to do some operation or set of operations various  times, we can handle the process  in several  different types.  The most straightforward types

A program to count how many files are on the file system which displays summary information regarding the total number of bytes used by all files and a breakdown of the number of b