Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 8 The Loops By: Mr. Baha Hanene.

Similar presentations


Presentation on theme: "Chapter 8 The Loops By: Mr. Baha Hanene."— Presentation transcript:

1 Chapter 8 The Loops By: Mr. Baha Hanene

2 Learning Outcomes In this chapter we will cover learning outcome no. 4
Use the program control structures of Sequences, Selections and Repetitions as well as simple functions to develop C programs to meet specified tasks. L04

3 THE WHILE LOOP Initialization (counter)
THE FORMAT Initialization (counter) while (<testing> or <condition>) { body for the loop (statements) increment for the counter } EXAMPLE count = 0; while (count < = 10) { printf (“ \n %i ”, count); count++; or count=count+1; }

4 THE OUTPUT

5 THE FOR LOOP THE FORMAT for (<initialization>; <testing>; <increment>) { body for the loop (statements) } EXAMPLE for (count = 0; count < = 10; count++) { printf (“ \n %i ”, count); }

6 THE OUTPUT

7 THE DO-WHILE LOOP Initialization (counter) do {
THE FORMAT Initialization (counter) do { body for the loop (statements) increment for the counter } while (<testing> or <condition>); EXAMPLE count = 0; do { printf (“ \n %i ”, count); count++; or count=count+1; } while (count<=10);

8 THE OUTPUT

9 Comparison (While Loop Vs. For Loop)
If we want to display the numbers from 0 to 10 then both loop statements will be as below. int count = 0; while ( count < = 10 ) { printf (“ \n %i ”, count); count++; } Initialization Increment int count; for (count = 0; count < = 10; count++) { printf (“ \n %i ”, count); }

10 A Sample Program Q: Write a program to display numbers from zero to ten. #include <stdio.h> void main() { int num; for (num = 0; num < = 10; num++) printf (“ \n %d ”, num); }

11 THE OUTPUT

12 Q: Change the previous program to prompt the user to enter the last number and then display the numbers from 0 to the last number. #include <stdio.h> void main() { int num, last ; printf(“\n Enter The Last Number = ”); scanf(“%d”, & last); for (num = 0; num < = last; num++) { printf (“ \n %d ”, num); }

13 THE OUTPUT

14 Q: Again change the previous program to prompt the user to enter the first & the last number and then display the numbers accordingly. #include <stdio.h> void main() { int num, first, last ; printf(“\n Enter The First Number = ”); scanf(“%d”, & first); printf(“\n Enter The Last Number = ”); scanf(“%d”, & last) for (num = first; num < = last; num++) { printf (“ \n %d ”, num); }

15 THE OUTPUT

16 Q: Write a program to get three numbers from the user, add them together & print their total & average.

17 THE OUTPUT

18 Q: Write a program to get three numbers from the user, add them together & print their total & average.

19 THE OUTPUT

20 GUESS THE OUTPUT 1) for (a=0; a=5; a++) 3) for (a=1; a<=5; a++)
Which code from the following prints “Hello” 5 times? 1) for (a=0; a=5; a++) printf(“\n Hello”); 3) for (a=1; a<=5; a++) 2) for (a=0; a==5; a++) 4) for (a=1; a==5; a+=2) Answer : 3

21 What is the output for the following?
# include <stdio.h> void main() { int i, y, z=0; for (i=5; i<10; i++) { y = ++z; printf(“%d ”,y); } Output :

22 Q: Write the program to read the salary of ten employees, add them and then display the total & average of all the salaries.

23 THE OUTPUT

24 Q: Write a program to display odd numbers between a range, whereas the range must be input by the user.

25 THE OUTPUT

26 To change the odd number program into even number, the following places we need to change….

27 Q: Write a program to display the odd or even numbers with the count of displayed numbers.

28 THE OUTPUT

29 Q: What is the output for the following program?
j k l For Loop Start i = 0 i = 2 i = 4 i = 6 i = 8


Download ppt "Chapter 8 The Loops By: Mr. Baha Hanene."

Similar presentations


Ads by Google