Presentation is loading. Please wait.

Presentation is loading. Please wait.

Review (before the 1 st test): while (conditions) { statements; } while loop: if/else if/else statements: if (conditions) { statements; } else if (different.

Similar presentations


Presentation on theme: "Review (before the 1 st test): while (conditions) { statements; } while loop: if/else if/else statements: if (conditions) { statements; } else if (different."— Presentation transcript:

1 Review (before the 1 st test): while (conditions) { statements; } while loop: if/else if/else statements: if (conditions) { statements; } else if (different conditions) { statements; } else { statements; } while (conditions) { statements; while (conditions) { statements; } statements; } Nested while statements: for (initialization; conditions; increment) { statements; } for loop:

2 Reminder on functions: int function(int a, int b); /* function prototype */ int main() { int functionvalue, num1, num2; functionvalue = function(num1, num2); /* call the function */ } int function(int num1, int num2) /* the function */ { int returnvariable; returnvariable = perform actions; return returnvariable; }

3 Reminder on functions, another example: void function(int a, float b); /* function prototype */ int main() { int num1; float num2; function(num1, num2); /* call the function */ } void function(int num1, float num2) /* the function */ { perform actions; }

4 This calculates the average! #include int main(void) { int num[9] = {10,20,30,40,50,60,70,80,90}; int calc=0, i; for (i=0;i<9;i++) { calc = calc+num[i]; } calc = calc/9; printf(“calc = %d\n”, calc); } REMINDER OF ARRAYS:

5 1. A A 65 #include int main() { char letterA = 'A'; char num = 65; printf("%c\n", letterA); printf("%c\n", num); printf("%d\n", letterA); } Output example #1: 2. A\0 A 65 3. A A 4. A 65 A

6 #include int main() { char letterA = 'A'; char sentence[80] = "Welcome back!"; printf("%s\n", sentence); printf("%d\n", strlen(sentence)); printf("%s %c\n", sentence, letterA); } Output example #2: 2. Welcome back! 13 Welcome back! A 1. Welcome back! 14 Welcome back! A 3. “Welcome back!” 13 “Welcome back!” A 4. W 13 W

7 #include int main() { char sentence[] = "Welcome back!"; int i; for (i = 0; i < strlen(sentence); i+=2) { printf("%c ", sentence[i]); } printf("\n"); } Output example #3: 2. “Welcome back!” 1. Welcome back! 3. Gives you an error! 4. W l o e b c !

8 #include int main() { char sentence[80] = "Welcome back!"; sentence[3] = '\0'; printf("%s\n", sentence); printf("%d\n", strlen(sentence)); } Output example #4: 2. Welcome back! 13 1. Wel 3 3. Welcome back! 3 4. Wel 13

9 #include int main() { char sentence[80] = "Welcome back!"; char sentence2[80] = "Hello class."; char sentence3[] = "Hello again."; strcpy(sentence2, sentence); printf("%s\n", sentence2); strcat(sentence, sentence3); printf("%s %s\n", sentence, sentence3); } Output example #5: 4. Welcome back! Welcome back!Hello again. Hello again. 2. Hello class. Welcome back!Hello again. 3. Hello class. Hello again. Hello again. 1. Welcome back! Welcome back!Hello again.

10 Strings are character arrays (example): #include int main() { char sentence[80]; /* large array length */ int i; sentence[0] = 'B'; sentence[1] = 'y'; sentence[2] = 'e'; sentence[3] = '\0'; printf("%s", sentence); /* prints Bye */ for (i = 0; i < strlen(sentence); i++) { printf("%c", sentence[i]); /* prints Bye */ } Some more string stuff: Caution when using scanf with strings: scanf(“%s”, string) /* no & is needed w/ %s*/ scanf(“%c”, &char) /* & is needed with %c */


Download ppt "Review (before the 1 st test): while (conditions) { statements; } while loop: if/else if/else statements: if (conditions) { statements; } else if (different."

Similar presentations


Ads by Google