Looping:
When we need a statement again and again then loop statements are used. Basically it is a statement where a statement is executed repeatedly .a loop statements consists of two parts one is body of the loop and other is control statement .the control statement can be placed either before or the body of the loop.Basically c\c++ provides three types of loop structures:
1) while statement
2)do while statement
3)for statement
While statement:
This is used to execute particular statement repeatedly as long as specified condition is true.Syntax:
While(logexp)
{
Statement;
}
Where: while is a keyword
Logexp is an that results in either true or false
Statement , which is to execute when logical expression is true.
In this case until the expression is false loop is executed again and again the statements given in the body of the loop.when expression get false program move to next executable statement in program.
Example:
#include<stdio.h>
main()
{
Int n,I, sum=0;
Printf(“enter the value of n \n”);
Scanf(“%d”,&n);
I=1;
While(i<=n)
{
Sum+=I;
i++;
}
Printf(“sum=%d\n”,sum);
}
Output of the program will be:
Enter the values of n
10
Sum=55
Do-while statement:
This statements executes until the logical expression get false.in this case logexp is check at the end each pass.first expression and then get check for truthness of the logexp.Syntax:
Do
{
Statement;
}
While(logexp);
Where: do and while are keywords
Statement, which is to execute
Logexp logical expression results in true or false.
Note: the body of do while statement is executed atleast once,because checking of logical expression is carried out at the last.
Example:
#include<stdio.h>
main()
{
Int oddnum,sum=0;
Oddnum=1;
Do
{
Sum+=oddnum;
Oddnum+=2;
}
While(oddnum<=50);
Printf(“sum=%d\n”,sum)
}
Output of the program will be:
sum=625
for statement:
when we know that how many times set of statements to be executed then this is for loop is used.Syntax:
For(expression1;expression2;expression3)
{
Statement;
}
Where: for, is a keyword
Expression1, it is the starting point of the execution that is to initialize loop index
Expression2, it is aexpression that tests whether the loop index reached the fixed value that is determine whether to continue or not to continue.
Statement, which is to be executed in range of true statements .
Number of iterations=((final value-initial+step increment)) /step increment
Example:
#include<stdio.h>
main()
{
Int num,;
For(num=1;num<=5;num++)
{
Printf(“xyz\n”);
}
Output of the program will be:
Xyz
Xyz
Xyz
Xyz
Xyz
Nested for loop statement
If there are many data items to be processed against a set of elements repeatedly, then a single for statement is not adequate. So we must use a nested for loop. If one for statement is completely placed within the other, it is known as nested for loop statement.Syntax:
For(exp11;exp12;exp13)
{
For(exp21;exp22;exp23)
{
Statement;
Statement;
}
}
Note:in nested for loop there must be different index variables for different for loop.
Example:
#include<stdio.h>
main()
{
Int I,j ,num=5;
Printf(“number pattern\n”);
For(i=num;i>=1;i--)
{
For(j=1;j<=I; j++)
{
Printf(“%d”,num);
}
Printf(“\n”);
num--;
}
}
Output of the program will be:
Number pattern
55555
4444
333
22
1
In this case I gives number of rows whereas j gives number of columns.
Jumps in loops
We know that loop executes a group of statements as long as test condition is true. But in some application it could be required to skip a portion of the loop or to exit from the loop,when a certain condition is met. For example,consider the searching problem. let us assume , a list of 50 non repeated integer number are accepted and stored in an array. The value(key) to be searched is also accepted .when we start comparing the key value with each of the array elements , at certain iteration we may encounter a successful search, say at 12th iteration should we continue searching for the remaining 38 integers. It is really not required. So we have to exit the iterative process. In order to do this , we need some jumping out of the loop body.Break statement
This statement allows us to terminate the execution of loop and jump out of the loop instead of getting back at the conditional test of the loop.Example:
For(i=0;i<100;i++)
{
Printf(%d”,i);
If(i==10)
Break;
}
.
.
Inthis case when i=10 then output is taken and loop terminates because of presence of break statement otherwise loop will check upto 100.
Continue statement:
This is used to execute next iteration of the loop and to skip any code in between. Hence, it is actually break the current iteration and continue with in the loop for the next iteration.Example:
For(i=1;i<=2;i++)
{
For(j=1;j<=2;j++)
{
If(i==j)
Continue;
Printf(“\n%d%d”,I,j)
}
}
Output of the program will be
12
21
Note: program not been run in c software but logic is correct.
No comments:
Post a Comment