CSCI 130 for Loops Chapter 7 - A. Execution of a C Program Execution starts in main( ) Top down...

20
CSCI 130 for Loops Chapter 7 - A

Transcript of CSCI 130 for Loops Chapter 7 - A. Execution of a C Program Execution starts in main( ) Top down...

Page 1: 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.

CSCI 130

for Loops

Chapter 7 - A

Page 2: 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.

Execution of a C Program

• Execution starts in main( )

• Top down style– sequential flow

• Unrealistic to expect sequence in more complicated programs

Page 3: 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.

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

Page 4: 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.

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)

Page 5: 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.

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

Page 6: 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.

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

Page 7: 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.

Example of for loop

Write the C code to display the first 3 numbers out to the console

//DEMONSTRATING A FOR LOOP

#include <stdio.h>

void main() {

int count;

for (count = 1; count <= 3; count ++)

{

printf(“\n%d”, count);

}

}

Page 8: 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.

Example of for loop

Write the C code to display the first 3 numbers out to the console

//DEMONSTRATING A FOR LOOP

#include <stdio.h>

void main() {

int count;

for (count = 3; count >= 1; count --)

{

printf(“\n%d”, count);

}

}

Page 9: 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.

Flexibility of for statement

count = 1

for (; count < 1000; count++)

IS equivalent to:

int count;

for (count = 1; count < 1000; count ++)

Page 10: 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.

Scope of Variables

• Index value holds a value after loop

void main() {

int count;

for (count = 1; count <= 3; count ++)

{

printf(“\n%d”, count);

}

printf(“\n%d”, count);

}

Page 11: 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.

Omission of increment

void main() {

int count;

for (count = 1; count <= 3;)

{

printf(“\n%d”, count);

}

}

Page 12: 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.

Omission of Increment - cont’d

void main() {

int count;

for (count = 1; count <= 3;)

{

printf(“\n%d”, count);

count++;

}

}

Page 13: 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.

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)

Page 14: 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.

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]);

}

Page 15: 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.

Referencing elements in an array

• Can use variables or integers

int count = 3;

data[count]

is equivalent to: data[3];

Page 16: 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.

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’;

}

Page 17: 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.

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);

Page 18: 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.

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]; }

Page 19: 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.

Nested for statements

• One loop may be embedded within another loop

• Nested for loops may be inefficient

Page 20: 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.

Nested for example

void main() { int i, j; for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { printf(“X”); } printf(“\n”); } }