Saturday 11 January 2014

Looping

Components

Looping's components: (a = b x c)
(a) what to be looped(b)
(b) how many times (c)
- Unknown
- Known

(c) result
(d) exit condition or stop condition

Which one?
If you know what to be looped and how many times you loop and what to be looped, you can use for.
If you don't know how many times you loop but know what to be looped and the what result you want, you can use while.

For: a, b, c
While: d

Examples
Python looping
- Python For
Example (Python)
>>> for i in range (0,2):
    print i

  
0
1
- Python While
 Example (Python):
>>> x=1
>>> while x < 3:
    x = x + 1
    print x

  
2
3

C Looping
- For
Usage: for (start counter; end counter; how to get to end counter)

Example:

- While
 This the most basic loop in C.
check first, do last.

Example:



- do..While
do.. While loop is just like while except the condition to test is behind
do first, check later




No comments:

Post a Comment