t05CProgramControlStandardStatements Pps
t05CProgramControlStandardStatements Pps
CSCI 230
Program Control
- Standard C Statements
Dale Roberts, Lecturer
IUPUI
[email protected]
Dale Roberts
Outline
This Topic Introduces
selection structure
if
if/else
Dale Roberts
Selection Structure: if
Selection structure:
Used to choose among alternative courses of action
Pseudocode:
If (students grade is greater than or equal to 60)
Print Passed
If condition true
Print statement executed and program goes on to next statement
If false, print statement is ignored and the program goes onto the
next statement
Indenting makes programs easier to read
C ignores whitespace characters
Pseudocode statement in C:
if ( grade >= 60 )
printf( "Passed\n" );
C code corresponds closely to the pseudocode
Dale Roberts
(3 4) is true
grade >= 60
true
print Passed
false
Dale Roberts
Pseudocode:
If (students grade is greater than or equal to 60)
Print Passed
else
Print Failed
C code:
if ( grade >= 60 )
printf( "Passed\n");
else
printf( "Failed\n");
Dale Roberts
Dale Roberts
>= 60 )
"Passed.\n" );
"Failed.\n" );
"You must take this course again.\n" );
);
);
this course again.\n" );
the statement
printf( "You must take this course again.\n" );
Dale Roberts
Counter-controlled repetition
Definite repetition: know how many times loop will execute
Control variable used to count repetitions
Sentinel-controlled repetition
Indefinite repetition
Used when number of repetitions not known
Sentinel value indicates "end of data
Dale Roberts
The statement
int counter = 1;
Names counter
Declares it to be an integer
Reserves space for it in memory
Sets it to an initial value of 1
This is not an executable statement, it is a declaration.
Dale Roberts
Dale Roberts