Chapter 8 The Loops By: Mr. Baha Hanene
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
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; }
THE OUTPUT
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); }
THE OUTPUT
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);
THE OUTPUT
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); }
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); }
THE OUTPUT
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); }
THE OUTPUT
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); }
THE OUTPUT
Q: Write a program to get three numbers from the user, add them together & print their total & average.
THE OUTPUT
Q: Write a program to get three numbers from the user, add them together & print their total & average.
THE OUTPUT
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
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 : 1 2 3 4 5
Q: Write the program to read the salary of ten employees, add them and then display the total & average of all the salaries.
THE OUTPUT
Q: Write a program to display odd numbers between a range, whereas the range must be input by the user.
THE OUTPUT
To change the odd number program into even number, the following places we need to change….
Q: Write a program to display the odd or even numbers with the count of displayed numbers.
THE OUTPUT
Q: What is the output for the following program? j k l 1 2 3 2 4 6 For Loop Start i = 0 4 6 8 i = 2 6 8 10 i = 4 8 10 12 i = 6 10 12 14 i = 8 12 14 16