spring semester 2011 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
AEEE 195 – Repetition Structures: Part A PowerPoint Presentation
Download Presentation
AEEE 195 – Repetition Structures: Part A

Loading in 2 Seconds...

play fullscreen
1 / 11

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


  • 121 Views
  • Uploaded on

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

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

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


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.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
Outline

“For” loop

“Nested for”

Examples

for repetition structure
“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
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
for Loop Flowchart

1. Priming

Set counter=1

2. Test

counter

<= 10

TRUE

3a. print counter

3b. Update counter++;

FALSE

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

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

for loop variations
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 variations1
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 variations2
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
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
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