Chaper_4 - The break and continue Statement Real World...

Info iconThis preview shows pages 1–14. Sign up to view the full content.

View Full Document Right Arrow Icon
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
The break and continue Statement Real World Application: Generating Prime Numbers The switch Statement The goto Statement and Labels The Cast Operator Real World Application: Summing a Series The sizeof Operator getchar and putchar
Background image of page 2
Exits from innermost while, do while, for loop and switch statement { for loop{ break; } } break
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
#include <stdio.h> main ( ) { int I; for ( I = 1; I <= 3; I++) { printf ( “%d\n”, I ); if ( I == 2 ) break; /* exit from for loop */ printf ( “bottom of loop\n”); } printf ( “out of loop” ); } The output is 1 bottom of loop 2 out of loop
Background image of page 4
#include <stdio.h> main ( ) { int I; for ( I = 1; I <= 3; I++) { printf ( “%d\n”, I ); if ( I == 2 ) break; /* exit from for loop */ printf ( “bottom of loop\n”); } printf ( “out of loop” ); } The output is 1 bottom of loop 2 out of loop
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
In while loop: jumps to the top and tests expression In do while loop: jumps to the bottom and tests expression In for loop: jumps to expression3 and tests expression2 Continue stays within the loop but jumps to the next round
Background image of page 6
#include <stdio.h> main ( ) { int I; for ( I = 1; I <= 3; I++) { printf ( “%d\n”, I ); if ( I == 2 ) continue; /* jump to execute expression 3 (I++) */ printf ( “bottom of loop\n”); } printf ( “out of loop” ); } The output is ?????
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Example Computes the average of the positive numbers in the standard input. #include <stdio.h> main ( ) { float x, sum = 0.0; int count = 0; if ( x < 0.0 ) continue; /* skip nonpositive input */ sum += x; count++; } if ( count > 0 ) printf ( “\naverage = %f\n”, sum / count ); else printf ( “\nno positive numbers read\n”); }
Background image of page 8
Exercises 4.1.1 for (I=1; I<=6;I++) { if( I%2 ) continue; else printf(“d\n”,I); printf(“last line\n”) }
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 last line 4 last line 6 last line
Background image of page 10
Exercise 4.1.2 for (I=1; I<=6; I++) { if ( I%2) printf(“%d\n”,I); else break; printf(“last line\n”) }
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
1 last line
Background image of page 12
Real World Application: Generating Prime Numbers Problem Write a program that prints all positive prime integers less than or equal to n. (A positive integer I is
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 14
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 32

Chaper_4 - The break and continue Statement Real World...

This preview shows document pages 1 - 14. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online