Download presentation
Presentation is loading. Please wait.
Published byGodwin Cannon Modified over 8 years ago
1
CSCI 171 Presentation 4
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
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
Example of for loop //Write the C code to display the first 3 positive integers //DEMONSTRATING A FOR LOOP #include void main() { int count = 0; for (count = 1; count <= 3; count ++) { printf(“\n%d”, count); }
7
Example of for loop //Write the C code to display the first 3 positive integers in descending order //DEMONSTRATING A FOR LOOP #include void main() { int count = 0; for (count = 3; count >= 1; count --) { printf(“\n%d”, count); }
8
Sample Program 4.1 #include int main( void ) { int index = 0; for (index = 2; index <= 40; index+=2) { printf("%d\n", index); }
9
Flexibility of for statement int count = 1; for (; count < 1000; count++) IS equivalent to: int count = 0; for (count = 1; count < 1000; count ++)
10
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); }
11
Sample Program 4.2 #include int main( void ) { int index = 0; for (index = 2; index <= 38; index+=2) printf("%d\n", index); }
12
Omission of increment void main() { int count = 0; for (count = 1; count <= 3;) { printf(“\n%d”, count); }
13
Omission of Increment - cont’d void main() { int count = 0; for (count = 1; count <= 3;) { printf(“\n%d”, count); count++; }
14
Sample Program 4.3 #include int main( void ) { int index = 0; for (index = 2; index <= 38; index+2) printf("%d ", index); }
15
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)
16
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]); }
17
Referencing elements in an array Can use variables or integers int count = 2; data[count] is equivalent to: data[2];
18
Sample Program 4.4 #include 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); }
19
Sample Program 4.5 #include 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); }
20
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’; }
21
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’; }
22
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; }
23
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);
24
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]; }
25
Nested for statements One loop may be embedded within another loop Nested for loops may be inefficient
26
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”); }
27
Sample Program 4.6 #include 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); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.