Output 1
Enter any arithmetic operator(+,-,*,/,%):*
Enter 2nos:4 5
The Product is:20
x
-3. LOOPS
3.1 Def: A portion of program that is executed repeatedly is called a loop.
The C programming language contains three different program statements for program looping. They are
1. For loop 2. While loop 3. Do-While loop
3.2 The For Loop
The for loop is most common in major programming languages.
However, the for loop in languages is very flexible and very powerful.
Generally, the for loop is used to repeat the execution statement for some fixed number of times.
The general form of for loop is
for(initialization;condition;increment/decrement) statement;
where the statement is single or compound statement.
initialization is the initialization expression, usually an assignment to the loop-control variable. This is performed once before the loop actually begins execution.
condition is the test expression, which evaluated before each iteration of the loop, which determines when the loop will exist.
increment is the modifier expression, which changes the value of loop control variable. This expression is executed at the end of each loop.
Semi colons separate these three sections.
The for loop repeats the execution of the statement as long as the conditional expression evaluates to true. Once this test condition becomes false, the for loop will terminate and control resumes to the statement that immediately following for loop.
The following program illustrates the use of for loop /* Print 1 to 10 numbers */
#include<stdio.h>
#include<conio.h>
void main() {
int i;
clrscr();
for(i=1;i<=10;i++) printf(“\n%d”,i);
getch();
}
In this example, the for loop is repeated 10 times.
In for-loop, any or all of the three sections in the parentheses can be omitted, although the semicolons must remain. If the conditional expression is omitted, it is assumed to have a value1 and the loop continues infinitely.
The for statement for(; ;)
{ statement;
}
is an infinite loop. This can be terminated by using a break statement or an exit() function.
The comma operator, is the power of the for loop. The general rule of comma operator is multiple C expressions may be separated by a comma operator as
expression1, expression2, expression3, ………… and so on.
Expressions separated by comma operator are evaluated from left to right. For example, the following for statement initializes the value of counter1 and counter2 values; and also increments the counter1 and counter2 for each iteration.
for
(counter1=0,counter=1;couter<=10;coutr1++,counter2++) printf(“\n%d %d”,counter1,counter2);
Of all operations in C the comma operator has the lowest precedence.
This important feature of loops in C languages is that the statement in the loop also can be omitted. Generally this type of loops is used in time-delay programs. For example.
for(i=0,j=5;i<10 && j<50;i++,j=i+j) printf(“\n%d %d,”i,j);
This loop is repeated as long as the value of 1 is less than 10 and the value of j is less than 50.
The increment and decrement operators are very useful in loops for incrementing or decrementing the loop-control variable.
3.3 Nested Loops: C allows nested loops, i.e., a loop within a loop. The nested loops are generally used when we want to run the program repeated by placing the entire program within a loop such as while or for loop. In such case, prompt may be included whether to continue the program or to terminate.
3.4 The While Loop
The while loop is best suited to repeat a statement or a set of statements as long as some condition is satisfied.
The general form of while loop is initial expression;
while(conditional-expression) {
statement;
increment/decrement;
}
where the statement (body of the loop) may be a single statement or a compound statements. The expression (test condition) must results zero or non-zero.
First of all, the expression in the while loop is evaluated. If the result is true (non-zero), then the statement is executed and the expression is evaluated again. The statements continue executes until the expression evaluates to false (zero). Then the loop is finished and the program execution continues with the statement that follows by body of while loop.
The statement in the while loop is executed zero or more times depending on the expression. If the expression evaluates to false at the first time, then the statement is never executed.
3.5 The do-while loop
The structure of do-while loop is similar to while loop. The difference is that in case of do-while loop the expression is evaluated after the body of loop is executed. In case of while loop the expression is evaluated before executing body of loop.
The general form of do-while statement is do
{
statement;
}while(expression);
where statement is a single statement or compound statement.
In contrast to while loop statement (body of loop), do-while loop is executed one or more times.
In do-while loop, first the statement is executed and then the expression is tested. If the expression evaluates to false, the loop terminates and the execution control transfer to the next statement that follows it. Otherwise, if expression evaluates to true (non-zero), execution of the statement is repeated and the iterative process continues.
In case of do-while loop, the statement is executed at least once, whereas in case of while loop, the statement may not execute at all if the expression results false for the first time itself.