DOC PREVIEW
IUPUI CSCI 23000 - Program Control - Standard C Statements

This preview shows page 1-2-3 out of 10 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Slide 1OutlineSelection Structure: ifThe if Selection Structure (cont.)Selection Structure: if/elseThe if/else Selection Structure (cont.)The if/else Selection StructureThe Essentials of RepetitionEssentials of Counter-Controlled RepetitionRepetition Structure: whileDale RobertsProgram Control- Standard C StatementsDepartment of Computer and Information Science,School of Science, IUPUICSCI 230Dale Roberts, LecturerDale Roberts, [email protected]@cs.iupui.eduDale RobertsOutlineOutlineThis Topic IntroducesThis Topic Introducesselection structureselection structureififif/elseif/elserepetition control structuresrepetition control structureswhilewhileDale RobertsSelection StructureSelection Structure: : ifif Selection structure: Selection structure: Used to choose among alternative courses of actionUsed to choose among alternative courses of actionPseudocode:Pseudocode:If (If (student’s grade is greater than or equal to 60student’s grade is greater than or equal to 60)) Print “Passed”Print “Passed”If condition If condition truetrue Print statement executed and program goes on to next statementPrint statement executed and program goes on to next statementIf If falsefalse, print statement is ignored and the program goes onto the , print statement is ignored and the program goes onto the next statementnext statementIndenting makes programs easier to readIndenting makes programs easier to readC ignores whitespace charactersC ignores whitespace charactersPseudocode statement in C:Pseudocode statement in C:if ( grade >= 60 ) if ( grade >= 60 ) printf( "Passed\n" ); printf( "Passed\n" ); C code corresponds closely to the pseudocodeC code corresponds closely to the pseudocodeDale RobertsThe The ifif Selection Structure Selection Structure (cont.)(cont.)A decision can be made on any expression. A decision can be made on any expression. zero - zero - falsefalse nonzero - nonzero - truetrueExampleExample::(3 – 4)(3 – 4) is is true truetruefalsegrade >= 60print “Passed”Dale RobertsSelection StructureSelection Structure: : ifif//elseelse ifif//elseelse if: oif: only performs an action if the condition is nly performs an action if the condition is truetrue if/else:if/else: Specifies an action to be performed both when the Specifies an action to be performed both when the condition is condition is truetrue and when it is and when it is falsefalsePseudocode:Pseudocode:If (If (student’s grade is greater than or equal to 60student’s grade is greater than or equal to 60)) Print “Passed”Print “Passed”elseelse Print “Failed” Print “Failed” Note spacing/indentation conventionsNote spacing/indentation conventionsC code:C code:if ( grade >= 60 )if ( grade >= 60 ) printf( "Passed\n");printf( "Passed\n");elseelse printf( "Failed\n");printf( "Failed\n");Dale RobertsThe The ifif//elseelse Selection Structure Selection Structure (cont.)(cont.)Ternary conditional operator (Ternary conditional operator (?:?:) ) Takes three arguments (condition, value if Takes three arguments (condition, value if truetrue, value if , value if falsefalse))Creates an if/else Creates an if/else expressionexpression. Recall that expressions are . Recall that expressions are computations that yield a single value.computations that yield a single value.Our pseudocode could be written:Our pseudocode could be written:printf( "%s\n", grade >= 60 ? "Passed" : "Failed" );printf( "%s\n", grade >= 60 ? "Passed" : "Failed" ); Or it could have been written:Or it could have been written:grade >= 60 ? printf( “Passed\n” ) : printf( “Failed\n” grade >= 60 ? printf( “Passed\n” ) : printf( “Failed\n” ); );Dale RobertsThe The ifif//elseelse Selection Structure Selection StructureCompound statement: Compound statement: Set of statements within a pair of bracesSet of statements within a pair of bracesExample:Example:if ( grade >= 60 ) if ( grade >= 60 ) printf( "Passed.\n" );printf( "Passed.\n" );else {else { printf( "Failed.\n" );printf( "Failed.\n" ); printf( "You must take this course again.\n" );printf( "You must take this course again.\n" );} } Without the braces,Without the braces,if ( grade >= 60 ) if ( grade >= 60 ) printf( "Passed.\n" );printf( "Passed.\n" );elseelse printf( "Failed.\n" );printf( "Failed.\n" );printf( "You must take this course again.\n" ); printf( "You must take this course again.\n" ); the statementthe statementprintf(printf( "You must take this course again.\n""You must take this course again.\n" );); would be executed under every condition.would be executed under every condition.Dale RobertsThe Essentials of RepetitionThe Essentials of RepetitionLoopLoopGroup of instructions computer executes repeatedly while Group of instructions computer executes repeatedly while some condition remains some condition remains truetrueCounter-controlled repetitionCounter-controlled repetitionDefinite repetition: know how many times loop will executeDefinite repetition: know how many times loop will executeControl variable used to count repetitionsControl variable used to count repetitionsSentinel-controlled repetitionSentinel-controlled repetitionIndefinite repetitionIndefinite repetitionUsed when number of repetitions not knownUsed when number of repetitions not knownSentinel value indicates "end of data“Sentinel value indicates "end of data“Dale RobertsEssentials of Counter-Controlled RepetitionEssentials of Counter-Controlled RepetitionCounter-controlled repetition requiresCounter-controlled repetition requiresThe name of a control variable (or loop counter)The name of a control variable (or loop counter)The initial value of the control variableThe initial value of the control variableA condition that tests for the final value of the control variable (i.e., whether A condition that tests for the final value of the control variable (i.e., whether looping should continue)looping should continue)An increment (or decrement) by which the control variable is modified each An increment (or decrement) by which the control variable is modified each time through the looptime through the loopExampleExample:: int counter = 1; int counter = 1; /* initialization *//* initialization */while ( counter <= 10 ) { while ( counter <= 10 ) { /* repetition condition *//* repetition condition */ printf( "%d\n", counter );printf( "%d\n", counter ); ++counter; ++counter; /* increment *//*


View Full Document

IUPUI CSCI 23000 - Program Control - Standard C Statements

Download Program Control - Standard C Statements
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Program Control - Standard C Statements and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Program Control - Standard C Statements 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?