@@ -260,4 +260,356 @@ for (int i{1}, j{5}; i <= 5; ++i, ++j){
260
260
``` c++
261
261
for (;;)
262
262
cout << " Endless loop" << endl;
263
- ```
263
+ ```
264
+
265
+ ### Range-based ` for ` loop
266
+
267
+ - Introduced in C++ 11
268
+
269
+ ``` c++
270
+ for (var_type var_name: sequence)
271
+ statement; // can use var_name
272
+
273
+ for (var_type var_name: sequence){
274
+ statement; // can use var_name
275
+ }
276
+ ```
277
+
278
+ Example
279
+
280
+ ``` c++
281
+ int scores [] {100, 90, 97}; // array of integers
282
+
283
+ for (int score : scores)
284
+ cout << score << endl;
285
+
286
+ // Output:
287
+ // 100
288
+ // 90
289
+ // 97
290
+ ```
291
+
292
+ We actually do not have to declare the type of a collection
293
+
294
+ ```c++
295
+ int scores [] {100, 90, 97}; // array of integers
296
+
297
+ for (auto score : scores)
298
+ cout << score << endl;
299
+
300
+ // Output:
301
+ // 100
302
+ // 90
303
+ // 97
304
+ ```
305
+
306
+ #### Example: Vector - temperautre average
307
+
308
+ ``` c++
309
+ vector <double > temps {87.2, 77.1, 80.0, 72.5};
310
+
311
+ double average_temp {};
312
+ double running_sum {};
313
+
314
+ for (auto temp : temps){
315
+ running_sum += temp;
316
+ }
317
+
318
+ average_temp = running_sum / temps.size();
319
+ cout << average_temp << endl;
320
+ ```
321
+
322
+ #### Example: Initializer list - temperautre average
323
+
324
+ ``` c++
325
+ double average_temp {};
326
+ double running_sum {};
327
+ int size {0};
328
+
329
+ for (auto temp : {60.2, 80.2, 90.0, 78.2}){
330
+ running_sum += temp;
331
+ ++size;
332
+ }
333
+
334
+ average_temp = running_sum / size;
335
+ cout << average_temp << endl;
336
+ ```
337
+
338
+ ### Example: String
339
+
340
+ ``` c++
341
+ for (auto c : " Frank" ) {
342
+ cout << c << endl;
343
+
344
+ // Output:
345
+ // F
346
+ // r
347
+ // a
348
+ // n
349
+ // k
350
+ }
351
+ ```
352
+
353
+ ### ` While ` loop
354
+
355
+ ``` c++
356
+ while (expression)
357
+ statement;
358
+
359
+ while (expression) {
360
+ statement (s);
361
+ }
362
+ ```
363
+
364
+ - Useful for input validation
365
+ - If user's input is not valid, you would like to ask them to input again-and-again until input is correct
366
+
367
+ ``` c++
368
+ int number {};
369
+
370
+ cout << " Enter an integer less than 100: " ;
371
+ cin >> number;
372
+
373
+ while (number >= 100 ) {
374
+ cout << "Enter an integer less than 100: ";
375
+ cin >> number;
376
+ }
377
+
378
+ cout << " Thanks" << endl;
379
+ ```
380
+
381
+ ``` c++
382
+ int number {};
383
+
384
+ cout << " Enter an integer between 1 and 5: " ;
385
+ cin >> number;
386
+
387
+ while (number <= 1 || number >= 5 ) {
388
+ cout << "Enter an integer between 1 and 5: ";
389
+ cin >> number;
390
+ }
391
+
392
+ cout << " Thanks" << endl;
393
+ ```
394
+
395
+ - boolean flag
396
+
397
+ ``` c++
398
+ // initialization
399
+ bool done {false};
400
+ int number {0};
401
+
402
+ while (!done) {
403
+ cout << "Enter an integer between 1 and 5: ";
404
+ cin >> number;
405
+
406
+ if (number <=1 || number >= 5)
407
+ cout << "Out of range, try again" <<endl;
408
+ else {
409
+ cout << "Thanks" << endl;
410
+ done = true;
411
+ }
412
+ }
413
+ ```
414
+
415
+ ### ` do-while ` Loop
416
+
417
+ ``` c++
418
+ do {
419
+ statements;
420
+ } while (expression);
421
+ ```
422
+
423
+ - Useful for input validation
424
+
425
+ ``` c++
426
+ int number {};
427
+
428
+ do {
429
+ cout <<"Enter an integer between 1 and 5: " << endl;
430
+ cin >> number;
431
+ } while (number <= 1 || number >= 5 );
432
+
433
+ cout << " Thanks" << endl;
434
+ ```
435
+
436
+ Area calculation with calculate another
437
+
438
+ ``` c++
439
+ char selection {};
440
+
441
+ do {
442
+ double width {}, height {};
443
+ cout << "Enter width and height seperated by a space: ";
444
+ cin >> width >> height;
445
+
446
+ double area {width * height};
447
+ cout << "The area is " << area << endl;
448
+
449
+ cout << "Calculate another? (Y/N): ";
450
+ cin >> selectionl
451
+ } while (selection == ' Y' || selection == ' y' );
452
+
453
+ cout << " Thanks!" << endl;
454
+ ```
455
+
456
+ ### ` continue ` and ` break ` statements
457
+
458
+ - ` continue `
459
+ - no further statements in the body of the loop are executed
460
+ - control immediately goes directly to the beginning of the loop for the next iteration
461
+
462
+ - ` break `
463
+ - no further statements in the body of the loop are executed
464
+ - loop is immediately terminate
465
+ - control immediately goes to the statement following the loop construct
466
+
467
+ Example
468
+
469
+ ``` c++
470
+ vector <int > values {1, 2, -1, 3, -1, -99, 7, 8, 10};
471
+
472
+ for (auto val : values) {
473
+ if (val == -99)
474
+ break;
475
+ else if (vale == -1)
476
+ continue;
477
+ else
478
+ cout << val << endl;
479
+ }
480
+
481
+ // Output
482
+ // 1
483
+ // 2
484
+ // 3
485
+ ```
486
+
487
+ ### Infinite loop
488
+
489
+ - aka endless loop
490
+ - Loops whose condition expression always evaluate to true
491
+ - Ususally this is unintended and a programmer error
492
+ - Sometimes programmers use infinite loop and include and break statements in the body to control them
493
+ - Sometimes infinite loops are exactly what we need
494
+ - event loop in an event-driven program
495
+ - operating system
496
+
497
+ #### Infinite ` for ` loop
498
+
499
+ ``` c++
500
+ for (;;)
501
+ cout << " This will print forever" << endl;
502
+ ```
503
+
504
+ #### Infinite ` while ` loop
505
+
506
+ ``` c++
507
+ while (true )
508
+ cout << " This will print forever" << endl;
509
+ ```
510
+
511
+ #### Infinite ` do-while ` loop
512
+
513
+ ``` c++
514
+ do {
515
+ cout << "This will print forever" << endl;
516
+ } while (true );
517
+ ```
518
+
519
+ Example of useful infinite loop
520
+
521
+ ``` c++
522
+ while (true ) {
523
+ char again {};
524
+ cout << "Do you want to loop again? (Y/N): " << endl;
525
+ cin >> again;
526
+
527
+ if (again == 'N' || again == 'n')
528
+ break;
529
+ }
530
+ ```
531
+
532
+ ### Nested loop
533
+
534
+ - Loop nested within another loop
535
+ - Can be many levels deep as the program needs
536
+ - Very useful with multi-dimensinoal data structures
537
+ - Outer loop vs Inner loop
538
+
539
+ Example
540
+
541
+ ``` c++
542
+ for (outer_val {1}; outer_val <= 2; ++outer_val)
543
+ for (inner_val {1}; inner_val <= 3; ++inner_val)
544
+ cout << outer_val << ", " << inner_val << endl;
545
+
546
+ // Output
547
+ // 1, 1
548
+ // 1, 2
549
+ // 1, 3
550
+ // 2, 1
551
+ // 2, 2
552
+ // 2, 3
553
+
554
+ // Note: inner loop looops "faster"
555
+ ```
556
+
557
+ Example: Multiplcation Table
558
+
559
+ ```c++
560
+ // Display 10 x 10 nultiplication table
561
+
562
+ for (int num1 {1}; num1 <= 10; ++num1) {
563
+ for (int num2 {1}; num2 <= 10; ++num2) {
564
+ cout << num1 << " * " << num2 << " = " << num1 * num2 << endl;
565
+ }
566
+ cout << "----------" << endl;
567
+ }
568
+ ```
569
+
570
+ Example: 2D arrays - set all elements to 1000
571
+
572
+ ``` c++
573
+ int grid[5 ][3 ] {};
574
+
575
+ for (int row {0}; row < 5; ++row) {
576
+ for (int col {0}; col < 3; ++col) {
577
+ grid[ row] [ col ] = 1000;
578
+ }
579
+ }
580
+
581
+ // =========
582
+
583
+ int grid[5 ][3 ] {};
584
+
585
+ for (int row {0}; row < 5; ++row) {
586
+ for (int col {0}; col < 3; ++col) {
587
+ cout << grid[ row] [ col << " ";
588
+ }
589
+ cout << endl;
590
+ }
591
+ ```
592
+
593
+ Example: 2D vector - display elements
594
+
595
+ ``` c++
596
+ vector <vector <int >> vector_2d
597
+ {
598
+ {1, 2, 3},
599
+ {10, 20, 30, 40},
600
+ {100, 200, 300, 400, 500}
601
+ };
602
+
603
+ for (auto vec : vector_2d) {
604
+ for (auto val : vec) {
605
+ cout << val << " ";
606
+ }
607
+ cout << endl;
608
+ }
609
+
610
+ // Output
611
+ // 1 2 3
612
+ // 10 20 30 40
613
+ // 100 200 300 400 500
614
+ ```
615
+
0 commit comments