LOOP (Part 2) for while do-while 1. TK1913-C Programming2 TK1913-C Programming 2 Loop : for Loop :...

36
LOOP LOOP (Part 2) (Part 2) for while do-while 1
  • date post

    21-Dec-2015
  • Category

    Documents

  • view

    236
  • download

    2

Transcript of LOOP (Part 2) for while do-while 1. TK1913-C Programming2 TK1913-C Programming 2 Loop : for Loop :...

LOOPLOOP(Part 2)(Part 2)

forwhile

do-while

1

TK1913-C ProgrammingTK1913-C Programming 22

Loop : Loop : forfor

Condition is tested first Loop is controlled by a counter Syntaxes

for (initial value ; condition; update counter) statement;

Or

for (initial value ; condition; update counter) {statement;statement;

}

TK1913-C ProgrammingTK1913-C Programming 33

ExampleExample

Write a program which does the following:

Reads 5 integers and displays the sum of all

integers

Input example: 3 6 4 1 2

Output example: 16

TK1913-C ProgrammingTK1913-C Programming 44

Recall the flowchartRecall the flowchart

counter ← 1, sum ← 0

counter < 6

sum←sum+ x

false

true

counter++

output sum

input x

TK1913-C ProgrammingTK1913-C Programming 55

Note the initial Note the initial value of ivalue of i

and conditionand condition

i ← 1, sum ← 0

i < 6

sum←sum+ x

false

true

i++

output sum

input xHow many times How many times does the loop get does the loop get

executed? executed?

TK1913-C ProgrammingTK1913-C Programming 66

i ← 0, sum ← 0

i < 6

sum←sum+ x

false

true

i++

output sum

input xHow many times How many times does the loop get does the loop get

executed? executed?

TK1913-C ProgrammingTK1913-C Programming 77

i ← 0, sum ← 0

i < 5

sum←sum+ x

false

true

i++

output sum

input xHow many times How many times does the loop get does the loop get

executed? executed?

TK1913-C ProgrammingTK1913-C Programming 88

The C statements:

int x, sum, i;sum = 0;for (i = 0; i < 5; i++) {

scanf(“%d”,&x);sum = sum + x;

}printf(“%d”,sum);

TK1913-C ProgrammingTK1913-C Programming 99

i ← 0, sum ← 0

i < 5

sum←sum+ x

false

true

i++

output sum

input x

int x, sum, i;sum = 0;for (i = 0; i < 5; i++) {

scanf(“%d”,&x);sum = sum + x;

}printf(“%d”,sum);

TK1913-C ProgrammingTK1913-C Programming 1010

forfor statement statement

Example:

for ( num = 1; num <= 3; num++ ) printf(“%d\t”, num);

num

???

_

1 _

TK1913-C ProgrammingTK1913-C Programming 1111

Example:

for (num = 1; num <= 3; num++ ) printf(“%d\t”, num);

num

1

_

forfor statement statement

TK1913-C ProgrammingTK1913-C Programming 1212

Example:

for (num = 1; num <= 3; num++ ) printf(“%d\t”, num);

num

1

_

forfor statement statement

TK1913-C ProgrammingTK1913-C Programming 1313

Example:

for (num = 1; num <= 3; num++ ) printf(“%d\t”, num);

num

1

1 _

forfor statement statement

TK1913-C ProgrammingTK1913-C Programming 1414

Example:

for (num = 1; num <= 3; num++ ) printf(“%d\t”, num);

num

2

1 _

forfor statement statement

TK1913-C ProgrammingTK1913-C Programming 1515

Example:

for (num = 1; num <= 3; num++ ) printf(“%d\t”, num);

num

2

1 _

forfor statement statement

TK1913-C ProgrammingTK1913-C Programming 1616

forfor statement statement

Example:

for (num = 1; num <= 3; num++ ) printf(“%d\t”, num);

bil

2

1 2 _

TK1913-C ProgrammingTK1913-C Programming 1717

Example:

for (num = 1; num <= 3; num++ ) printf(“%d\t”, num);

num

3

1 2 _

forfor statement statement

TK1913-C ProgrammingTK1913-C Programming 1818

Example:

for (num = 1; num <= 3; num++ ) printf(“%d\t”, num);

num

3

1 2 _

forfor statement statement

TK1913-C ProgrammingTK1913-C Programming 1919

Example:

for (num = 1; num <= 3; num++ ) printf(“%d\t”, num);

num

3

1 2 3 _

forfor statement statement

TK1913-C ProgrammingTK1913-C Programming 2020

Example:

for (num = 1; num <= 3; num++ ) printf(“%d\t”, num);

num

4

1 2 3 _

forfor statement statement

TK1913-C ProgrammingTK1913-C Programming 2121

Example:

for (num = 1; num <= 3; num++ ) printf(“%d\t”, num);

num

4

1 2 3 _

forfor statement statement

TK1913-C ProgrammingTK1913-C Programming 2222

forwhile

do-while

TK1913-C ProgrammingTK1913-C Programming 2323

Loop: whileLoop: while

