{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# loops1-2 - Computer Programming TA C162 Repetition in...

This preview shows pages 1–9. Sign up to view the full content.

1 Computer Programming TA C162 Second Semester 2008-2009 Repetition in Programs Iterative Statements in C for While Do-while

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 Computer Programming TA C162 Second Semester 2008-2009 Problem1: Adding first N integers Algorithm Steps: 1. Initialize sum=0, term=0 and count=1 2. Input N from user 3. If count >N then goto Step 5 4. term =count sum = sum + term count=count+1 goto step 3 5. Print sum 6. Stop Note: Step 4 will be repeated until condition at Step 3 not satisfied. Need iterative construct to implement the algorithm
3 Computer Programming TA C162 Second Semester 2008-2009 for Statement Structure: for (init; test-condn; incr/decr) { loop_body; } Executes loop body as long as test evaluates to TRUE (non-zero). 1. Initialization 2. Conditional Testing 3. Updation of loop control variable Either 1 or 2 or 3 or ALL of them can be optional Loop body can have a single statement or a group of statements. init test loop_body updation F T

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
4 Computer Programming TA C162 Second Semester 2008-2009 Loop Construct for series problems /* 1+2+3+4+……. .N terms */ for(count=1; count<=N; count++){ term = count; sum = sum + term;} /* 1+3+5+7+…….N terms */ for(count=1; count<=N; count++){ term = 2*count-1; sum = sum + term;} /* -1+2-3+4-5+6-……N terms */ for(count=1; count<=N; count++){ term = pow(-1,count)*count; sum = sum + term;}
5 Computer Programming TA C162 Second Semester 2008-2009 Single statement in Loop Body sum = 0; for (i = 1; i <= 10; i++) sum + = i; i = 1; sum = 0; for ( ; i <= 10; i++) sum + = i; i = 1; sum = 0 for ( ; i < 10 ; ) sum + = i++;

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
6 Computer Programming TA C162 Second Semester 2008-2009 More Examples i=1; sum = 0; for( ; ; ) sum + = i++; How many times loop statement will be executed? for ( i = 0,j = 7 ; i < j; i++, j-- ) printf(“%d %d”,i,j); Output??? for (i = 0,j = 7; i < j; i++, j--); printf(“%d %d”,i,j); Output???
7 Computer Programming TA C162 Second Semester 2008-2009 Block of statement in Loop Body Option1: for (i = 0, sum = 0; i < 10; i++) { sum = sum + i; printf(“%d”, sum); } Option2: i = 0; sum = 0; for ( ; i<10;i++) { sum + = i; printf(“%d”, sum); }

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
8 Computer Programming TA C162 Second Semester 2008-2009 Few more Examples /* -- what is the output of this loop? -- */
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 26

loops1-2 - Computer Programming TA C162 Repetition in...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online