1 / 11

AEEE 195 – Repetition Structures: Part A

Spring semester 2011. AEEE 195 – Repetition Structures: Part A. Outline. “For” loop “Nested for” Examples. “ for ” Repetition Structure. Another type of loop in C is the “for” loop. It is very good for definite loops (the number of iterations is known) ‏

gerda
Download Presentation

AEEE 195 – Repetition Structures: Part A

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Spring semester 2011 AEEE 195 – Repetition Structures: Part A

  2. Outline “For” loop “Nested for” Examples

  3. “for” Repetition Structure • Another type of loop in C is the “for” loop. • It is very good for definite loops (the number of iterations is known)‏ • All the parts (priming, testing and updating) are in one place. • format: for (prime expression; test expression; update expression)‏ • Since the expressions are all in one place, many people prefer “for” to “while” when the number of iterations is known.

  4. Basic For Loop Syntax • “For” loops are good for creating definite loops (the number of iterations is known). int counter; for (counter =1;counter <= 10;counter++)‏ printf ("%d\n", counter); 1. Priming: Set the start value. 2. Test Condition: Set the stop value. 3. Update: Update the value. Note that each section is separated by a semicolon.

  5. for Loop Flowchart 1. Priming Set counter=1 2. Test counter <= 10 TRUE 3a. print counter 3b. Update counter++; FALSE

  6. Infinite Loop • You can still end up with an infinite loop when using for loops for (counter=0; counter<=10; counter--)‏

  7. For Loop Variations • Increment may be negative: for (i=100; i>=1; i--)‏ • This counts from 100 to 1. • Increment may be (negative and) greater than 1: for (i=100; i>=5; i-=5)‏ • This counts from 100 to 5 in steps of 5

  8. For Loop Variations • any expression may be more complex: for (i=100*y; i>=1; i--)‏ for (i=100; i>=y/2; i--)‏ for (i=100; i>=1; i-=4*y)‏

  9. For Loop Variations • by using commas, you can put more than one statement in any expression for (i=100, y=0; i>=1; i--)‏

  10. Nested For Loops • It is also possible to place a for loop inside another for loop. int rows, columns; for (rows=1; rows<=5; rows++) { for (columns=1; columns<=10; columns++) printf ("*"); printf ("\n"); } Output: ********** ********** ********** ********** ********** Outer Loop Inner Loop

  11. Nested For Loops: Example #2 #include <stdio.h> int main ()‏ { int rows, columns; for (rows=1; rows<=5; rows++) { for (columns=1; columns<=rows; columns++)‏ printf ("*"); printf ("\n"); } return 0; } Output: * ** *** **** ***** Outer Loop Inner Loop

More Related