From Apple to Raspberry Pi: While Loops

appleberry
In Xcode, both in Objective-C and C, we have the while loop.

int number = 5;
while (number > 0){
    NSLog(@"The number is %i",number--);
}

The python equivalent is also while Since we already understand the block, know sometimes as a suite in Python, while loops in  Python are very simple to use.

number = 5
while number > 0:
    number = number -1
    print ('The number is ', number)

We can use break in a loop to break execution. This is a valid loop if we need to test at the end of the loop

number = 5
while True:
    number = number -1
    print ('The number is ', number)
    if number < 0:
        break

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s