csci 130 advanced program control chapter 8. program controls so far for loop while loop do…while...
TRANSCRIPT
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
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
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
}
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
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
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
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
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"); }
Strong suggestion about goto
NEVER
USE
A
GOTO
Infinite Loops
• Condition will never be evaluated as false
• Theoretically would run forever
• Avoid infinite loops
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?
The switch statement
• Most flexible program control statement
• Program control based on an expression with more than 2 possible values
Referencing elements in an array
• General form of switch statement: switch(expression)
{
case template1:
statements;
case template2:
statements;
…
case templaten:
statements;
default:
statements;
}
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"); }
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
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
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"); }
System functions
• All within stdlib.h file (must be included)
• exit( )– terminates execution
• atexit( )– performs functions at program termination
• system( )– executes operating system commands
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 }
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)
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
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); }
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”);
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.”