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

29
CS201 - Repetition loops
  • date post

    19-Dec-2015
  • Category

    Documents

  • view

    256
  • download

    3

Transcript of CS201 - Repetition loops. Types Of Loops Counting Loop (while, for) Sentinel-Controlled Loop (while,...

CS201 - Repetition

loops

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)

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;}

0

1

2

3

4

5

6

7

8

9

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 !

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.

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

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

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.

What prints?

int i=0;

do

{

i=i+3;

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

} while (i<12);

What prints?

int i=1;

do

{

i=i+3;

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

} while (i<12);

4

7

10

13

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; += -= *= /= %=

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++;

Prefix increment

What prints?

int i=0, j=0;

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

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

j = i++;

printf(“%d\n,j);

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?

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

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.

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 */

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

What prints?int i, sum=0;

scanf(“%d”,&i);

while(i!=-9)

{

sum+=i;

scanf(“%d”,&i);

}

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

/* input = -9 */

What prints?int i, sum=0;

scanf(“%d”,&i);

while(i!=-9)

{

sum+=i;

scanf(“%d”,&i);

}

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

/* input = -9 */

0

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)

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

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

Nested Loops

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

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

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

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

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

∞ Loops It’s hard to distinguish between a

complex calculation loop and an infinite loop without debugging statements.