t05CProgramControlStandardStatements

t05CProgramControlStandardStatements - Program Control -...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Program Control - Standard C Statements www.kite-legends.blogspot.com Dale Roberts Outline This Topic Introduces selection structure if if/else repetition control structures while www.kite-legends.blogspot.com Dale Roberts Selection Structure: if if Selection structure: Used to choose among alternative courses of action Used Pseudocode: If (student’s grade is greater than or equal to 60) Print “Passed” Print If condition true If true Print statement executed and program goes on to next statement Print If false, print statement is ignored and the program goes onto the If false print next statement next Indenting makes programs easier to read C ignores whitespace characters Pseudocode statement in C: if ( grade >= 60 ) if printf( "Passed\n" ); C code corresponds closely to the pseudocode code www.kite-legends.blogspot.com Dale Roberts The if Selection Structure (cont.) (cont.) The if A decision can be made on any expression. decision zero - false false nonzero - true true Example: (3 – 4) is true grade >= 60 false true print “Passed” www.kite-legends.blogspot.com Dale Roberts Selection Structure: if/else if if/else if if: only performs an action if the condition is true if: nly true if/else: Specifies an action to be performed both when the if/else: condition is true and when it is false true false Pseudocode: If (student’s grade is greater than or equal to 60) Print “Passed” Print else Print “Failed” Note spacing/indentation conventions Note C code: if ( grade printf( printf( else printf( printf( >= 60 ) "Passed\n"); "Failed\n"); www.kite-legends.blogspot.com Dale Roberts The if/else Selection Structure (cont.) (cont.) The if Ternary conditional operator (?:) Takes three arguments (condition, value if true, value if false) true value false Creates an if/else expression. Recall that expressions are Creates expression Recall computations that yield a single value. computations Our pseudocode could be written: printf( "%s\n", grade >= 60 ? "Passed" : "Failed" ); Or it could have been written: Or grade >= 60 ? printf( “Passed\n” ) : printf( “Failed\n” grade ); www.kite-legends.blogspot.com Dale Roberts The if/else Selection Structure The if Compound statement: Compound Set of statements within a pair of braces Set Example: if ( grade if printf( printf( else { printf( printf( printf( printf( } >= 60 ) "Passed.\n" ); "Failed.\n" ); "You must take this course again.\n" ); Without the braces, Without if ( grade >= 60 ) if printf( "Passed.\n" printf( else printf( "Failed.\n" printf( printf( "You must take printf( ); ); this course again.\n" ); the statement the printf( "You must take this course again.\n" ); "You ); would be executed under every condition. would www.kite-legends.blogspot.com Dale Roberts The Essentials of Repetition Loop Group of instructions computer executes repeatedly while Group some condition remains true true 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“ www.kite-legends.blogspot.com Dale Roberts Essentials of Counter-Controlled Repetition Counter-controlled repetition requires The name of a control variable (or loop counter) The initial value of the control variable A condition that tests for the final value of the control variable (i.e., whether condition looping should continue) looping An increment (or decrement) by which the control variable is modified each An time through the loop time Example: int counter = 1; /* /* while ( counter <= 10 ) { /* while /* printf( "%d\n", counter ); printf( ++counter; /* /* } initialization */ repetition condition */ increment */ The statement int counter = 1; Names counter Names counter Declares it to be an integer Reserves space for it in memory Sets it to an initial value of 1 Sets This is not an executable statement, it is a declaration. This not www.kite-legends.blogspot.com Dale Roberts Repetition Structure: while while 1 2 3 4 5 6 7{ 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 24 25 26 27 } /* Fig. 3.6: fig03_06.c Class average program with counter-controlled repetition */ "Enter grade, -1 to end: " ); printf( #include <stdio.h> scanf( "%d", &grade ); while ( grade != -1 ) { int main() total = total + grade; counter = counter + 1; printf( "Enter grade, -1 to end: " ); int counter, grade, total, average; scanf( "%d", &grade ); } /* termination phase */ /* initialization phase */ if ( counter != 0 ) { total = 0; average = ( float ) total / counter; counter = 1; printf( "Class average is %.2f", average ); } /* processing phase */ else while ( counter <= 10 ) { printf( "No grades were entered\n" ); printf( "Enter grade: " ); scanf( "%d", &grade ); total = total + grade; Enter grade: 98 Program Output: Enter grade: 76 counter = counter + 1; } Enter grade: 71 Enter grade: 87 /* termination phase */ Enter grade: 83 printf( "Class average is %d\n", average ); Enter grade: 90 Enter grade: 57 return 0; /* indicate program ended successfully */ Enter grade: 79 Enter grade: 82 Enter grade: 94 Class average is 81 www.kite-legends.blogspot.com Dale Roberts ...
View Full Document

This note was uploaded on 12/08/2010 for the course CSE 2017 taught by Professor Kittu during the Spring '10 term at APIIT.

Ask a homework question - tutors are online