270 likes | 396 Views
This presentation covers the essentials of executing a C program, focusing on the `for` loop structure and its applications in managing arrays. It starts with the fundamental flow of a C program from the `main()` function and introduces the sequential control flow. The `for` loop is explored in detail, demonstrating how to execute code blocks a predetermined number of times. Multiple examples illustrate the implementation of loops to handle integers, and arrays are introduced as indexed storage for variables. Logical operators are discussed for improved performance, including the use of break statements and null statements.
E N D
CSCI 171 Presentation 4
Execution of a C Program • Execution starts in main( ) • Top down style • sequential flow • Unrealistic to expect sequence in more complicated programs
Program Control • Change order in which program statements are executed • logic may no longer be top down • Done by using Program Control Statements • for loops • while loops
for statement • Executes a block of code a predetermined number of times • number of executions is known prior to iterations being performed • Counted Repetition Structure
for Statement • Format: • for (initial; condition; increment) • 1. initial evaluated (usually assignment) • 2. condition evaluated (usually relational) • 3. condition is false - terminate for loop • condition is true - C statements within • loop execute • 4. increment executed - return to step 2
Example of for loop //Write the C code to display the first 3 positive integers //DEMONSTRATING A FOR LOOP #include <stdio.h> void main() { int count = 0; for (count = 1; count <= 3; count ++) { printf(“\n%d”, count); } }
Example of for loop //Write the C code to display the first 3 positive integers in descending order //DEMONSTRATING A FOR LOOP #include <stdio.h> void main() { int count = 0; for (count = 3; count >= 1; count --) { printf(“\n%d”, count); } }
Sample Program 4.1 #include <stdio.h> int main( void ) { int index = 0; for (index = 2; index <= 40; index+=2) { printf("%d\n", index); } }
Flexibility of for statement int count = 1; for (; count < 1000; count++) IS equivalent to: int count = 0; for (count = 1; count < 1000; count ++)
Scope of Variables • Index value holds a value after loop void main() { int count = 0; for (count = 1; count <= 3; count ++) { printf(“\n%d”, count); } printf(“\n%d”, count); }
Sample Program 4.2 #include <stdio.h> int main( void ) { int index = 0; for (index = 2; index <= 38; index+=2) printf("%d\n", index); printf("%d\n", index); }
Omission of increment void main() { int count = 0; for (count = 1; count <= 3;) { printf(“\n%d”, count); } }
Omission of Increment - cont’d void main() { int count = 0; for (count = 1; count <= 3;) { printf(“\n%d”, count); count++; } }
Sample Program 4.3 #include <stdio.h> int main( void ) { int index = 0; for (index = 2; index <= 38; index+2) printf("%d ", index); }
Quick Introduction to Arrays • Indexed group of storage locations • Same type of variables • Identified by subscript (i.e. the index) • int data[1000]; • creates a storage location for 1000 integer elements in the variable data • First location in an array is 0 (not 1)
Referencing elements in an array • int i; • int data[3]; • data[0] = 3; • data[1] = –4; • data[2] = 7; • for (i = 0; i < 3; i++) • { • printf(“%d “, data[i]); • }
Referencing elements in an array • Can use variables or integers • int count = 2; • data[count] • is equivalent to: • data[2];
Sample Program 4.4 #include <stdio.h> int main( void ) { int my_array[5], sum = 0; my_array[0] = 1; my_array[1] = 2; my_array[2] = 3; my_array[3] = 4; my_array[4] = 5; sum = my_array[0] + my_array[1] + my_array[2] + my_array[3] + my_array[4]; printf("The sum of the elements in the array is: %d", sum); }
Sample Program 4.5 #include <stdio.h> int main( void ) { int my_array[5], sum = 0, index = 0; for (index = 0; index < 5; index++) my_array[index] = index + 1; for (index = 0; index < 5; index++) sum+=my_array[index]; printf("The sum of the elements in the array is: %d", sum); }
Logical operators in a for loop • Assume a 10 element array with random integers has been defined • Search the array to determine if the input value (searchElement) is in the array: • searchFlag = ‘N’; • scanf(“%d”, &searchElement); • for (cnt = 0; cnt < 10; cnt++) • { • if (array[cnt] == searchElement) • searchFlag = ‘Y’; • }
Logical operators in a for loop • Logical operators can sometimes be used to increase performance • searchFlag = ‘N’; • scanf(“%d”, &searchElement); • for (cnt = 0; cnt < 10 && searchFlag = = ‘N’; cnt++) • { • if (array[cnt] == searchElement) • searchFlag = ‘Y’; • }
The break in a for loop • The break statement will unconditionally exit the structure it is inside of • scanf(“%d”, &searchElement); • for (cnt = 0; cnt < 10; cnt++) • { • if (array[cnt] == searchElement) { • searchFlag = ‘Y’; break; • } • }
Null statement following for • For loop does not necessarily have to have programming statements within the body of the loop • Example: • for (cnt = 0; cnt < 10; array[cnt++] = 50);
Commas in for loops • Expressions can be separated by commas • Subexpressions evaluated left to right • Write a for loop to copy the contents of a 100 element array called x into a 100 element array called y in the reverse order • for (i = 0, j = 99; i < 100; i++, j--) • { • y[i] = x[j]; • }
Nested for statements • One loop may be embedded within another loop • Nested for loops may be inefficient
Nested for example • void main() { • int i = 0, j = 0; • for (i = 0; i < 3; i++) • { • for (j = 0; j < 3; j++) • { • printf(“X”); • } • printf(“\n”); • } • }
Sample Program 4.6 #include <stdio.h> int main( void ) { int row = 1, column = 1; printf("Multiplication Table"); printf("\n--------------------"); for (; row < 6; row++) { printf("\n"); for (column = 1; column < 6; column++) printf("%d\t", row*column); } }