Top Banner
CS201 - Repetition loops
29

CS201 - Repetition loops. Types Of Loops Counting Loop (while, for) Sentinel-Controlled Loop (while, for) Endfile-Controlled Loop (while, for) Input Validation.

Dec 19, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CS201 - Repetition loops. Types Of Loops Counting Loop (while, for) Sentinel-Controlled Loop (while, for) Endfile-Controlled Loop (while, for) Input Validation.

CS201 - Repetition

loops

Page 2: CS201 - Repetition loops. Types Of Loops Counting Loop (while, for) Sentinel-Controlled Loop (while, for) Endfile-Controlled Loop (while, for) Input Validation.

Types Of Loops

Counting Loop (while, for) Sentinel-Controlled Loop (while,

for) Endfile-Controlled Loop (while, for) Input Validation Loop (do-while) General Conditional Loop (while,

for)

Page 3: CS201 - Repetition loops. Types Of Loops Counting Loop (while, for) Sentinel-Controlled Loop (while, for) Endfile-Controlled Loop (while, for) Input Validation.

While Loop (same as Java) while (condition) { Do Something } When condition is true, Do

Something and return to while. When condition is false, skip and

exit loop. i = 0; while ( i<10 ) { printf(“%d “, i); i=i+1;}

Page 4: CS201 - Repetition loops. Types Of Loops Counting Loop (while, for) Sentinel-Controlled Loop (while, for) Endfile-Controlled Loop (while, for) Input Validation.

0

1

2

3

4

5

6

7

8

9

Page 5: CS201 - Repetition loops. Types Of Loops Counting Loop (while, for) Sentinel-Controlled Loop (while, for) Endfile-Controlled Loop (while, for) Input Validation.

For Loop (almost like Java)

No internal type statement allowed! for (initialization; test; update) { Do

Something } for(i=0; i<10; i=i+1) { printf(“%d\

n”,i); } (same output) for (int j=0; j<10; j=j+1) { … }

NOT ALLOWED IN C !

Page 6: CS201 - Repetition loops. Types Of Loops Counting Loop (while, for) Sentinel-Controlled Loop (while, for) Endfile-Controlled Loop (while, for) Input Validation.

Interpretation First the initialization expression(s) is(are)

executed. Then the test expression(s) is(are)

evaluated. If true, statements are executed, update

expression(s) is(are) executed, and test is evaluated again.

If false, loop exits. Last expression controls multiple expressions.

Page 7: CS201 - Repetition loops. Types Of Loops Counting Loop (while, for) Sentinel-Controlled Loop (while, for) Endfile-Controlled Loop (while, for) Input Validation.

What Prints?

int i=0, j=0;

for (i=0, j=-2; i<8, j<2; i=i+1, j=j+2)

printf(“%d %d\n”, i,j);

Page 8: CS201 - Repetition loops. Types Of Loops Counting Loop (while, for) Sentinel-Controlled Loop (while, for) Endfile-Controlled Loop (while, for) Input Validation.

What Prints?

int i=0, j=0;

for (i=0, j=-2; i<8, j<2; i=i+1, j=j+2)

printf(“%d %d\n”, i,j);

0 -2

1 0

Page 9: CS201 - Repetition loops. Types Of Loops Counting Loop (while, for) Sentinel-Controlled Loop (while, for) Endfile-Controlled Loop (while, for) Input Validation.

Do While Loop (just like Java)

Do {something} while ( expression);

Always does “something” at least once!

Test expression. If true, return and to something

again. If false, exit loop at that point.

Page 10: CS201 - Repetition loops. Types Of Loops Counting Loop (while, for) Sentinel-Controlled Loop (while, for) Endfile-Controlled Loop (while, for) Input Validation.

What prints?

int i=0;

do

{

i=i+3;

printf(“%d\n”, i);

} while (i<12);

Page 11: CS201 - Repetition loops. Types Of Loops Counting Loop (while, for) Sentinel-Controlled Loop (while, for) Endfile-Controlled Loop (while, for) Input Validation.

What prints?

int i=1;

do

{

i=i+3;

printf(“%d\n”, i);

} while (i<12);

4

7

10

13

Page 12: CS201 - Repetition loops. Types Of Loops Counting Loop (while, for) Sentinel-Controlled Loop (while, for) Endfile-Controlled Loop (while, for) Input Validation.

Compound Assignment

Like Java. x += 3; is the same as x = x + 3; In general: var op = expression; Is the same as var = var op expression; += -= *= /= %=

Page 13: CS201 - Repetition loops. Types Of Loops Counting Loop (while, for) Sentinel-Controlled Loop (while, for) Endfile-Controlled Loop (while, for) Input Validation.

Increment and Decrement Operators

Like Java Prefix and postfix versions ++i i++ i++; is the same as i = i + 1; j--; is the same as j = j – 1; However k=++i; is different than

k=i++;

Page 14: CS201 - Repetition loops. Types Of Loops Counting Loop (while, for) Sentinel-Controlled Loop (while, for) Endfile-Controlled Loop (while, for) Input Validation.

Prefix increment

What prints?

int i=0, j=0;

printf(“%d\n”, i++);

printf(%d\n”,++i);

j = i++;

printf(“%d\n,j);

Page 15: CS201 - Repetition loops. Types Of Loops Counting Loop (while, for) Sentinel-Controlled Loop (while, for) Endfile-Controlled Loop (while, for) Input Validation.

Prefix increment

What prints?

int i=0, j=0;

printf(“%d\n”, i++);

printf(%d\n”,++i);

j = i++;

