Sie sind auf Seite 1von 13

3.

6 The ifelse Selection Statement


Block:
Compound statements with declarations

Syntax errors
Caught by compiler

Logic errors:
Have their effect at execution time Non-fatal: program runs, but has incorrect output Fatal: program exits prematurely
1

4.9 The break and continue Statements


continue

Skips the remaining statements in the body of a while, for or dowhile statement
Proceeds with the next iteration of the loop
while and dowhile

Loop-continuation test is evaluated immediately after the continue statement is executed


for

Increment expression is executed, then the loopcontinuation test is evaluated


2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

/* Fig. 4.12: fig04_12.c Using the continue statement in a for statement */ #include <stdio.h> /* function main begins program execution */ int main() { int x; /* counter */ /* loop 10 times */ for ( x = 1; x <= 10; x++ ) { /* if x is 5, continue with next iteration of loop */ if ( x == 5 ) { continue; /* skip remaining code in loop body */ } /* end if */ printf( "%d ", x ); /* display value of x */ } /* end for */ printf( "\nUsed continue to skip printing the value 5\n" ); return 0; /* indicate program ended successfully */

Outline
fig04_12.c

25 } /* end function main */

1 2 3 4 6 7 8 9 10 Used continue to skip printing the value 5 Copyright 19922004 by Deitel & Associates, Inc. and Pearson

Program Output

4.11Confusing Equality (==) and Assignment (=) Operators


Dangerous error
Does not ordinarily cause syntax errors Any expression that produces a value can be used in control structures Nonzero values are true, zero values are false Example using ==:
if ( payCode == 4 )

printf( "You get a bonus!\n" );

Checks payCode, if it is 4 then a bonus is awarded


4

4.11Confusing Equality (==) and Assignment (=) Operators


Example, replacing == with =:
if ( payCode = 4 ) printf( "You get a bonus!\n" );

This sets payCode to 4 4 is nonzero, so expression is true, and bonus awarded no matter what the payCode was

Logic error, not a syntax error

5.4 Functions
Functions
Modularize a program All variables defined inside functions are local variables
Parameters
Known only in function defined Communicate information between functions Local variables

Benefits of functions
Divide and conquer Software reusability

Manageable program development Use existing functions as building blocks for new programs Abstraction - hide internal details (library functions)

Avoid code repetition

5.12
File scope

Scope Rules

Identifier defined outside function, known in all functions Used for global variables, function definitions, function prototypes

Function scope
Can only be referenced inside a function body Used only for labels (start:, case: , etc.)

5.12
Block scope

Scope Rules

Identifier declared inside a block


Block scope begins at definition, ends at right brace

Used for variables, function parameters (local variables of function) Outer blocks "hidden" from inner blocks if there is a variable with the same name in the inner block

Function prototype scope


Used for identifiers in parameter list

1 2 3 4 5 6 7 8 9 10

/* Fig. 5.12: fig05_12.c A scoping example */ #include <stdio.h> void useLocal( void ); void useGlobal( void ); /* function prototype */ /* function prototype */

void useStaticLocal( void ); /* function prototype */

int x = 1; /* global variable */

11 /* function main begins program execution */ 12 int main() 13 { 14 15 16 17 18 19 20 21 22 23 24 25 printf( "local x in outer scope of main is %d\n", x ); printf( "local x in inner scope of main is %d\n", x ); } /* end new scope */ { /* start new scope */ int x = 7; /* local variable to new scope */ printf("local x in outer scope of main is %d\n", x ); int x = 5; /* local variable to main */

fig05_12.c (Part 1 of 3)

26 27 28 29 30 31 32 33 34 35 36

useLocal(); useGlobal(); useLocal(); useGlobal();

/* useLocal has automatic local x */ /* useGlobal uses global x */ /* useLocal reinitializes automatic local x */ /* global x also retains its value */

useStaticLocal(); /* useStaticLocal has static local x */

useStaticLocal(); /* static local x retains its prior value */

printf( "local x in main is %d\n", x ); return 0; /* indicates successful termination */

37 } /* end main */ 38 39 /* useLocal reinitializes local variable x during each call */ 40 void useLocal( void ) 41 { 42 43 44 45 46 printf( "\nlocal x in a is %d after entering a\n", x ); x++; printf( "local x in a is %d before exiting a\n", x ); int x = 25; /* initialized each time useLocal is called */

fig05_12.c (Part 2 of 3)

47 } /* end function useLocal */ 48

49 /* useStaticLocal initializes static local variable x only the first time 50 51 53 { 54 55 56 57 58 59 printf( "\nlocal static x is %d on entering b\n", x ); x++; printf( "local static x is %d on exiting b\n", x ); /* initialized only first time useStaticLocal is called */ static int x = 50; the function is called; value of x is saved between calls to this function */

52 void useStaticLocal( void )

60 } /* end function useStaticLocal */ 61 63 void useGlobal( void ) 64 { 65 66 67

fig05_12.c (Part 3 of 3)

62 /* function useGlobal modifies global variable x during each call */

printf( "\nglobal x is %d on entering c\n", x ); x *= 10; printf( "global x is %d on exiting c\n", x );

68 } /* end function useGlobal */

local x in outer scope of main is 5 local x in inner scope of main is 7 local x in outer scope of main is 5 local x in a is 25 after entering a local x in a is 26 before exiting a local static x is 50 on entering b local static x is 51 on exiting b global x is 1 on entering c global x is 10 on exiting c local x in a is 25 after entering a Program local x in a is 26 before exiting a local static x is 51 on entering b local static x is 52 on exiting b global x is 10 on entering c global x is 100 on exiting c local x in main is 5

Output

6.4 Examples Using Arrays


Initializers
int n[ 5 ] = { 1, 2, 3, 4, 5 };

If not enough initializers, rightmost elements become 0


int n[ 5 ] = { 0 }

All elements 0

If too many a syntax error is produced syntax error C arrays have no bounds checking

If size omitted, initializers determine it


int n[ ] = { 1, 2, 3, 4, 5 };
13

5 initializers, therefore 5 element array

Das könnte Ihnen auch gefallen