Download presentation
Presentation is loading. Please wait.
Published bySharyl Scott Modified over 9 years ago
1
CSCI 130 for Loops Chapter 7 - A
2
Execution of a C Program Execution starts in main( ) Top down style –sequential flow Unrealistic to expect sequence in more complicated programs
3
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
4
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)
5
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
6
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
7
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); } }
8
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); } }
9
Flexibility of for statement count = 1 for (; count < 1000; count++) IS equivalent to: int count; for (count = 1; count < 1000; count ++)
10
Scope of Variables Index value holds a value after loop void main() { int count; for (count = 1; count <= 3; count ++) { printf(“\n%d”, count); } }
11
Omission of increment void main() { int count; for (count = 1; count <= 3;) { printf(“\n%d”, count); } }
12
Omission of Increment - cont’d void main() { int count; for (count = 1; count <= 3;) { printf(“\n%d”, count); count++; } }
13
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)
14
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]); }
15
Referencing elements in an array Can use variables or integers int count = 3; data[count] is equivalent to: data[3];
16
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’; }
17
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);
18
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]; }
19
Nested for statements One loop may be embedded within another loop Nested for loops may be inefficient
20
Nested for example void main() { int i, j; for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { printf(“X”); } printf(“\n”); } }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.