Friday, April 5, 2019

Loops

What is loops ?
What loops do ?

Please do imagine you need go bicycle for 7 days.
There is a loop of 7 iterations, and your statement / activity should be cycling.

Keep it simple.
Loop help us to do job repeatedly in one block of codes.

There are several types of loops. For instance, there are for loop, while loop, do while loop and etc.

The basic / simple loop should be for loop.
You may ask your program to do the loop for how many times using "for" keywords.

Example : you may print number 0 to 4 using the statement below
for ind in range(5) : 
      print (ind)

On the other hand, a while loop will execute the block of codes / statement while / when some condition is fulfilled.

Example : the while loop print "It is true" as long as the condition is true.
Please be reminded that the condition must be true initially, else the while loop will not be executed.
Please be aware that u must have stopping criteria : assign the condition to false in the while loop.
Else you will be having an infinity loop and hit runtime error. (since you never tell the program when should it stops)

condition is true initially
while condition is true : 
       print("It is true")
       if  something happend :
              condition is false

Please do refer to the link below for further details:
Loop

No comments:

Post a Comment

Superior of partial fraction

What is complex fraction? Complex fraction is a fraction whereby its numerator or denominator or both consists of fractions. Partial frac...