1 / 26

Program Control in C: Loop Statements, Break, Continue, and Switch

Learn about loop statements (for, while, do-while), break and continue statements, and switch statements in C programming. Understand their usage and examples.

dkinney
Télécharger la présentation

Program Control in C: Loop Statements, Break, Continue, and Switch

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSCI 130 Advanced Program Control Chapter 8

  2. Program Controls so far • for loop • while loop • do…while loop

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

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

  5. 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 • }

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

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

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

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

  10. 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"); • }

  11. Strong suggestion about goto NEVER USE A GOTO

  12. Infinite Loops • Condition will never be evaluated as false • Theoretically would run forever • Avoid infinite loops

  13. 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?

  14. The switch statement • Most flexible program control statement • Program control based on an expression with more than 2 possible values

  15. Referencing elements in an array • General form of switch statement: • switch(expression) • { • case template1: • statements; • case template2: • statements; • … • case templaten: • statements; • default: • statements; • }

  16. 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"); • }

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

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

  19. 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"); • }

  20. System functions • All within stdlib.h file (must be included) • exit( ) • terminates execution • atexit( ) • performs functions at program termination • system( ) • executes operating system commands

  21. 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 • }

  22. 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)

  23. 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

  24. 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); • }

  25. 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”);

  26. 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.”

More Related