#include <stdio.h>
main () {
int count1, count2;
for(count1 = 1, count2 = 0; count1 <=100; count1++) {
printf(“Enter %d Count2 : “, count1);
scanf(“%d”, &count2);
if(count2 == 100) break;
} }
A sample output is shown below:
Enter 1 count2 : 10 Enter 2 count2 : 20 Enter 3 count2 : 100
In the above code, the user can enter 100 values for j. However, if 100 is entered, the loop ends and control is passed to the next statement.
Another point to be remembered while using a break is that it causes an exit from an inner loop. This means that if a for loop is nested within another for loop, and a break statement is encountered in the inner loop, the control is passed back to the outer for loop.
.2.4 The ‘continue’ Statement
The continue statement causes the next iteration of the enclosing loop to being. When this statement is met in the program, the remaining statements in the body of the loop are skipped and the control is passed to the re-initialization step.
In case of the for loop, continue causes the increment portions of the loop and then the conditional test to be executed. In case of the while and do...while loops, program control passes to the conditional tests. For example:
Example 13:
#include <stdio.h>
main ()
Session
Loop
Concepts
{
int num;
for(num = 1; num <=100; num++) {
if (num % 9 == 0) continue;
printf(“%d\t”, num);
} }
The above prints all numbers from 1 to 100, which are not divisible by 9. The output will look somewhat like this.
1 2 3 4 5 6 7 8 10 11 12 13 14 15 16 17
19 20 21 22 23 24 25 26 28 29 30 31 32 33 34 35 37 38 39 40 41 42 43 44 46 47 48 49 50 51 52 53 55 56 57 58 59 60 61 62 64 65 66 67 68 69 70 71 73 74 75 76 77 78 79 80 82 83 84 85 86 87 88 89 91 92 93 94 95 96 97 98 100
.3 The ‘exit()’ function
The exit() function is a standard C library function. Its working is similar to the working of a jump statement, the major difference being that the jump statements are used to break out of a loop, whereas exit() is used to break out of the program. This function causes immediate ending of the program and control is transferred back to the operating system. The exit() function is usually used to check if a mandatory condition for a program execution is satisfied or not. The general syntax of the exit() function is
exit(int return_code);
where, return_code is optional. Zero is generally used as a return_code to indicate normal program ending. Other values indicate some sort of error.
Session
Concepts
Loop
Summary
The loop structures available in C are:
The for loop The while loop The do...while loop
The for loop enables repeated execution statements in C. It uses three expressions, separated by semicolons, to control the looping process. The statement part of the loop can be simple statement or a compound statement.
The ‘comma’ operator is occasionally useful in the for statements . Of all the operators in C it has the lowest priority.
The body of a do statement is executed at least once.
C has four statements that perform an unconditional branch : return, goto, break, and continue.
The break statement enables early exit from a simple or a nesting of loops. The continue statement causes the next iteration of the loop to begin.
A goto statement transfers control to any other statement within same function in a C program, but it allows jumps in and out of blocks.
The exit() function causes immediate termination of the program and control is transferred back to the operating system.
Session
Loop
Concepts
Check Your Progress
_________ allows a set of instructions to be performed until a certain condition is reached.
A. Loop B. Structure
C. Operator D. None of the above
_________ loops check the condition at the top of the loop which means the loop code is not executed, if the condition is false at the start.
A. while loop B. for loop
C. do..while loop D. None of the above
A ___________ is used to separate the three parts of the expression in a for loop.
A. comma B. semicolon
C. hyphen D. None of the above
The _________ loop checks its condition at the end of the loop, that is after the loop has been executed.
A. while loop B. for loop
C. do..while loop D. None of the above
The _______ statement causes execution to return to the point at which the call to the function was made.
A. exit B. return
C. goto D. None of the above
The _______ statement violates the rules of a strictly structured programming language.
A. exit B. return
1.
2.
3.
4.
5.
6.
Session
Concepts
Loop
Check Your Progress
C. goto D. None of the above
The _________ function causes immediate termination of the program and control is transferred back to the operating system
A. exit B. return
C. goto D. None of the above 7.
Session
Loop
Concepts
Try It Yourself
Write a program to print the series 100, 95 , 90, 85,………., 5.
Accept two numbers num1 and num2. Find the sum of all odd numbers between the two numbers entered.
Write a program to generate the Fibonacci series. (1,1,2,3,5,8,13,………).
Write a program to display the following patterns.
(a) 1 (b) 12345 12 1234 123 123 1234 12 12345 1
Write a program to generate the following pattern.
*******
******
*****
****
***
**
* 1.
2.
3.
4.
5.
Loop (Lab)
10 Session
Lab Guide
Objectives
At the end of this session, you will be able to:
Use the loop structure Write Some Programs
Using ‘for’ Loop Using ‘while’ loop Using ‘do…while’ loop
The steps given in this session are detailed, comprehensive and carefully thought through. This has been done so that the learning objectives are met and the understanding of the tool is complete. Follow the steps carefully.
Part I – For the first 1 Hour and 30 Minutes :
10.1 Using the ‘for’ Loop
In this section you will write a program using the ‘for’ loop. The program displays even numbers from 1 to 30.
In the program an ‘integer’ variable, num, is declared. The ‘for’ loop is used to display the even numbers till 30. The first argument of the ‘for’ loop, initializes the variable num to 2. The second argument of the
‘for’ loop, checks whether the value of the variable is less than or equal to 30. If this condition is satisfied the statement in the loop is executed. The ‘printf()’ statement is used to display the value of the variable num.
In the third argument, the value of the variable num is incremented by 2. In C, num += 2 is like num
= num + 2. The ‘printf()’ is executed until the second argument is satisfied. Once the value of the variable becomes greater than 30 the condition does not get satisfied and hence the loop is not executed.
The curly brackets are not necessary when only one statement is present within the loop, but it is a good programming practice to use them.
Session 10
Loop (Lab)
Lab Guide
Create a new file.
Type the following code in the ‘Edit window’ : 1.
Save the file with the name, for.c Compile the file, for.c
Execute the program, for.c.
Return to the editor.
3.
4.
5.
6.
OUTPUT :
The even Numbers from 1 to 30 are 2
Session 10
Lab Guide
Loop (Lab)
10.2 Using the ‘while’ Loop
In this section you will write a program using the ‘while’ loop. The program displays numbers from 10 to 0 in the reverse order.
In this program there is an integer variable num. The variable is initialized to a value.
Consider the following lines of code:
while (num >= 0) {
printf(“\n%d”,num);
num--;
}
The ‘while’ statement checks, if the value of the variable num is greater than 0. If the condition is satisfied the ‘printf()’ is executed and the value of the variable num is reduced by 1. In C, num-- woks as num = num – 1. The ‘while’ loop continues till, the value of the variable is greater than 1 or equal to 0.
Create a new file.
Type the following code in the ‘Edit Window’:
1.
2.
#include <stdio.h>
#include <conio.h>
void main() {
int num;
clrscr();
num = 10;
printf(“\n Countdown”);
while(num >= 0) {
printf(“\n%d”,num);
num--;
} }
Session 10
Loop (Lab)
Lab Guide
Save the file with the name, while.c.
Compile the file, while.c . Execute the program, while.c Return to the editor.
3.
10.3 Using do-while Loop
In this section you will write a program that makes use of the ‘do-while’ loop. The do loop differs from the while loop only in that it executes the statement before evaluating the expression. An important consequence of this, which you should keep in mind, is that unlike a while loop, the contents of a do loop will be executed at least once. Because the while loop evaluates the expression before executing the statement, if the condition is false (zero) right at the beginning, the statement will never be executed.
The program will accept integers and display them until zero (0) is entered. It will then exit from the do…
while loop and print the number of integers entered.
The program declares two variables cnt and cnt1. Inside the do-while loop we are entering the number by giving the code below:
printf(“\nEnter a Number : ”);
scanf(“%d”, &cnt);
Session 10
Lab Guide