lunes, 9 de febrero de 2015

Using a While loop

A while loop is used in a similar fashin that the conditional loop, exept this will run every single time that the condition in it is found to be truth.

So you have your basic stuff. You grite your conditional inside parenthesis followed by a colon, and then you write all the code you want the program to repeat till the condition is truth in the following line with an identation.

example:

x = int(input("give me a number"))
while(x!=0):
           if(x>0):
                  print(x," is a positive number")
           if(x<0):
                  print(x," is a negative number")
           x=int(input("Change the number: ")
if(x==0):
          print(x," is cero")

In this case the program wont stop (it wont get out of the loop) untill you tipe the number "0". 

Again the Random númber program is a great exaple using the while loop.


No hay comentarios:

Publicar un comentario