Condition is tested first Loop is controlled by condition or a counter Syntax

while (condition) statement;

Orwhile (condition) {

statement;statement;

}

TK1913-C ProgrammingTK1913-C Programming 2424

Recall this example:

Given an exam marks as input, display the appropriate message based on the rules below:

If marks is greater than 49, display “PASS”, otherwise display “FAIL”

However, for input outside the 0-100 range, display “WRONG INPUT” and prompt

the user to input again until a valid input is entered

TK1913-C ProgrammingTK1913-C Programming 2525

false

true

input m

m<0 || m>100

m>49 “PASS”

“FAIL”

true

false

“WRONG INPUT”

input m

Exercise:

Convert this flowchart to a C program

TK1913-C ProgrammingTK1913-C Programming 2626

int marks;scanf(“%d”,&marks);while (marks<0) | | (marks>100) {

printf(“WRONG INPUT”);scanf(“%d”,&marks);

}if (marks>49) {

printf(“PASS”);else

printf(“FAIL”);}

DoubleSelection

TK1913-C ProgrammingTK1913-C Programming 2727

ExerciseExercise Given a set of integers

with the last one being 999 Display the summation of

all the integers.

Input example: 1 3 23 999

Output example: Sum = 27

Draw the flowchart for this problem

TK1913-C ProgrammingTK1913-C Programming 2828

Sentinel-controlled loop

true

x!=999

sum←sum+x

false

input x

sum=0

input x

output sum

#include <stdio.h>

void main() {

int sum, x;

sum = 0;

scanf(“%d”, &x);

while (x != 999) {

sum = sum + x;

scanf(“%d”, &x);

}

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

}

TK1913-C ProgrammingTK1913-C Programming 2929

int sum, x; sum = 0; scanf(“%d”, &x); while (x != 999) { sum = sum + x; scanf(“%d”, &x); } printf(“\nThe sum : %d\n”, sum);

?

?x

sum 0

1

_1

1 != 999

0+11

1 3

33 != 999

1+34

1 3 23

2323 != 999

4+2327

1 3 23 999

999999 != 999

The sum : 27

TK1913-C ProgrammingTK1913-C Programming 3030

Do-while LoopDo-while Loop Statements in the loop are executed first (at least

onc, and condition is tested last Loop is controlled by a condition or counter Syntax

do { statement;statement;

} while (condition);statement;

TK1913-C ProgrammingTK1913-C Programming 3131

do-while statementdo-while statement

Example :printf(“Input start and end value : “);scanf(“%d %d”, &start, &end);do {

printf(“%c (%d)\n“, start, start);start++;

} while (start <= end) ;

_

???

start

???

end

Input start and end value : _Input start and end value : 65 67_

65 67

Input start and end value : 65 67A (65)_

66

66 <= 67

Input start and end value : 65 67A (65)B (66)_

67 <= 67

67

Input start and end value : 65 67A (65)B (66)C (67)_

68

68 <= 67

TK1913-C ProgrammingTK1913-C Programming 3232

continuecontinue statement statement

Example:

for ( i = 0; i <= 5; i++ ) { if ( i % 2 )

continue; else

printf(“%d is an even number. ”, i); printf(“Print iff even ! \n”);}

i i <= 5 i % 2

_

0 0 <= 9 true 01 <= 5 true1 2 <= 5 true3 <= 5 true4 <= 5 true5 5 <= 5 true 16 6 <= 5 false234 1010

0 is an even number._0 is an even number. Print iff even !

_

0 is an even number. Print iff even !

2 is an even number._

0 is an even number. Print iff even !

2 is an even number. Print iff even !

_

0 is an even number. Print iff even !

2 is an even number. Print iff even !

4 is an even number._

0 is an even number. Print iff even !

2 is an even number. Print iff even !

4 is an even number. Print iff even !

_

TK1913-C ProgrammingTK1913-C Programming 3333

Now what can you conclude about continue?

TK1913-C ProgrammingTK1913-C Programming 3434

breakbreak statement statement

printf(“Input a value between 1 – 7: ”);scanf(“%d”,&value);for (i = 1; i <= 7; i++) {

if ( i = = value ) break;printf(“\n%d”,i);

}printf(“YeePee! I’m out of the loop!\n”);

value

???

i i <= 7 i == value

Input a value between 1-7: _Input a value between 1-7: 4

1 1 <= 7 true 1 == 4 false

4

Input a value between 1-7: 4

1

2 2 <= 7 true 2 == 4 false

Input a value between 1-7: 4

1

2

3 3 <= 7 true 3 == 4 false

Input a value between 1-7: 4

1

2

3

4 4 <= 7 true 4 == 4 true

Input a value between 1-7: 5

1

2

3YeePee! I’m out of the loop!

TK1913-C ProgrammingTK1913-C Programming 3535

Now what can you conclude about

break?

TK1913-C ProgrammingTK1913-C Programming 3636

End of 1End of 1stst half of the semester half of the semester

Yes !! That’s all? Yes !! That’s all? What’s next???What’s next???

1 week HOLIDAY1 week HOLIDAYon the way … on the way …