@@ -316,104 +316,92 @@ perform various actions such as correcting an error, navigating to a certain
316
316
part of a program, etc. That's the power of catching the FALSE result.
317
317
318
318
## Loops
319
- There are commands or portions of your program
320
- to be repeated several times. Loops are there to do
321
- that. Now, there are simple loops and
322
- loops based on a given condition,
323
- much like a repeated IF statement.
324
- Simple loops are like repeat 10 times or repeat
325
- forever. Conditional loops are loops with
326
- specific conditions other than simple iteration
327
- just like in robot programming:
328
- repeat until color red, repeat until the
329
- distance is less than 50mm, etc.
330
-
331
- The most common that we see in computer
332
- programming: the ` for ` loop,
333
- ` while ` loop and ` do-while ` loop.
319
+ There are commands or portions of your program to be repeated several times.
320
+ Loops are there to do that. Now, there are simple loops and loops based on a
321
+ given condition, much like a repeated IF statement. Simple loops are like
322
+ "repeat 10 times" or "repeat forever." Conditional loops are loops with
323
+ specific conditions other than simple iteration, just like in robot
324
+ programming: "repeat until color red," "repeat until the distance is less than
325
+ 50mm," etc.
326
+
327
+ The most common loops that we see in computer programming are the ` for ` loop,
328
+ ` while ` loop, and ` do-while ` loop.
334
329
335
330
### Sample Program Using Loops
336
- ```
331
+ ``` c
337
332
#include < stdio.h>
338
333
339
334
int main () {
340
-
341
335
int n = 11;
342
-
343
336
int i;
344
337
int x[n];
345
- printf("the `for` loop: \n");
346
- for (i = 1; i < n; i++) {
347
338
348
- printf("iteration: %d | Hello World. \n", i);
339
+ printf ("The ` for ` loop:\n");
340
+ for (i = 1; i < n; i++) {
341
+ printf("Iteration: %d | Hello World.\n", i);
349
342
}
350
343
351
344
int y = 0;
352
- printf("------ \n");
353
- printf("the `while` loop: \n");
345
+ printf ("------\n");
346
+ printf("The ` while ` loop:\n");
354
347
while (y < 10) {
355
348
y += 1;
356
- printf("iteration : %d | Hello World. \n", y);
349
+ printf("Iteration : %d | Hello World.\n", y);
357
350
}
358
351
359
352
y = 0;
360
-
361
- printf("------ \n");
362
- printf("the `do while` loop: \n");
363
-
353
+ printf ("------\n");
354
+ printf("The ` do-while ` loop:\n");
364
355
do {
365
356
y += 1;
366
- printf("iteration: %d | Hello World. \n", y);
367
- }
368
- while (y < 10);
357
+ printf("Iteration: %d | Hello World.\n", y);
358
+ } while (y < 10);
369
359
370
360
return 0;
371
-
372
361
}
373
362
```
374
363
375
- the result:
364
+ The result:
376
365
```
377
- the `for` loop:
378
- iteration : 1 | Hello World.
379
- iteration : 2 | Hello World.
380
- iteration : 3 | Hello World.
381
- iteration : 4 | Hello World.
382
- iteration : 5 | Hello World.
383
- iteration : 6 | Hello World.
384
- iteration : 7 | Hello World.
385
- iteration : 8 | Hello World.
386
- iteration : 9 | Hello World.
387
- iteration : 10 | Hello World.
388
- ------
389
- the `while` loop:
390
- iteration : 1 | Hello World.
391
- iteration : 2 | Hello World.
392
- iteration : 3 | Hello World.
393
- iteration : 4 | Hello World.
394
- iteration : 5 | Hello World.
395
- iteration : 6 | Hello World.
396
- iteration : 7 | Hello World.
397
- iteration : 8 | Hello World.
398
- iteration : 9 | Hello World.
399
- iteration : 10 | Hello World.
400
- ------
401
- the `do while` loop:
402
- iteration : 1 | Hello World.
403
- iteration : 2 | Hello World.
404
- iteration : 3 | Hello World.
405
- iteration : 4 | Hello World.
406
- iteration : 5 | Hello World.
407
- iteration : 6 | Hello World.
408
- iteration : 7 | Hello World.
409
- iteration : 8 | Hello World.
410
- iteration : 9 | Hello World.
411
- iteration : 10 | Hello World.
366
+ The `for` loop:
367
+ Iteration : 1 | Hello World.
368
+ Iteration : 2 | Hello World.
369
+ Iteration : 3 | Hello World.
370
+ Iteration : 4 | Hello World.
371
+ Iteration : 5 | Hello World.
372
+ Iteration : 6 | Hello World.
373
+ Iteration : 7 | Hello World.
374
+ Iteration : 8 | Hello World.
375
+ Iteration : 9 | Hello World.
376
+ Iteration : 10 | Hello World.
377
+ ------
378
+ The `while` loop:
379
+ Iteration : 1 | Hello World.
380
+ Iteration : 2 | Hello World.
381
+ Iteration : 3 | Hello World.
382
+ Iteration : 4 | Hello World.
383
+ Iteration : 5 | Hello World.
384
+ Iteration : 6 | Hello World.
385
+ Iteration : 7 | Hello World.
386
+ Iteration : 8 | Hello World.
387
+ Iteration : 9 | Hello World.
388
+ Iteration : 10 | Hello World.
389
+ ------
390
+ The `do- while` loop:
391
+ Iteration : 1 | Hello World.
392
+ Iteration : 2 | Hello World.
393
+ Iteration : 3 | Hello World.
394
+ Iteration : 4 | Hello World.
395
+ Iteration : 5 | Hello World.
396
+ Iteration : 6 | Hello World.
397
+ Iteration : 7 | Hello World.
398
+ Iteration : 8 | Hello World.
399
+ Iteration : 9 | Hello World.
400
+ Iteration : 10 | Hello World.
412
401
```
413
402
414
- As you can see here, it's just printing the
415
- Hello World ten times, whether it's ` for ` loop,
416
- ` while ` loop or ` do-while ` loop.
403
+ As you can see here, it's just printing "Hello World" ten times, whether it's
404
+ a ` for ` loop, ` while ` loop, or ` do-while ` loop.
417
405
418
406
## Functions
419
407
A function is a group of statements (commands)
0 commit comments