72
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

  • View
    225

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 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

Page 2: 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

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

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”

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

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

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

5

Example – Fibonacci Series

fibonacci.c

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

31

Example – Power of Two

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

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

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

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

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

33

Agenda - Loops

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

Page 34: 1 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

}

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

47

Agenda - Loops

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

Page 48: 1 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;

}

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

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)

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

50

Infinite Loops

What are they? Beware of them

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

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

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

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

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

53

Agenda - Loops

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

Page 54: 1 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");

}

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

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)

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

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

}}

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

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

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

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

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

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

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

60

Exercise

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

The height of the pyramid is the input.

*****

*****

****

*

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

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

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

62

Agenda - Loops

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

Page 63: 1 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

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

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

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

65

Agenda - Loops

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

Page 66: 1 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);

====

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

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

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

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

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

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

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

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

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

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

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

72

Using the Debugger