DOC PREVIEW
IUPUI CSCI 23000 - Program Control

This preview shows page 1-2-3-4-5-6 out of 17 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 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 17 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 17 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 17 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 17 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 17 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Slide 1OutlineRepetition Structure: forThe for Structure (cont.)The for Structure (cont.)Repetition Structure: do/whileSlide 7Multiple-Selection Structure: switchSlide 9Slide 10The break and continue Statementscontinue Statementbreak StatementEquality (==) vs. Assignment (=) OperatorsExamplesExamples:Slide 17Dale RobertsProgram Control- Additional C StatementsDepartment of Computer and Information Science,School of Science, IUPUICSCI 230Dale Roberts, LecturerDale Roberts, [email protected]@cs.iupui.eduDale RobertsOutlineOutlineThis Topic IntroducesThis Topic Introducesadditional repetition control structuresadditional repetition control structuresforfordodo//whilewhileswitchswitch additional multiple selection structure additional multiple selection structurebreakbreak statement statementUsed for exiting immediately and rapidly from certain control Used for exiting immediately and rapidly from certain control structuresstructurescontinuecontinue statement statementUsed for skipping the remainder of the body of a repetition Used for skipping the remainder of the body of a repetition structure and proceeding with the next iteration of the loopstructure and proceeding with the next iteration of the loopDale Robertsforfor loops syntax loops syntaxfor ( initialization ; loopContinuationTest ; increment ) for ( initialization ; loopContinuationTest ; increment ) statement statementExampleExample: Prints the integers from one to ten: Prints the integers from one to tenfor ( counter = 1; counter <= 10; counter++ )for ( counter = 1; counter <= 10; counter++ ) printf( "%d\n", counter );printf( "%d\n", counter );For loops can usually be rewritten as For loops can usually be rewritten as whilewhile loops: loops:initialization;initialization;whilewhile ( ( loopContinuationTestloopContinuationTest ) ) {{ statement; statement; increment; increment;} } Initialization and incrementInitialization and increment Can be comma-separated list of statementsCan be comma-separated list of statementsExampleExample::for ( i = 0, j = 0; j + i <= 10; j++, i++)for ( i = 0, j = 0; j + i <= 10; j++, i++) printf( "%d\n", j + i );printf( "%d\n", j + i );Repetition StructureRepetition Structure: : forfor No semicolon (;) after last expressionDale RobertsThe The forfor Structure Structure (cont.)(cont.) Arithmetic expressionsArithmetic expressionsInitialization, loop-continuation, and increment can contain Initialization, loop-continuation, and increment can contain arithmetic expressions. If arithmetic expressions. If xx equals equals 22 and and yy equals equals 1010 for ( j = x; j <= 4 * x * y; j += y / x ) for ( j = x; j <= 4 * x * y; j += y / x ) is equivalent tois equivalent tofor ( j = 2; j <= 80; j += 5 )for ( j = 2; j <= 80; j += 5 )Notes about the Notes about the forfor structure: structure:"Increment" may be negative (decrement)"Increment" may be negative (decrement)If the loop continuation condition is initially If the loop continuation condition is initially falsefalseThe body of the The body of the forfor structure is not performed (i.e. pre-test) structure is not performed (i.e. pre-test)Control proceeds with the next statement after the Control proceeds with the next statement after the forfor structure structureControl variableControl variableOften printed or used inside for body, but not necessarilyOften printed or used inside for body, but not necessarilyDale Roberts Sum is 25501 /* Fig. 4.5: fig04_05.c 2 Summation with for */3 #include <stdio.h>45 int main()6 {7 int sum = 0, number;89 for ( number = 2; number <= 100; number += 2 )10 sum += number;11 12 printf( "Sum is %d\n", sum );1314 return 0;15 }1. Initialize variables2. for repetition structureProgram Output:2 + 4 + 8 + … +100 = 2550The The forfor Structure Structure (cont.)(cont.)Dale RobertsThe The dodo//whilewhile repetition structure repetition structure Similar to the Similar to the whilewhile structure structuredo/while is a “do/while is a “post-testpost-test” condition. The body of the loop is ” condition. The body of the loop is performed at least once.performed at least once.All actions are performed at least onceAll actions are performed at least onceFormat:Format:do {do { statement;statement;} while ( } while ( conditioncondition ); );ExampleExample:: Prints the integers from Prints the integers from 11 to to 10.10.(letting counter = 1):(letting counter = 1):do {do { printf( "%d ", counter );printf( "%d ", counter );} while (++counter <= 10);} while (++counter <= 10);Flowchart of the Flowchart of the dodo//whilewhile repetition structure repetition structureRepetition StructureRepetition Structure: : dodo//whilewhile trueconditionaction(s)falseDale Roberts1 /* Fig. 4.9: fig04_09.c2 Using the do/while repetition structure */3 #include <stdio.h>45 int main()6 {7 int counter = 1;8 9 do {10 printf( "%d ", counter );11 } while ( ++counter <= 10 );1213 return 0;14 }1 2 3 4 5 6 7 8 9 10 1. Initialize variable1. Initialize variable2. Loop2. Loop3. Print3. PrintProgram Output:Repetition StructureRepetition Structure: : do/whiledo/whileDale RobertsMultiple-Selection StructureMultiple-Selection Structure: : switchswitch switchswitchUseful when a variable or expression is tested for all the values it can assume Useful when a variable or expression is tested for all the values it can assume and different actions are takenand different actions are takenFormatFormatSeries of Series of casecase labels and an optional labels and an optionaldefaultdefault case caseswitch (switch ( value value ){){casecase '1' '1'::actionsactionscasecase '2' '2'::actionsactionsdefault:default:actionsactions}} break;break; exits from structure exits from structureFlowchart of the Flowchart of the switchswitch structure structure truecase 1case 2falsecase n…case 1 breakfalsefalsetruecase 2 breaktruecase n breakaction(s)action(s)action(s)defaultaction(s)Dale Roberts1 /* Fig. 4.7: fig04_07.c2 Counting letter grades */3 #include <stdio.h>45 int main()6 {7 int grade;8 int aCount = 0, bCount = 0, cCount = 0, dCount = 0, 99 fCount = 0;1011 printf( "Enter the letter grades.\n" );12 printf( "Enter the EOF character to end input.\n" );1314 while ( ( grade = getchar() ) != EOF ) {1516 switch ( grade ) { /* switch nested in while */1718 case 'A': case 'a': /* grade was uppercase A */19 ++aCount; /* or


View Full Document

IUPUI CSCI 23000 - Program Control

Download Program Control
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 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 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?