printf(“%d\n,j);

0

2

2

What’s the value of i here?

Page 16: CS201 - Repetition loops. Types Of Loops Counting Loop (while, for) Sentinel-Controlled Loop (while, for) Endfile-Controlled Loop (while, for) Input Validation.

Prefix increment

What prints?

int i=0, j=0;

printf(“%d\n”, i++);

printf(%d\n”,++i);

j = i++;

printf(“%d\n,j);

0

2

2

What’s the value of i here?

3

Page 17: CS201 - Repetition loops. Types Of Loops Counting Loop (while, for) Sentinel-Controlled Loop (while, for) Endfile-Controlled Loop (while, for) Input Validation.

Sentinel-Controlled Loops

Loops that run as long as needed. Signal the end of the loop with a

special value (negative for ages, etc.)

Get a line of data While the sentinel value has not been

encountered Process the data line. Get another line of data.

Page 18: CS201 - Repetition loops. Types Of Loops Counting Loop (while, for) Sentinel-Controlled Loop (while, for) Endfile-Controlled Loop (while, for) Input Validation.

What prints?int i, sum=0;

scanf(“%d”,&i);

while(i!=-9)

{

sum+=i;

scanf(“%d”,&i);

}

printf(“%d\n”, sum);

/* input = 1 3 5 7 -9 */

Page 19: CS201 - Repetition loops. Types Of Loops Counting Loop (while, for) Sentinel-Controlled Loop (while, for) Endfile-Controlled Loop (while, for) Input Validation.

What prints?int i, sum=0;

scanf(“%d”,&i);

while(i!=-9)

{

sum+=i;

scanf(“%d”,&i);

}

printf(“%d\n”, sum);

/* input = 1 3 5 7 -9 */

16

Page 20: CS201 - Repetition loops. Types Of Loops Counting Loop (while, for) Sentinel-Controlled Loop (while, for) Endfile-Controlled Loop (while, for) Input Validation.

What prints?int i, sum=0;

scanf(“%d”,&i);

while(i!=-9)

{

sum+=i;

scanf(“%d”,&i);

}

printf(“%d\n”, sum);

/* input = -9 */

Page 21: CS201 - Repetition loops. Types Of Loops Counting Loop (while, for) Sentinel-Controlled Loop (while, for) Endfile-Controlled Loop (while, for) Input Validation.

What prints?int i, sum=0;

scanf(“%d”,&i);

while(i!=-9)

{

sum+=i;

scanf(“%d”,&i);

}

printf(“%d\n”, sum);

/* input = -9 */

0

Page 22: CS201 - Repetition loops. Types Of Loops Counting Loop (while, for) Sentinel-Controlled Loop (while, for) Endfile-Controlled Loop (while, for) Input Validation.

EOF Many library functions return helpful

data as the value of the function? For example, scanf returns the

number of successful conversions. File functions can return a value equal

to EOF (a special defined variable). You can send this to scanf with ctrl-D

(linux)

Page 23: CS201 - Repetition loops. Types Of Loops Counting Loop (while, for) Sentinel-Controlled Loop (while, for) Endfile-Controlled Loop (while, for) Input Validation.

What prints?

int i, sum=0,status=0;

status = scanf(“%d”,&i);

while(status!=EOF)

{

sum+=i;

status = scanf(“%d”,&i);

}

printf(“%d\n”, sum);

/* input = */

2

4

6

ctl-d

Page 24: CS201 - Repetition loops. Types Of Loops Counting Loop (while, for) Sentinel-Controlled Loop (while, for) Endfile-Controlled Loop (while, for) Input Validation.

What prints?

int i, sum=0,status=0;

status = scanf(“%d”,&i);

while(status!=EOF)

{

sum+=i;

status = scanf(“%d”,&i);

}

printf(“%d\n”, sum);

/* input = */

2

4

6

ctl-d

12

Page 25: CS201 - Repetition loops. Types Of Loops Counting Loop (while, for) Sentinel-Controlled Loop (while, for) Endfile-Controlled Loop (while, for) Input Validation.

Nested Loops

Usually used to work with two dimensional arrays (later).

Simply put the definition for one loop in the body of another loop.

Page 26: CS201 - Repetition loops. Types Of Loops Counting Loop (while, for) Sentinel-Controlled Loop (while, for) Endfile-Controlled Loop (while, for) Input Validation.

What Prints?

int a=0, b=0, sum=0;

for(a=0; a<6; a+=2)

for(b=0; b>4; b--)

sum=sum+1;

printf(“%d”,sum);

Page 27: CS201 - Repetition loops. Types Of Loops Counting Loop (while, for) Sentinel-Controlled Loop (while, for) Endfile-Controlled Loop (while, for) Input Validation.

What Prints?

int a=0, b=0, sum=0;

for(a=0; a<6; a+=2)

for(b=0; b>-4; b--)

sum=sum+1;

printf(“%d”,sum);

Page 28: CS201 - Repetition loops. Types Of Loops Counting Loop (while, for) Sentinel-Controlled Loop (while, for) Endfile-Controlled Loop (while, for) Input Validation.

Debugging Use a source level debugger as part

of your IDE. Use a command line debugger like

gdb. Add printf statements to trace

execution. (Be careful when removing the

printf statements that you don’t introduce errors.)

Page 29: CS201 - Repetition loops. Types Of Loops Counting Loop (while, for) Sentinel-Controlled Loop (while, for) Endfile-Controlled Loop (while, for) Input Validation.

∞ Loops It’s hard to distinguish between a

complex calculation loop and an infinite loop without debugging statements.