CSCI 130 for Loops Chapter 7 - A
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 (CIS 101)
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
Pseudocode for for loop Write the pseudocode to display the first 3 numbers out to the console DOWHILE index = 1 TO 3 OUTPUT index ENDO
Example of for loop Write the C code to display the first 3 numbers out to the console //DEMONSTRATING A FOR LOOP #include void main() { int count; for (count = 1; count <= 3; count ++) { printf(“\n%d”, count); } }
Example of for loop Write the C code to display the first 3 numbers out to the console //DEMONSTRATING A FOR LOOP #include void main() { int count; for (count = 3; count >= 1; count --) { printf(“\n%d”, count); } }
Flexibility of for statement count = 1 for (; count < 1000; count++) IS equivalent to: int count; for (count = 1; count < 1000; count ++)
Scope of Variables Index value holds a value after loop void main() { int count; for (count = 1; count <= 3; count ++) { printf(“\n%d”, count); } }
Omission of increment void main() { int count; for (count = 1; count <= 3;) { printf(“\n%d”, count); } }
Omission of Increment - cont’d void main() { int count; for (count = 1; count <= 3;) { printf(“\n%d”, count); count++; } }
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[10]; data[0] = 3; data[4] = 7; for (i = 0; i < 10; i++) { printf(“%d “, data[i]); }
Referencing elements in an array Can use variables or integers int count = 3; data[count] is equivalent to: data[3];
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: scanf(“%d”, &searchElement); for (cnt = 0; cnt < 10 && searchFlag = = ‘N’; cnt++) { if (array[cnt] == searchElement) searchFlag = ‘Y’; }
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, j; for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { printf(“X”); } printf(“\n”); } }