33
AEEE 195 – Repetition Structures: Part B Spring semester 2011

AEEE 195 – Repetition Structures: Part B Spring semester 2011

Embed Size (px)

Citation preview

Page 1: AEEE 195 – Repetition Structures: Part B Spring semester 2011

AEEE 195 – Repetition Structures: Part B

Spring semester 2011

Page 2: AEEE 195 – Repetition Structures: Part B Spring semester 2011

2

Outline Repetition structures definition

“while” loop

“do/while” loop

Inifite loop

Arithmetic operators

Examples

Page 3: AEEE 195 – Repetition Structures: Part B Spring semester 2011

3

Repetition Structures

Are structures that allow you to repeat execution of the same code

That is, you can execute particular statements more than once in a controlled fashion

Statements are executed as long as some condition remains true

Also known as “loops” Sometimes referred to as “iterations”

Page 4: AEEE 195 – Repetition Structures: Part B Spring semester 2011

4

“while” Repetition Structure

Actions will be performed as long as some condition is true

For example (pseudocode),

while (something is true){ do this (again and again...)

}

Can have various kinds of conditions tested

Page 5: AEEE 195 – Repetition Structures: Part B Spring semester 2011

5

Parts of a While Loop

Every while loop will always contain three main elements: Priming: initialize your variables. Testing: test against some known condition. Updating: update the variable that is tested.

Page 6: AEEE 195 – Repetition Structures: Part B Spring semester 2011

6

Simple While Loop

#include <stdio.h>#define MAX 10main (){

int index =1;  while (index <= MAX) {

printf ("Index: %d\n", index);index = index + 1;

}}

OUTPUT:

Index: 1

Index: 2

Index: 3

Index: 4

Index: 5

Index: 6

Index: 7

Index: 8

Index: 9

Index: 10

1. Priming1. Priming

2. Test 2. Test ConditionCondition

3. Update3. Update

Page 7: AEEE 195 – Repetition Structures: Part B Spring semester 2011

7

While Loop Flowchart

1. Priming1. Priming

Set index=1Set index=1

2. Test2. Test

index index

<= 10<= 10

3a. print index3a. print index

3b. Update index = index 3b. Update index = index + 1;+ 1;

TRUETRUE

FALSEFALSE

next iterationnext iteration

Page 8: AEEE 195 – Repetition Structures: Part B Spring semester 2011

8

Infinite Loop

Infinite Loop: A loop that never ends. Generally, you want to avoid these! There are special cases, however, when you do

want to create infinite loops on purpose.

Page 9: AEEE 195 – Repetition Structures: Part B Spring semester 2011

9

Infinite Loop: Example #1

#include <stdio.h>

#define MAX 10

main ()

{

int index =1;

  while (index <= MAX)

{

printf ("Index: %d\n", index);

}

} Here, part 3 is deleted : the index = index + 1 statement.Here, part 3 is deleted : the index = index + 1 statement.

Index: 1

Index: 1

Index: 1

Index: 1

Index: 1

… [forever]

Page 10: AEEE 195 – Repetition Structures: Part B Spring semester 2011

10

Infinite Loop: Example #2

#include <stdio.h>/*no MAX here*/ main (){

int index = 1;  while (index > 0)

{printf ("Index: %d\n", index);index = index + 1;

}}

Here, part 2 has changed : Here, part 2 has changed : the test condition.the test condition.

Index: 1

Index: 2

Index: 3

Index: 4

Index: 5

… [forever]

Page 11: AEEE 195 – Repetition Structures: Part B Spring semester 2011

11

“do/while” Repetition Structure Similar to the while structure But instead of testing the condition at the top,

test is at the bottom of the loop Used when you want to execute the loop AT

LEAST ONCE

Page 12: AEEE 195 – Repetition Structures: Part B Spring semester 2011

12

do/while pseudocode

do

{

something

} while (condition is true) ;

Note the semicolon at the end

Page 13: AEEE 195 – Repetition Structures: Part B Spring semester 2011

13

do/while example

int iCounter = 1; do { printf( “%d “, iCounter ); iCounter = iCounter + 1;

} while ( iCounter <= 10 ) ;

Page 14: AEEE 195 – Repetition Structures: Part B Spring semester 2011

14

do/while loop Flowchart1. Priming

Set index=1;

3. Test

index

<= 10

2a. print index

2b. Update index = index + 1;

TRUETRUE

FALSEFALSE

Page 15: AEEE 195 – Repetition Structures: Part B Spring semester 2011

15

Example: while vs do-while structures

int num;

printf(“Give a positive number: );

scanf(“%d”, &num);

while(num <=0)

{

printf(“Give a positive number: );

scanf(“%d”, &num);

}

printf(“The number is %d\n”, num);

int num;

do

{

printf(“Give a positive number: );

scanf(“%d”, &num);

} while(num <=0);

printf(“The number is %d\n”, num);

Page 16: AEEE 195 – Repetition Structures: Part B Spring semester 2011

16

Shortcuts

C provides abbreviations for some common operations Assignment operators Increment/Decrement operators

Page 17: AEEE 195 – Repetition Structures: Part B Spring semester 2011

17

Assignment Operators

Abbreviations are provided for the basic binary operations Addition Subtraction Multiplication Division Modulus (%)

Page 18: AEEE 195 – Repetition Structures: Part B Spring semester 2011

18

