ALL SIR NOTES HAVE BEEN UPLOADED HERE.
Subject Teacher: Bhagirath Singh Chauhan # 9829275869 Page No.: 1
Loops or iterations are used when we want to execute a statement or set of statement many times. A program loop therefore consists of two segments, one known as the body of the loop and the other known as the control statement (test condition). The control statement tests certain conditions and then directs the repeated execution of the statements contained in the body of the loop.
Depending on the position of the control statement in the loop, a control structure may be classified either as the entry-controlled loop or as the exit-controlled loop.
Entry Entry
False True Loop
Condition Body
Loop
Body
True
Condition
False
Entry Controlled Loop Exit Controlled Loop
The C language provides for three constructs for performing loop operations. They are:
The while statement
The do statement
The for statement
The while is an entry-controlled loop statement. The condition is evaluated and if the condition is true, then the body of the loop is executed. This process of repeated execution of the body continues until the condition finally becomes false and the control is transferred out of the loop. The basic format of the while statement is:
while ( condition )
{
//code to be executed;
}
The do is an exit controlled loop. On some occasions it might be necessary to execute the body of the loop before the test is performed. Such situations can be handled with the help of the do statement. This takes the form:
do
{
//code to be executed;
} while (condition );
On reaching the do statement, the program proceeds to evaluate the body of the loop first. At the end of the loop, the condition in the while statement is evaluated. If the condition is true, the program continues to evaluate the body of the loop again. This process continues as long as the condition is true.
S.No. while loop do-while loop
1
while loop is an entry controlled loop.
do-while is en exit controlled loop.
2
Condition is checked first then statement(s) is executed.
Statement(s) is executed atleast once, thereafter condition is checked.
3
The while loop terminates when the condition becomes false.
As long as the condition is true, the compiler keeps executing the loop.
4
It might occur statement(s) is executed zero times, if condition is false.
At least once the statement(s) is executed.
5
No semicolon at the end of while.
Semicolon at the end of while.
6
If there is a single statement, brackets are not required.
Brackets are always required.
7
Variable in condition is initialized before the execution of loop.
Variable may be initialized before or within the loop.
8
while loop is not used for creating menu-driven programs.
It is mostly used for creating menu-driven programs.
9
Syntax of while loop:
while (condition)
{
Statement block;
}
Syntax of do-while loop
do
{
Statement block;
} while (condition);
The for loop is another entry-controlled loop that provides a more concise loop control structure. The general form of the for loop is:
for ( initialization ; condition ; increment/decrement/update )
{
//Loop Body
}
The execution of the for statement is as follows:
Initialization of the control variables is done first, using assignment statements.
The value of the control variable is tested using the condition. If the condition is true, the loop body is executed, otherwise the loop is terminated.
After executing loop body, the control variable is incremented or decremented, and new value of the control variable is again tested.
Jump statement makes the control jump to another section of the program unconditionally when encountered. It is usually used to terminate the loop or switch case instantly. It is also used to escape the execution of a section of the program. There are following jump statements offered by C language:
break
continue
goto
return
break: When a break statement is encountered inside a loop, the loop is immediately exited and the program continues with the statement immediately following the loop. When the loop are nested, the break would only exit from the loop containing it.
Example: Add only positive numbers, if negative value is entered then jump out from loop.
#include <stdio.h>
int main()
{
int i, n, sum = 0;
for ( i = 1; i <= 5; i++)
{
printf(“\nEnter number: ”);
scanf(“%d”, &n);
if ( n < 0)
break;
sum += n;
}
printf(“\nSum = %d”, sum);
return 0;
}
continue: The continue causes the loop to be continued with the next iteration after skipping any statements in between.
Example: Add only positive numbers out of 5 inputted numbers.
#include <stdio.h>
int main()
{
int i, n, sum = 0;
for ( i = 1; i <= 5; i++)
{
printf(“\nEnter number: ”);
scanf(“%d”, &n);
if ( n < 0)
continue;
sum += n;
}
printf(“\nSum = %d”, sum);
return 0;
}
The goto Statement
C supports the goto statement to branch unconditionally from one point to another in the program. The goto requires a label in order to identify the place where the branch is to be made. A label is any valid variable name, and must be followed by a colon. The label is placed immediately before the statement where the control is to be transferred.
goto label;
….
l abel: statement;
return Statement
Return jump statement is usually used at the end of a function to end or terminate it with or without a value. It takes the control from the calling function back to the main function (main function itself can also have a return).
Nesting of Loops
Nesting of loops, that is, one loop statement within another loop statement, is allowed in C. For example, two loops can be nested as follows:
for ( n = 1; n < 5; n++ )
{
while ( condition )
{
Statement(s);
}
}
The nesting may continue up to any desired level. (ANSI C allows up to 15 levels of nesting).
Example: Print the factorial of a given number.
#include <stdio.h>
int main()
{
int i, num, fact = 1;
printf(“Enter a number: “);
scanf(“%d”, &num);
for(i = 2; i <=num; i++)
{
fact = fact * i;
}
printf(“\nFactorial of %d = %d”,num, fact);
return 0;
}
Example: Print the sum of digits of a number.
#include <stdio.h>
int main()
{
int num, temp, remainder, sum = 0;
printf(“Enter a number: ”);
scanf(“%d”, &num);
temp = num;
while(temp != 0)
{
remainder = temp % 10;
sum = sum + remainder;
temp = temp / 10;
}
printf(“\nSum of digits of %d = %d”, num, sum);
return 0;
}
Example: Print the reverse of a number.
#include <stdio.h>
int main()
{
int num, temp, reverse = 0, remainder;
printf(“Enter a number: ”);
scanf(“%d”, &num);
temp = num;
while(temp != 0)
{
remainder = temp % 10;
reverse = (reverse * 10) + remainder;
temp = temp / 10;
}
printf(“\nReverse of %d = %d”, num, reverse);
return 0;
}
Example: Calculate the sum of first n natural numbers.
#include <stdio.h>
int main()
{
int num, i, sum = 0;
printf(“Enter a positive number: “);
scanf(“%d”,&num);
for( i = 1; i <= num; i++)
{
sum = sum + i;
}
printf(“\nSum = %d”, sum);
return 0;
}
Example: Print the following pattern.
1
2 2
3 3 3
4 4 4 4
#include <stdio.h>
int main()
{
int line, i , j;
printf(“\nEnter number of lines: “);
scanf(“%d”, &line);
for( i = 1; i <= line; i++)
{
printf(“\n”);
for( j = 1; j <= i ; j++)
{
printf(“%d “, i );
}
}
return 0;
}
Example: Program for check prime number.
#include <stdio.h>
int main()
{
int i , num, flag = 1;
printf(“ \nEnter a number: “);
scanf(“%d”,&num);
for(i = 2; i<=num/2;i++)
{
if(num % i == 0)
{
flag = 0;
break;
}
}
If ( flag == 1)
printf(“\n Number is Prime”);
else
printf(“ \n Number is NOT Prime”);
return 0;
}