Engineering Computation Workshop 3 === ###### tags: `comp20005` `workshop` `c` --- ## Loops - While Loops - Do While Loops - For Loops - Nested Loops --- ## While Loops ![](https://i.imgur.com/BcEB7wR.png) --- ### While Loops ```cpp= int x = 5; while (x < 10) { printf("x is now: %d", x); x += 1; } ``` --- ### While Loops **Important**: Always remember to have a exit condition! ```cpp= int x = 5; while (x < 10) { printf("x is now: %d", x); } ``` --- ### While Loops **Important**: Make sure you iterate the right way! ```cpp= int x = 5; while (x < 10) { printf("x is now: %d", x); x -= 1; } ``` --- ### Do While Loops Essentially the same as a while loop, except the condition part gets checked after the executing the body of the loop. ![](https://i.imgur.com/DBNWHAM.jpg) --- ### Do While Loops Why use them? I honestly don't know and havent seen anyone use them... --- ### Do While Loops ```cpp= int x = 5; do { printf("x is now: %d", x); x += 1; } while (x < 10); ``` --- ### For Loops The most common, most awesome loop ![](https://i.imgur.com/IeNhIhF.png) --- ### For Loops The most common type, iterating over a fixed size n ```cpp= for (int i = 0 ; i < n ; i++) { printf("i is %d\n", i); } ``` --- ### For Loops In reverse? Have a think and try to figure out what needs changing! --- ### For Loops The most common type, iterating over a fixed size n ```cpp= for (int i = (n-1) ; i >= 0 ; i--) { printf("i is %d\n", i); } ``` --- ### Nested Loops ```cpp= #include <stdio.h> int main () { /* local variable definition */ int i, j; for(i = 2; i<100; i++) { for(j = 2; j <= (i/j); j++) { if(!(i%j)) break; // if factor found, not prime } if(j > (i/j)) printf("%d is prime\n", i); } return 0; } ``` --- ### Nested loops Keeping it clean ```cpp= #include <stdio.h> int main () { int i, j; for(i = 2; i<100; i++) { for(j = 2; j <= (i/j); j++) if(!(i%j))break; if(j > (i/j)) printf("%d is prime\n", i);}return 0; } ``` --- ### Nested Loops Always add a tab of indentation for any loop or if statment you enter ```cpp= int i, j, k, n; n = 10; for(int i=0; i < n; i++) { for(int j = i; j < n; j++) { for (int k = j; k < n; k++) { printf("i: %d, j: %d, k: %d", i, j, k); } } } ``` --- ## Exercises! --- ### 4.01a ```cpp= #include<stdio.h> int main(int argc, char *argv[]) { int i; for (i=0; i<20; i=i+3) { printf("%2d\n", i); } return 0; } ``` --- ### 4.01a ``` 0 3 6 9 12 15 18 ``` --- ### 4.01b ```cpp= #include<stdio.h> int main(int argc, char *argv[]) { int i; for (i=1; i<2000000; i=2*i) { printf("%7d\n", i); } return 0; } ``` --- ### 4.01b ``` 1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288 1048576 ``` --- ### 4.01c ```cpp= #include<stdio.h> int main(int argc, char *argv[]) { int i, sum; sum = 0; for (i=1; i<10; i++) { sum=sum+i; printf("S(%2d) = %2d\n", i, sum); } return 0; } ``` --- ### 4.01c ```cpp S( 1) = 1 S( 2) = 3 S( 3) = 6 S( 4) = 10 S( 5) = 15 S( 6) = 21 S( 7) = 28 S( 8) = 36 S( 9) = 45 ``` --- ### 4.01d ```cpp= #include<stdio.h> int main(int argc, char *argv[]) { int i, j; for (i=0; i<8; i++) { for (j=i+1; j<8; j+=3) { printf("i=%d, j=%d\n", i, j); } } return 0; } ``` --- ### 4.01d ``` i=0, j=1 i=0, j=4 i=1, j=2 i=1, j=5 i=2, j=3 i=2, j=6 i=4, j=5 i=5, j=6 i=6, j=7 ``` --- ### 4.01e ```cpp= #include<stdio.h> int main(int argc, char *argv[]) { int i, j; for (i=0; i<8; i++) { for (j=i+1; j<8; j+=3) { if (i+j==7) { break; } printf("i=%d, j=%d\n", i, j); } } return 0; } ``` --- ### 4.01e ``` i=0, j=1 i=0, j=4 i=1, j=2 i=1, j=5 i=2, j=3 i=2, j=6 i=4, j=5 i=5, j=6 i=6, j=7 ``` --- ### 4.01f ```cpp= #include<stdio.h> int main(int argc, char *argv[]) { int i, j; j=5; for (i=0; i<j; i++) ; { printf("i=%d, j=%d\n", i, j); } return 0; } ``` --- ### 4.01f ``` i=5, j=5 ``` --- ### 4.01g ```cpp= #include<stdio.h> int main(int argc, char *argv[]) { int i, j; j=5; for (i=0; i<j; j++) { printf("i=%d, j=%d\n", i, j); } return 0; } ``` --- ### 4.01g ``` i=0, j=251831 i=0, j=251832 i=0, j=251833 i=0, j=251834 i=0, j=251835 i=0, j=251836 i=0, j=251837 i=0, j=251838 i=0, j=251839 i=0, j=251840 i=0, j=251841 i=0, j=251842 i=0, j=251843 i=0, j=251844 ``` --- ### 4.02 > Give a general construction that shows how any do statement can be converted to an equivalent while statement. Discuss your solution. --- ### 4.02 Solution do while loop ```cpp= int x = 1; do { printf("x is %d\n", x); x += 1; } while (x < 10); ``` --- ### 4.02 Solution while loop ```cpp= int x = 1; int condition = True; while (condition) { printf("x is %d\n", x); x += 1; if (x >= 10) { break; } } ``` --- ### Helpful Snippets :) **For Loops** ```cpp int n = 10 for (int i = 0; i < n; i++){ // do something } ``` **While Loops** ```cpp int counter = 0; while (counter < 10) { counter++; // do something } ``` **Modulo** ```cpp 10 % 5 = 0 10 / 5 = 2 13 % 5 = 3 13 / 5 = 2 ``` --- ### Graphing ``` Enter numbers: 20 25 30 28 26 22 17 14 13 20 |******************** 25 |************************* 30 |****************************** 28 |**************************** 26 |************************** 22 |********************** 17 |***************** 14 |************** 13 |************* ``` --- ### Compute next prime ``` ./program Enter an integer value: 8 The next prime is : 11 ./program Enter an integer value: 87654321 The next prime is : 87654337 ``` --- ### ASCII Table ``` +0 +1 +2 +3 +4 +5 +6 +7 +-------------------------------- 32 | ! " # $ % & ' 40 | ( ) * + , - . / 48 | 0 1 2 3 4 5 6 7 56 | 8 9 : ; < = > ? 64 | @ A B C D E F G 72 | H I J K L M N O 80 | P Q R S T U V W 88 | X Y Z [ \ ] ^ _ 96 | ` a b c d e f g 104 | h i j k l m n o 112 | p q r s t u v w 120 | x y z { | } ~ ```
{"metaMigratedAt":"2023-06-15T04:32:42.445Z","metaMigratedFrom":"Content","title":"Engineering Computation Workshop 3","breaks":true,"contributors":"[{\"id\":\"097a8b2e-1817-41aa-b11f-65c49c54dbaf\",\"add\":6605,\"del\":230}]"}
    157 views