Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

CHAPTER IV: Looping Statements

In order to repeat a statement multiple times, a control structure known as loops are to be used. Loop allows a set of instructions to repeat until a certain condition is reached. C supports the same type of loop as other modern structured languages. A for-loop executes a set statement s a fixed number of times. It is used typically when you known in advanced how many times you want to execute these statements. C looping statements are written in the following format: a. for-loop for (initialization; condition; incrementation) { <statements/s>; }

where: Initialization an assignment statement that is used to set the loop control variable. Condition a relational expression that determines when the loop will exist by testing the loop-control variable against some values. Incrementation defines how the loop-control variable will change each time the loop is repeated. In an event-controlled loop, the number of times that the loop is executed is not predetermined. Instead, termination of the loop is triggered by the occurrence of some event. The while loop and do/while loop does this kind of operation. b. while-loop while (<condition>) <statement/s>; }

where: statement can be an empty statement, a single statement or a block of statement that is to be repeated.

40

conditions may be any valid expression. The loop iterates while the condition is true and program control passes to the next line following the loop if the condition is false. Another way of creating a loop that is not counter controlled is to use the do-while loop. It is convenient when at least one repetition of loop body must be ensured. c. do/while-loop do{ <statement/s>; } while (<condition>); Unlike the for and while loop, the do/while lop checks the condition at the bottom of the loop. This means that a do/while loop while always execute at least once. Its common use is in a menu selection routine by testing a valid response at the bottom of the loop, where can be reprompt the user until a valid response is entered. Nested loop When one loop is inside of another, the loop is said to be nested. It provides a means of solving some interesting programming problems. It is sometimes important to note how much iteration a nested loop executes. This number is determined by multiplying the number of times the outer loop iterates by the number of times the inner loop repeats each time it is executed. Example 4.1. Create a program that will display integers 1 to 10 on the screen using for loops. #include<stdio.h> int ctr; main() { clrscr(); for (ctr=1; ctr<=10; ctr++) printf("%d \n",ctr); getch(); }

41

Example 4.2. Make a program that will display integers 25, 21, 17, 13, 9 using while loops. #include<stdio.h> int ctr; main() { clrscr(); ctr=25; while (ctr>=9) { printf("%d \n",ctr); ctr = ctr - 4; } getch(); } Example 4.3. Design a C code that will display all numbers divisible by 11 from 1 to 100 using do-while loops. #include<stdio.h> int ctr; main() { clrscr(); ctr=100; do { if (ctr % 11 == 0) printf("%d \n",ctr); ctr = ctr - 1; }while(ctr>=1); getch(); } Example 4.4. Display the following using nested loops: * * * * * * * * * * * * * * * #include <stdio.h> #include <conio.h> int a ; int b; void main() {

42

for (a = 1; a <= 5; a++) { for (b = 1; b <=a; b++) printf( * ); printf(\n); } } Example 4.5. Create a C program that will input any integer from 1 to 7 and display its factorial using looping. #include<stdio.h> int num,ctr,factorial; main() { clrscr(); printf("Enter number [1-7]: "); scanf("%d",&num); if (num>=1 && num<=7) { factorial=1; for(ctr=num; ctr>=1; ctr--) { factorial=factorial * ctr; } printf("Factorial is %d",factorial); } else printf("Overflow!"); getch(); }

43

Machine Problems:
4.1. Write a code for the following a. 9 b. 117 c. 12 104 15 91 18 78 21 65 24 52 27 39 30 26 33 13 36 0 display using for loops. 1 1 2 4 7 11 16 22 29 38

44

4.2. Design a code for a. 14 b. 13 12 11 10 9 8 7 6 5

the following display using while loops. 2 c. 1 6 1 12 2 20 3 30 5 42 8 56 13 72 21 90 34 100 55

45

4.3. Create a code for a. 100 b. 82 66 52 40 30 22 16 12 10

the following display using do-while loops 6 c. 64 16 32 25 16 33 8 40 9 46 27 51 81 55 243 58 729 60 2187

46

4.4. Build a code for the following display using nested loops. a. ***** b. * **** ** *** *** ** **** * *****

c.

5 5 5 5 5

4321 432 43 4

d.

1 1 1 1 1

2345 234 23 2

47

4.5. Using nested loops make a program that will do the following displays. a. 1 b. 2 2 3 3 4 2 3 4 4 6 5 3 4 6 5 9 6

48

4.6. Create a C program that will display I will pass PRGLF 10 times using for loops.

4.7. Design a program using C that input 20 integers and display the sum using for loops.

49

4.8. Write a code that will loop by inputting any integer and displays whether the integer is POSITIVE or NEGATIVE, and terminate the loop if the integer is equal to zero using while loops.

4.9. Construct a C program that will terminate the loop if the entered password is CORRECT, using do-while.

50

4.10. Create a program that will loop by inputting any character, and terminate the loop if PRGLF are entered consecutively. (Using dowhile).

4.11. Build a program that will display all prime numbers from 1 to 1000 using looping.

51

4.12. Design a program that will input integer and display it in reverse using looping.

52

You might also like