AEEE 195 – Repetition Structures: Part A

1 / 11

# AEEE 195 – Repetition Structures: Part A - PowerPoint PPT Presentation

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) ‏

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about 'AEEE 195 – Repetition Structures: Part A' - gerda

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
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)‏
• 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.
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.

for Loop Flowchart

1. Priming

Set counter=1

2. Test

counter

<= 10

TRUE

3a. print counter

3b. Update counter++;

FALSE

Infinite Loop
• You can still end up with an infinite loop when using for loops

for (counter=0; counter<=10; counter--)‏

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
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)‏

For Loop Variations
• by using commas, you can put more than one statement in any expression

for (i=100, y=0; i>=1; i--)‏

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

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