1.Write briefly about the unconditional statement with suitable

advertisement
1.Write briefly about the unconditional statement with suitable examples. (or)
What is control statement? Explain briefly about the Iterative statements.
(or)
What are the different types of iterative statements? Explain each with suitable example.
(or)
What are the advantages of for loop? List the differences between for loop and do while loop. Give an example on break and
continue statements.
Iterative :Iterative is used to describe a situation in which a sequence of instructions can be executed multiple times.one pass
through the sequence is called an Iteration.
Loop:
If the sequence of instructions is executed repeatedly it is called a Loop.
They are Three forms of loops commonly used in C.They are
1.while
2.do while
3.for loop
While loop:
It is one of the condition loop. The loop statement will be executed till the condition is true. When the condition become
false the execution will be out of the loop.
Syntax:-while(condition)
{
body of the loop;
}
Flow chart of the while loop execution:
Test
Conditio
n
Exit from the Loop
Body of the loop
Example:
Example for finding a factorial of a given number.
#include<stdio.h>
Main()
{
Int f=1,n;
Clrscr();
Printf(“Enter the n value”);
Scanf(“%d”,&n);
While(n>0)
{
F=f*n;
n--;
}
Printf(“%d”,f);
Getch();
}
Do-while loop:
Syntax:- do
{
Statement;
}while(condition);
 In do-while loop the condition is checked at the end of the loop.
 The do-while loop will execute atleast one time even if the condition is false initially.
 The do-while loop executes until the condition become false.
Difference between while and do-while:
The difference between the while and do-while loop is in the place where the condition is to be tested.
 In the while the condition is tested following the while statement and then the body gets executed.
 Where as in the do-while the condition is checked at the end of the loop.
Example:
#include<stdio.h>
Main()
{
Int i=10;
Do {
Printf(“hello %d \n”,i);
I=i-1;
}while(i>0);
For loop:
The for loop allows to execute a set of instructions until a certain condition is satisfied.
The for loop statement comprises of three actions.The actions are:
Initialize, test condition, and re-evaluation parameters are included in one statement ad they are separated by semicolon.
Syntax:
For(initialize counter; test condition; revaluation parameter)
{
Statements;
}
1. The initialize counter sets a loop to an initial value .this statement is executed only once.
2. The test condition is relational expression that determines the number of iterations desired or it determines when to exit
from the loop.
3. The re-evaluation parameter decides how to make changes in the loop(quite often increment or decrement operations are
used)
Example :To find the ASCII value of a character in c.
#include<stdio.h>
void main(void)
{
int i;
for (i = 0; i < 128; i++)
{
printf("%d = %c\n", i, i);
}
}
Loops in c:
While
do-while
For
while(condition) Do
for(initialize; condition; re-evaluation)
{
{
{
Statement;
Statement;
Statement;
}
}
}
while(condition)
Break:
The keyword break allows the program to terminate the loop . The break skips from the loop or block in which it is defined. The
control then automatically goes to the first statement after the loop or the block. The break can be associated with all conditional
statements.
Syntax:
break;
for example: while(condition)
{
Statement ;
break;
}
Continue:
 The continue statement is used for continuing next iteration of loop statements. when it occurs in the loop, it doesn’t
mean to terminate..
 It is useful when we want to continue the program without executing any part of it.
Syntax :
continue;
Initialization and update:
 Programs typically require some type of preparation ,before executing loops.
 Loop initialization which happens before a loop’s first iteration is a way to prepare the loop
 It may be explicit or implicit initialization
 In explicit initialization, we write the code to set the values of key variables used by the loop.

In implicit initialization relies on a preexisting situation to control the loop.
Updating a loop:
 A loop update is what happens inside a loop’s block that eventually causes the loop to satisfy the condition ,thus ending
the loop.
 Updating happens during each loop iteration .
 Without a loop update, the loop would be an infinite loop.
Event controlled loops:
 In an event controlled ,an event is some thing that happens is the loop’s execution block that changes the loop ‘s control
expression from true to false.
 The program can update the loop implicitly or explicitly.
 We cannot predict the maximum number of iteration during running of a program.
Counter-controlled loops:
 In a counter controlled loop we can control the number of loop iteration.
 In such a loop we use a counter, which we most initialize, update and test.
 The number of loops assign to the counter doesn’t need to be a constant value.
 To update ,we can increment or decrement the counter.
Example for event controlled loop:
Sum=0;
Printf(“enter the initialize value”);
Scanf(“%d”,&value);
While(value !=-1)
{
Sum=sum + value; event which controls loop
Printf(“enter another number “);
Scanf(“%d”,& value);
}
Example for counter controlled loop:
int i=1;
while(i<=10)
{
printf(“i=%d”);
}
Download