<h1>Chapter 2. Control statements and preprocessor</h1> # Conditional statements <!-- 1. Numeroinnin voi toteuttaa jälkeenpäin --> ## If-statement <!-- 1.1. --> ```clike #include <stdio.h> int main(void) { printf("Program starts.\n"); int firstInput; int secondInput; /* Asking user for two inputs of type integer */ printf("Give an integer: "); scanf("%d", &firstInput); printf("Give second integer: "); scanf("%d", &secondInput); /* Doing conditional check for input equality */ if(firstInput==secondInput) { printf("Inputs are equal.\n"); } else if(firstInput>secondInput) { printf("First is greater than second.\n"); } else if(firstInput<secondInput) { printf("Second is greater than first.\n"); } else { printf("Something went wrong.\n"); } /* This program is missing a check for user input variable type */ printf("Program stops.\n"); return(0); } ``` Nested if-statements are hard to debug with poor indetation. ## Switch cases <!-- 1.2. --> ```clike #include <stdio.h> int main(void) { printf("Program starts.\n"); int choice; printf("Choose a number (1-3): "); scanf("%d", &choice); switch (choice) { case 1: printf("You chose 1.\n"); break; case 2: printf("You chose 2.\n"); break; case 3: printf("You chose 3.\n"); break; default: printf("Unknown input.\n"); break; } printf("Program stops.\n"); return(0); } ``` Switch case syntax is very similar to python. Notice that the cases do not end in } or ; this is very rare in C. ## Ternary operation <!-- 1.3. --> C offers alot of ways to compress code. For example an if-statement can be written on one line using ternary operator. Ternary operation syntax: `result = (condition) ? value_if_true : value if_false`{.clike} In the example below, 3 variables are initialized as an integer. Then the last integer value is decided in ternary operation. ```clike #include <stdio.h> int main(void) { int number1 = 2, number2 = 13, bigger; bigger = (number1 > number2) ? number1 : number2; /* After ? cases are true : false*/ printf("The bigger number is %d.\n", bigger); return(0); } ``` **Example program run:** ```stdio The bigger number is 13. ``` Here we compare `number1` and `number2`. Based on if its true or false we choose # Repetition structures <!-- 2. --> ## While structure <!-- 2.1 --> While loops don't require loop counts but is still required the programmer to choose how the loop ends. ```clike #include <stdio.h> int main(void) { printf("Program starts.\n"); int loops = 5, loopCount = 0; while (loops > loopCount) { printf("Current loop number is: %d!\n", loopCount); loopCount++; } printf("Program stops.\n"); return(0); } ``` **Example program run:** ```stdio Program starts. Current loop number is: 0. Current loop number is: 1. Current loop number is: 2. Current loop number is: 3. Current loop number is: 4. Program stops. ``` ## For structure <!-- 2.2 --> Works exactly the same way as in python but syntax is closer to JavaScript. ```clike #include <stdio.h> int main(void) { int i = 0; printf("Program starts.\n"); for (i = 0; i <= 10; i++) { printf("%d ", i); } printf("\nProgram stops.\n"); return(0); } ``` **Example program run:** ```stdio Program starts. 0 1 2 3 4 5 6 7 8 9 10 Program stops. ``` ## Do-while structure <!-- 2.3 --> This is a new one that is not found in python. ```clike #include <stdio.h> int main(void) { int stop = 5, start = 10; printf("Program starts.\n"); printf("Do runs atleast once.\n"); do { if (stop < start) { printf("Stop is already less than start:\n"); } printf("Stop: %d and start: %d\n", stop, start); start++; } while (start < stop); printf("Program stops.\n"); return(0); } ``` **Example program run:** ```bash Program starts. Do runs atleast once. Stop is allready less than start: Stop: 5 and start: 10 Program stops. ``` Do-while is best used for checking a condition and doing loops after. Even tough the example numbers are already twice larger it still runs it once. # Other control commands (return,continue,break,goto) <!-- 3. --> C has the familiar return, continue and break commands from python and a new one goto. Pass command is not found in c. ```clike #include <stdio.h> int main(void) { int start = 30, end = 100; printf("Program starts.\n"); do { start++; /* Check parity and continue */ if (start % 2 != 0) { printf("Odd number, going for a new loop...\n"); continue; } /* Substract 42 */ if (start - 42 != 0) { printf("The difference is %d\n", start - 42); } else { /* Break when start is 42 */ printf("Found 42!\n"); break; } } while (start < end); printf("Program stops.\n"); return(0); } ``` **Example program run:** ```stdio Program starts. Odd number, lets continue... The difference is -10 Odd number, lets continue... The difference is -8 Odd number, lets continue... The difference is -6 Odd number, lets continue... The difference is -4 Odd number, lets continue... The difference is -2 Odd number, lets continue... Found 42! Program stops. ``` Why goto is bad? Goto statement lets you skip to a certain part of your program. This leads to spaghetti code. You might run into it when handling legacy code. # Preprocessors <!-- 4. --> Most common preprocessors are `include`, `define` and `if-endif` statements. These statements start with a `#` (hashtag). They can be used for example in minimizing the memory requirements, maximizing the runtime or enabling debugging in a program. ## Include <!-- 4.1. --> Include command is used to import libraries into your program. The most common imported library is `stdio.h` which contains basic numerical and printing functions. <h4>Common C-libraries</h4> | Library | Functionality | | -------- | -------- | | stdlib.h | Type convertion, memory management and system commands | | time.h | Time and date functions | | math.h | Mathematical functions | | string.h | String handling functions | ## Define <!-- 4.2. --> Define is essentially complicated version of constants from other programming languages. There constant are defined using ``#define name value`` syntax. These constant are string literals meaning the preprocessor replaces the given string with their values. ```clike #define TRUE 1 ``` Example use is to give ``TRUE`` the value of ``1``. ```clike #include <stdio.h> #define TRUE 1 #define FALSE 0 #define NUMBER 1234 int main(void) { int a = TRUE; int b = 0; printf("%d\n", NUMBER); if (a == TRUE) { printf("a is TRUE even as an integer!\n"); } if (b == FALSE) { printf("It also works the otherway around.\n"); } return(0); } ``` **Example program run:** ```stdio 1234 a is TRUE even as an integer! It also works the otherway around. ``` ## if-endif <!-- 4.3. --> These are used for excluding certain pieces of code from the final program run. Basicly the same way as commenting out code but more useful. ```clike #if 0 /* These are visible in sourcecode. * Preprocessor deletes between if 0 and endif from the users view. * Rows can be made visible by making the if 0 to if 1. */ #endif ``` There is also a ``ìfndef`` (if not defined) which is basicly used to set a default value for a variable ```clike // By leaving NUMBER undefined ifndef get triggered and gives it value of 10 #ifndef NUMBER #define NUMBER 10 #endif ```