Addition Assignment Operator Instead of writing

iCount = iCount + 1; You can write

iCount += 1 ; Adds the value of the expression on the right

to the variable on the left and stores the new total in the variable on the left

Page 19: AEEE 195 – Repetition Structures: Part B Spring semester 2011

19

Subtraction Assignment Operator Instead of writing

iCount = iCount - 1; You can write

iCount -= 1 ; Subtracts the value of the expression on the

right from the variable on the left and stores the new total in the variable on the left

Page 20: AEEE 195 – Repetition Structures: Part B Spring semester 2011

20

Multiplication Assignment Operator Instead of writing iCount = iCount * 2; You can write iCount *= 2; Multiplies the variable on the left by the value

of the expression on the right and stores the new total in the variable on the left

Page 21: AEEE 195 – Repetition Structures: Part B Spring semester 2011

21

Division Assignment Operator Instead of writing

iCount = iCount / 2; You can write

iCount /= 2; Divides the variable on the left by the value of

the expression on the right and stores the new total in the variable on the left

Page 22: AEEE 195 – Repetition Structures: Part B Spring semester 2011

22

Modulus Assignment Operator Instead of writing iSeconds = iSeconds % 60; You can write iSeconds %= 60; Takes the remainder of dividing the variable

on the left by the value of the expression on the right, and stores the new total in the variable on the left

Page 23: AEEE 195 – Repetition Structures: Part B Spring semester 2011

23

Examples

Assume variable int c = 10;

3 to cc = c % 7;c %= 7;10%=

2 to cc = c / 5;c /= 5;10/=

60 to cc = c * 6;c *= 6;10*=

7 to cc = c – 3;c -= 3;10-=

17 to cc = c + 7;c += 7;10+=

AssignsMeaningSample

Initial Value Operator

Page 24: AEEE 195 – Repetition Structures: Part B Spring semester 2011

24

Increment and Decrement Operators C provides unary increment operators ++ and

decrement operators –- (no spaces between them)

Increment operators add 1 Decrement operators subtract 1 Not for other assignment operators (*, /, %)

Page 25: AEEE 195 – Repetition Structures: Part B Spring semester 2011

25

Increment Operator ++

Instead of writing iCount = iCount + 1; or

iCount += 1; You can write

iCount++ ; (post-increment) or

++iCount ; (pre-increment)

Page 26: AEEE 195 – Repetition Structures: Part B Spring semester 2011

26

Post-increment

The ++ operator is after the variable

Causes the initial value of the variable to be used in the expression where it appears, and THEN adds the 1 to the variable

For example,int iCount = 5;printf( “%d\n”, iCount++ );

Would print 5… but iCount is incremented to 6 after the statement

Page 27: AEEE 195 – Repetition Structures: Part B Spring semester 2011

27

Pre-increment

The ++ operator is before the variable

Adds 1 to the initial value of the variable BEFORE it is used in the expression where it appears

For example,int iCount = 5;printf( “%d\n”, ++iCount);

Would print 6… and iCount is incremented to 6 after the statement

Page 28: AEEE 195 – Repetition Structures: Part B Spring semester 2011

28

More examples ++post-increment vs. pre-increment

int iTotal = 0;int iCount = 5;

iTotal = iCount++ * 2;printf( “%d\n”, iTotal);printf( “%d\n”, iCount);

Would print 10 for iTotal and then 6 for iCount

iTotal = ++iCount * 2;printf( “%d\n”, iTotal);printf( “%d\n”, iCount);

Would print 14 for iTotal and then 7 for iCount

Page 29: AEEE 195 – Repetition Structures: Part B Spring semester 2011

29

Decrement Operator --

Similar to increment in syntax/operation Instead of writing iCount = iCount - 1; or

iCount -= 1; You can write

iCount-- ; or --iCount ;

Page 30: AEEE 195 – Repetition Structures: Part B Spring semester 2011

30

Post-decrement

The -- operator is after the variable Causes the initial value of the variable to be used in

the expression where it appears, and THEN subtracts the 1 from the variable

For example,int iCount = 5;printf( “%d\n”, iCount-- );

Would print 5… but iCount is decremented to 4 after the statement

Page 31: AEEE 195 – Repetition Structures: Part B Spring semester 2011

31

Pre-decrement

The -- operator is before the variable Subtracts 1 from the initial value of the variable

BEFORE it is used in the expression where it appears

For example,int iCount = 5;printf( “%d\n”, --iCount);

Would print 4… and iCount is decremented to 4 after the statement

Page 32: AEEE 195 – Repetition Structures: Part B Spring semester 2011

32

More examples --post-decrement vs. pre-decrement

int iTotal = 0;int iCount = 5;

iTotal = iCount-- * 2;printf( “%d\n”, iTotal);printf( “%d\n”, iCount);

Would print 10 for iTotal and then 4 for iCount

iTotal = --iCount * 2;printf( “%d\n”, iTotal);printf( “%d\n”, iCount);

Would print 6 for iTotal and then 3 for iCount

Page 33: AEEE 195 – Repetition Structures: Part B Spring semester 2011

33

Summary Table

Subtract 1 from c THEN use new value of c in

expression

--c--

Use value of c in expression THEN subtract 1 from c

c----

Add 1 to c THEN use the new value of c in expression

++c++

Use value of c in expression THEN add 1 to c

c++++

ExplanationSampleOperator