CSCI 130 Advanced Program Control Chapter 8. Program Controls so far for loop while loop do…while...

26
CSCI 130 Advanced Program Control Chapter 8

Transcript of CSCI 130 Advanced Program Control Chapter 8. Program Controls so far for loop while loop do…while...

Page 1: CSCI 130 Advanced Program Control Chapter 8. Program Controls so far for loop while loop do…while loop.

CSCI 130

Advanced Program Control

Chapter 8

Page 2: CSCI 130 Advanced Program Control Chapter 8. Program Controls so far for loop while loop do…while loop.

Program Controls so far

• for loop

• while loop

• do…while loop

Page 3: CSCI 130 Advanced Program Control Chapter 8. Program Controls so far for loop while loop do…while loop.

The break statement

• Can be placed inside the body of a loop

• When break is encountered, loop is exited (regardless of the condition)

• Execution passes to first statement outside the loop

• A break inside a nested loop only causes exiting of the innermost loop

Page 4: CSCI 130 Advanced Program Control Chapter 8. Program Controls so far for loop while loop do…while loop.

break example

for (count = 0; count <= 10; count ++)

{

if (count == 5)

break;

}

• Without break loop iterates 11 times

• With break, loop stops during 6th iteration

Page 5: CSCI 130 Advanced Program Control Chapter 8. Program Controls so far for loop while loop do…while loop.

Another break example

Write a for loop that will search a 10 element array for the value 12.

for (i = 0; i < 10; i ++) { //for written with no break

if (arrayName[i] = 12) //this is less efficient

foundFlag = ‘Yes’; //Entire array always

} //searched

-----------------------------------------------------------------------------

for (i = 0; i < 10; i++) { //for written with break

if (arrayName[i] = 12) { //more efficient

foundFlag = ‘Yes’; //if 12 is first element

break; //than loop only entered

} //once

}

Page 6: CSCI 130 Advanced Program Control Chapter 8. Program Controls so far for loop while loop do…while loop.

The continue statement

• Can be placed within the body of a loop

• When continue encountered, control passes to the next iteration of the loop

• Statements between continue and end of loop not executed

• Significantly different than break

Page 7: CSCI 130 Advanced Program Control Chapter 8. Program Controls so far for loop while loop do…while loop.

Example of continue

for (count = 0; count <= 10; count ++)

{

if (count == 5)

continue;

printf(“%d ”, count);

}

• Loop iterates 11 times, with or without continue statement

• Output: 1 2 3 4 6 7 8 9 10– 5 is skipped

Page 8: CSCI 130 Advanced Program Control Chapter 8. Program Controls so far for loop while loop do…while loop.

Another continue example

Write the code to check an array of 100 elements. Write out only those elements in the array that are not prime

for (i = 0; i < 100; i++) {

if (numIsPrime(arrayName[i]))

continue;

printf(“\n%d”, arrayName[i]);

}

Note: numIsPrime will return a positive number for primes

Page 9: CSCI 130 Advanced Program Control Chapter 8. Program Controls so far for loop while loop do…while loop.

The goto statement

• C’s unconditional branching statement

• goto and target statement must be in the same function

• goto can always be performed using better structures (do…while, etc.)

• NEVER use a goto

Page 10: CSCI 130 Advanced Program Control Chapter 8. Program Controls so far for loop while loop do…while loop.

Example of a goto

void main ( ) { int count= 3; printf("Before any goto %d\n", count);

goto location0; printf("This will not print out\n");

location0: ; printf("At location 0\n");

location1: ; printf("At location 1\n"); }

Page 11: CSCI 130 Advanced Program Control Chapter 8. Program Controls so far for loop while loop do…while loop.

Strong suggestion about goto

NEVER

USE

A

GOTO

Page 12: CSCI 130 Advanced Program Control Chapter 8. Program Controls so far for loop while loop do…while loop.

Infinite Loops

• Condition will never be evaluated as false

• Theoretically would run forever

• Avoid infinite loops

Page 13: CSCI 130 Advanced Program Control Chapter 8. Program Controls so far for loop while loop do…while loop.

Examples of an infinite loop

for (i = 0; i < 10; i+1) //i+1 does not change

printf(“%d”, i); //the value of i

---------------------------------

while ( i < 10) { //Programmer thinks

printf(“%d”, i); //i always starts greater

i-=1; //than 10

}

----------------------------------

