1 Agenda - Loops while for for & while Nested Loops do-while Misc. & Questions.

Post on 22-Dec-2015

220 views 0 download

Transcript of 1 Agenda - Loops while for for & while Nested Loops do-while Misc. & Questions.

1

Agenda - Loops

while for for & while Nested Loops do-while Misc. & Questions

2

Loops

Used to repeat the same instructions until a stop criterion is met

C provides some flexible ways of deciding how many times to loop, or when to exit a loop

for, while, do-while loops

3

while Loops

while (condition) {

statements;}

The statements are executed as long as condition is true

When the condition is no longer true, the loop “exits”

4

Example - Factorialint i, n, fact = 1;

printf("Enter a number\n");scanf("%d", &n);

i=1; while (i<=n) {

fact = fact*i;i = i + 1;

}printf("the factorial is %d\n", fact);

5

Example – Fibonacci Series

fibonacci.c

6

Fibonacci – step by stepfib1 = 0;fib2 = 1;

printf("%d ", fib1);

while(fib2 < lim){

printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0

Screen

5

lim0

fib11

fib2---

fib_next

7

Fibonacci – step by stepfib1 = 0;fib2 = 1;

printf("%d ", fib1);

while(fib2 < lim){

printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0

Screen

5

lim0

fib11

fib2---

fib_next

8

Fibonacci – step by stepfib1 = 0;fib2 = 1;

printf("%d ", fib1);

while(fib2 < lim){

printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1

Screen

5

lim0

fib11

fib2---

fib_next

9

Fibonacci – step by stepfib1 = 0;fib2 = 1;

printf("%d ", fib1);

while(fib2 < lim){

printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1

Screen

5

lim0

fib11

fib21

fib_next

10

Fibonacci – step by stepfib1 = 0;fib2 = 1;

printf("%d ", fib1);

while(fib2 < lim){

printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1

Screen

5

lim1

fib11

fib21

fib_next

11

Fibonacci – step by stepfib1 = 0;fib2 = 1;

printf("%d ", fib1);

while(fib2 < lim){

printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1

Screen

5

lim1

fib11

fib21

fib_next

12

Fibonacci – step by stepfib1 = 0;fib2 = 1;

printf("%d ", fib1);

while(fib2 < lim){

printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1

Screen

5

lim1

fib11

fib21

fib_next

13

Fibonacci – step by stepfib1 = 0;fib2 = 1;

printf("%d ", fib1);

while(fib2 < lim){

printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1 1

Screen

5

lim1

fib11

fib21

fib_next

14

Fibonacci – step by stepfib1 = 0;fib2 = 1;

printf("%d ", fib1);

while(fib2 < lim){

printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1 1

Screen

5

lim1

fib11

fib22

fib_next

15

Fibonacci – step by stepfib1 = 0;fib2 = 1;

printf("%d ", fib1);

while(fib2 < lim){

printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1 1

Screen

5

lim1

fib11

fib22

fib_next

16

Fibonacci – step by stepfib1 = 0;fib2 = 1;

printf("%d ", fib1);

while(fib2 < lim){

printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1 1

Screen

5

lim1

fib12

fib22

fib_next

17

Fibonacci – step by stepfib1 = 0;fib2 = 1;

printf("%d ", fib1);

while(fib2 < lim){

printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1 1

Screen

5

lim1

fib12

fib22

fib_next

18

Fibonacci – step by stepfib1 = 0;fib2 = 1;

printf("%d ", fib1);

while(fib2 < lim){

printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1 1 2

Screen

5

lim1

fib12

fib22

fib_next

19

Fibonacci – step by stepfib1 = 0;fib2 = 1;

printf("%d ", fib1);

while(fib2 < lim){

printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1 1 2

Screen

5

lim1

fib12

fib23

fib_next

20

Fibonacci – step by stepfib1 = 0;fib2 = 1;

printf("%d ", fib1);

while(fib2 < lim){

printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1 1 2

Screen

5

lim2

fib12

fib23

fib_next

21

Fibonacci – step by stepfib1 = 0;fib2 = 1;

printf("%d ", fib1);

while(fib2 < lim){

printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1 1 2

Screen

5

lim2

fib13

fib23

fib_next

22

Fibonacci – step by stepfib1 = 0;fib2 = 1;

printf("%d ", fib1);

while(fib2 < lim){

printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1 1 2

Screen

5

lim2

fib13

fib23

fib_next

23

Fibonacci – step by stepfib1 = 0;fib2 = 1;

printf("%d ", fib1);

while(fib2 < lim){

printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1 1 2 3

Screen

5

lim2

fib13

fib23

fib_next

24

Fibonacci – step by stepfib1 = 0;fib2 = 1;

printf("%d ", fib1);

while(fib2 < lim){

printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1 1 2 3

Screen

5

lim2

fib13

fib25

fib_next

25

Fibonacci – step by stepfib1 = 0;fib2 = 1;

printf("%d ", fib1);

while(fib2 < lim){

printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1 1 2 3

Screen

5

lim3

fib13

fib25

fib_next

26

Fibonacci – step by stepfib1 = 0;fib2 = 1;

printf("%d ", fib1);

while(fib2 < lim){

printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1 1 2 3

Screen

5

lim3

fib15

fib25

fib_next

27

Fibonacci – step by stepfib1 = 0;fib2 = 1;

printf("%d ", fib1);

while(fib2 < lim){

printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1 1 2 3

Screen

5

lim3

fib15

fib25

fib_next

28

Fibonacci – step by stepfib1 = 0;fib2 = 1;

printf("%d ", fib1);

while(fib2 < lim){

printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1 1 2 3

Screen

5

lim3

fib15

fib25

fib_next

29

Example – Integer Division

Input: Two integers – A and B

Output: How many times A contains B (it is

the result of the integer division A/B) Do not use the operators ‘/’, ‘*’ Solution: division.c

30

Solution

int a, b, res, tmp;

printf("Please enter two numbers.\n");scanf("%d %d", &a, &b);

tmp = a;res = 0;while(tmp >= b) {

tmp = tmp - b;res = res + 1;

}

printf("%d / %d = %d\n", a, b, res);

31

Example – Power of Two

Input: integer A Output: is there an integer N such

that A == 2^N? Solution: powerOfTwo.c

32

Solution

int a, tmp;

printf("Please enter a num\n");scanf("%d", &a);

tmp = a;while((tmp > 0) && (tmp % 2 == 0)) {

tmp = tmp / 2;}

if (tmp == 1) printf("%d is a power of two\n",a);

elseprintf("%d is NOT a power of two\n",a);

33

Agenda - Loops

while for for & while Nested Loops do-while Misc. & Questions

34

for Loops

for (initiate; termination-condition; update) {body

}

1. Initiate2. If termination-condition holds:

a. Execute bodyb. Updatec. Go to step 2

35

Order of Executionfor ( i = 1; i<=10 ; i++) printf(“%d\n”, i);for ( i = 1; i<=10 ; i++) printf(“%d\n”, i);for ( i = 1; i<=10 ; i++) printf(“%d\n”, i); 1for ( i = 1; i<=10 ; i++) printf(“%d\n”, i);for ( i = 1; i<=10 ; i++) printf(“%d\n”, i);for ( i = 1; i<=10 ; i++) printf(“%d\n”, i); 2for ( i = 1; i<=10 ; i++) printf(“%d\n”, i);for ( i = 1; i<=10 ; i++) printf(“%d\n”, i); for ( i = 1; i<=10 ; i++) printf(“%d\n”, i); 3for ( i = 1; i<=10 ; i++) printf(“%d\n”, i); ……

36

Factorial (again)

int i, n, fact = 1;

printf("Enter a number\n");scanf("%d", &n);

for(i = 1; i <= n; i = i + 1)fact = fact * i;

printf("the factorial is %d\n", fact);

37

Factorial using for – step by step

#include <stdio.h>int main(void){ int i,n,fact = 1; printf("Enter a number\n"); scanf("%d", &n);

for(i=1;i<=n;i++) fact *= i;

printf("the factorial is %d\n", fact); return 0;}

---

i

3

n

1

fact

38

Factorial using for – step by step

#include <stdio.h>int main(void){ int i,n,fact = 1; printf("Enter a number\n"); scanf("%d", &n);

for(i=1;i<=n;i++) fact *= i;

printf("the factorial is %d\n", fact); return 0;}

1

i

3

n

1

fact

39

Factorial with for – step by step

#include <stdio.h>int main(void){ int i,n,fact = 1; printf("Enter a number\n"); scanf("%d", &n);

for(i=1;i<=n;i++) fact *= i;

printf("the factorial is %d\n", fact); return 0;}

1

i

3

n

1

fact

40

Factorial with for – step by step

#include <stdio.h>int main(void){ int i,n,fact = 1; printf("Enter a number\n"); scanf("%d", &n);

for(i=1;i<=n;i++) fact *= i;

printf("the factorial is %d\n", fact); return 0;}

2

i

3

n

1

fact

41

Factorial with for – step by step

#include <stdio.h>int main(void){ int i,n,fact = 1; printf("Enter a number\n"); scanf("%d", &n);

for(i=1;i<=n;i++) fact *= i;

printf("the factorial is %d\n", fact); return 0;}

2

i

3

n

2

fact

42

Factorial with for – step by step

#include <stdio.h>int main(void){ int i,n,fact = 1; printf("Enter a number\n"); scanf("%d", &n);

for(i=1;i<=n;i++) fact *= i;

printf("the factorial is %d\n", fact); return 0;}

3

i

3

n

2

fact

43

Factorial with for – step by step

#include <stdio.h>int main(void){ int i,n,fact = 1; printf("Enter a number\n"); scanf("%d", &n);

for(i=1;i<=n;i++) fact *= i;

printf("the factorial is %d\n", fact); return 0;}

3

i

3

n

6

fact

44

Factorial with for – step by step

#include <stdio.h>int main(void){ int i,n,fact = 1; printf("Enter a number\n"); scanf("%d", &n);

for(i=1;i<=n;i++) fact *= i;

printf("the factorial is %d\n", fact); return 0;}

4

i

3

n

6

fact

45

Factorial with for – step by step

#include <stdio.h>int main(void){ int i,n,fact = 1; printf("Enter a number\n"); scanf("%d", &n);

for(i=1;i<=n;i++) fact *= i;

printf("the factorial is %d\n", fact); return 0;}

4

i

3

n

6

fact

46

Example: Fahrenheit to Celsius Conversion Table/* Print a Fahrenheit-to-Celsius conversion table

*/ int fahr;double celsius; int lower = 0, upper = 300;int step = 20;

for(fahr=lower ; fahr<=upper ; fahr = fahr + step) {celsius = 5.0*(fahr -32.0)/9.0;printf("%d\t%g\n", fahr, celsius);

}

47

Agenda - Loops

while for for & while Nested Loops do-while Misc. & Questions

48

for while

for (initiate; termination-condition; update) {body;

}

initiate;while (termination-condition) {

body update;

}

49

When use for/while? Any for loop can be converted to while

loop and vice versa Some applications are more natural to

for, and others to while for is more suited when something is

performed a predefined number of times

while is more suited if the number of iterations is not known in advance (e.g., asking for legal input from a user)

50

Infinite Loops

What are they? Beware of them

51

‘break’ in Loops

When break is encountered, the loop exits regardless of whether the condition’s state

The program then continues to run from the first line after the loop

If called within a nested loop, break breaks out of the inner loop only

52

‘continue’ in Loops

When continue is encountered, the rest of the current’s loop’s iteration is ignored

The program then continues to run from the beginning of the loop

53

Agenda - Loops

while for for & while Nested Loops do-while Misc. & Questions

54

Example: Rectangle of ‘*’/* Print a rectangle of *. The height and width are defined

by the user */int i,j;int height, width;

printf("Please enter the two box dimensions: \n");scanf("%d%d",&height,&width);

for (i = 1; i <= height; i++) {for(j = 1; j <= width; j++) {

printf("*");}printf("\n");

}

55

Example

Write a program that accepts a number from the user, and prints out all of the prime numbers up to that number (Hints: nested loops, part of the solution was seen during lecture)

56

Solution (listprimes.c)int i, j, last;

printf("enter a number\n");scanf("%d", &last);for(i = 2; i <= last; i = i + 1) {

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

// i is not a primebreak;

}}if (j == i) {

// j "passed" all test and thus is a primeprintf("the number %d is prime\n", i);

}}

57

Exercise

Change the former prime-listing program, so that is displays only the largest prime number which is smaller than or equal to the user’s input

58

Solution 1 (largest_prime.c)int i, j, last;int found = 0; /* This indicates whether we found the largest

prime */

printf("enter a number\n");scanf("%d", &last);i = last;while (!found) {/* Loop until we find the required prime */

for (j = 2 ; j < i; j = j + 1) if (i % j == 0) break;

if (j == i) /* If this is true then i is prime */ found = 1;else i--;

}printf("The largest prime not larger than %d is %d.\n", last, i);

59

Solution 2 (largest_prime_2for.c)

int i, j, last;printf("enter a number\n");scanf("%d", &last);for(i = last; i > 1; i = i - 1) {

for(j = 2 ; j < i; j = j + 1) if (i % j == 0) break; // inner loop

if (j == i) /* i is prime */ break;

}printf("The largest prime not larger than %d is %d.\n", last, i);

60

Exercise

Write a program that prints an upside-down half triangle of *.

The height of the pyramid is the input.

*****

*****

****

*

61

Solution (triangle.c) #include<stdio.h>

int main(void){

int i, j, size;

printf(“Please enter a size:\n”);scanf(“%d”,&size);for (i = 1; i <= size; i++){

for(j = i; j <= size; j++)printf("*");

printf("\n");}

return 0;}

62

Agenda - Loops

while for for & while Nested Loops do-while Misc. & Questions

63

do-while Loops

do {body

} while (condition);

Similar to while loops Except the condition is evaluated after the

loop body The loop body is always executed at least

once, even if the expression is never true

64

Example: wait for legal input

int i;

printf("Please enter a positive number.\n");do { scanf("%d", &i); if (i <= 0) {

printf("That's not a positive number! Try again.\n");

}} while (i<=0);/* The program continues.... */

65

Agenda - Loops

while for for & while Nested Loops do-while Misc. & Questions

66

getchar getchar() gets a single character from

the user. Requires including stdio.h Returns a non-positive number on

failure. Similar to scanf.

char c;

c = getchar();

char c;

scanf(“%c”, &c);

====

67

putchar

putchar(‘char’) prints out the character inside the brackets.

Requires including stdio.h Similar to printf.

char c;

putchar(c);

char c;

printf(“%c”, c);====

68

More Operators

Used as a short-hand for incrementing (or decrementing) variables.i++ or ++i == i = i + 1i-- or --i == i = i – 1i += a == i = i + ai -= a == i = i - ai *= a == i = i * ai /= a == i = i / a

69

Q1:עיינו בקטע הבא וסמנו את כל התשובות הנכונות

int k = 10, sum = 0;int j;for (j = 0;j < k; j = k - 1)

sum = sum + 1;

a. After execution the value of k is 10b. This code will fail in compilationc. After execution the value of j is 9 d. There will be an infinite loope. Non of these answers is correct

70

Q2:עיינו בקטע הבא וסמנו את כל התשובות הנכונות

int sum = 0,number;for (number = 1;number <= 10; number = number

+ 1)number = number - 1;

printf(“%d”,sum);

a. This code will fail in compilationb. 0 will be printedc. 55 will be printedd. 45 will be printede. Non of these answers is correct

71

Q3:עיינו בקטע הבא וסמנו את כל התשובות הנכונות

int i;for (i = 0; i < 10; i = i + 1)

for (i = 0; i < 9; i = i + 1)System.out.println(“*”);

a. 90 ‘*’ will be printedb. There will be an infinite loopc. 10 ‘*’ will be printedd. This code will fail in compilatione. Non of these answers is correct

72

Using the Debugger