for (i = 1; i !=10; i +=2) //Printing out odd ints

printf(“%d”, i); //up to 10?

Page 14: CSCI 130 Advanced Program Control Chapter 8. Program Controls so far for loop while loop do…while loop.

The switch statement

• Most flexible program control statement

• Program control based on an expression with more than 2 possible values

Page 15: CSCI 130 Advanced Program Control Chapter 8. Program Controls so far for loop while loop do…while loop.

Referencing elements in an array

• General form of switch statement: switch(expression)

{

case template1:

statements;

case template2:

statements;

case templaten:

statements;

default:

statements;

}

Page 16: CSCI 130 Advanced Program Control Chapter 8. Program Controls so far for loop while loop do…while loop.

Concrete example of switch

switch(i) { case 1: printf(”The number is 1”);

case 2: printf(“The number is 2”);

case 3: printf(”The number is 3”);

default: printf(”The number is not 1, 2, or 3"); }

Page 17: CSCI 130 Advanced Program Control Chapter 8. Program Controls so far for loop while loop do…while loop.

Evaluation of a switch statement

• If expression matches a template, control passes to first statement within that template

• If no match, control passes to first statement within default

• If no match and no default, control passed to first statement after switch structure

Page 18: CSCI 130 Advanced Program Control Chapter 8. Program Controls so far for loop while loop do…while loop.

Output of switch statement switch(i) { case 1: printf(”The number is 1\n”); case 2: printf(“The number is 2\n”); case 3: printf(”The number is 3\n”); default: printf(”The number is not 1, 2, or 3\n"); }

If i = 1 Output: The number is 1 The number is 2 The number is 3 The number is not 1, 2, or 3

Page 19: CSCI 130 Advanced Program Control Chapter 8. Program Controls so far for loop while loop do…while loop.

Correct way to code a switch

switch(i) { case 1: printf(”The number is 1\n”); break; case 2: printf(“The number is 2\n”); break; case 3: printf(”The number is 3\n”); break; default: printf(”The number is not 1, 2, or 3\n"); }

Page 20: CSCI 130 Advanced Program Control Chapter 8. Program Controls so far for loop while loop do…while loop.

System functions

• All within stdlib.h file (must be included)

• exit( )– terminates execution

• atexit( )– performs functions at program termination

• system( )– executes operating system commands

Page 21: CSCI 130 Advanced Program Control Chapter 8. Program Controls so far for loop while loop do…while loop.

exit ( ) function

• Terminates program execution #include <stdio.h> #include <stdlib.h>

void main ( ) { char i; exit(0);

printf("Enter a character"); //These statements will scanf("%c", &i); //not be executed }

Page 22: CSCI 130 Advanced Program Control Chapter 8. Program Controls so far for loop while loop do…while loop.

exit ( ) function continued

• If 0 is passed into function it means program executed normally

• If 1 is passed into function it means program abnormally terminated

• stdlib.h has two symbolic constanst:– #define EXIT_SUCCESS 0– #define EXIT_FAILURE 1

• can call exit(EXIT_SUCCESS)• can call exit(EXIT_FAILURE)

Page 23: CSCI 130 Advanced Program Control Chapter 8. Program Controls so far for loop while loop do…while loop.

atexit ( ) function

• Specifies one (or more) functions that are automatically executed at termination time

• Up to 32 functions can be registered in this way

• Executed in reverse order

Page 24: CSCI 130 Advanced Program Control Chapter 8. Program Controls so far for loop while loop do…while loop.

atexit( ) function continued

#include <stdio.h> #include <stdlib.h>

void cleanup(); void cleanupLast();

void main ( ) { char i; atexit(cleanupLast); atexit(cleanup); printf("Enter a character"); scanf("%c", &i); }

Page 25: CSCI 130 Advanced Program Control Chapter 8. Program Controls so far for loop while loop do…while loop.

system ( ) function

• Executes operating system commands• Example:

system(“c:\dir *.exe”);

• Must capture any output in a file– will not open up another console– will not print to current console

• Can execute any command line system(“c:\winnt\system32\notepad.exe”);

Page 26: CSCI 130 Advanced Program Control Chapter 8. Program Controls so far for loop while loop do…while loop.

system( ) does not work in CW

• system( ) function does not work in Code Warrior

• Help states:

“The system() function is an empty function that is included in the Metrowerks stdlib.h to conform to the ANSI C Standard Library specification.”