Translate

Labels

Showing posts with label LOOP STATEMENT. Show all posts
Showing posts with label LOOP STATEMENT. Show all posts

Saturday, 5 January 2013

LOOP STATEMENT( WHILE )

The while statement is used to execute the set of statements repeatedly till the condition specified remains true.In while statement, the condition is tested at the entry  level.The number of times the loop gets executed is controlled by a control variable. The following  program prints the first ten natural numbers each in one line.Here, a loop is required to execute the printf() function for 10 times.
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
i=1;                          /*Initialization*/
while(i<=10)
{
printf("%d\n",i);        /*processing statement*/
i=i+1;                      /*updating*/
}
}
In the above program, the variable i is used as a control variable that will control the execution of the while loop.The control variable is properly initialized just before the while statement .Since the above program is to print the first 10 natural numbers and the first number to be printed is 1, the control variable is initialized to 1 . The loop has to be executed for 10 times and hence the condition i<=10 is used The body of the while loop is executed if the specified condition is true.The control variable is incremented by 1 every time the loop executes so that the loop will be repeated exactly for 10 times.If the test condition fails, the control is transferred out of the loop.On exit, the program continues with the statement immediately after the body of the loop.If you look into the program once again, the control variable is tested immediately after the initialization of it and it is incremented just before the end of the while loop (just before the closing curly brace).The syntax of the while statement is as follows:
Initialization of the control variable 
while(condition)
{
..........,
..........,           processing statements 
updating the control variable;
}
     The control variable is tested against a condition in the while statement and it should be properly updated within the while loop for proper termination of the loop.If the updating line is missing, the value the control variable will be always 1 and the loop never ends.

If we omit the initialization, condition and the updating statements from the above program, it condition and the updating statement, which printf("%\n",i); that will serve the purpose of this program.

Friday, 4 January 2013

LOOP STATEMENT( FOR )

The for loop in C is simply a shorthand way of expression a while statement.For example,suppose you have the following code in C:

x=1;
while(x<=10)
{
printf("%d\n",x);
x++;
}
you convert this into for loop as follows:
for(x=1;x<=10;x++)
{
printf("%d\n",x);
}
               
 Note that the while lop contains an initialization step(x=1),a test step (x<=10),and an increment step(x++).The for loop lets you put all three parts into one line.In the for loop also the condition is tested at the entry level.The body of the above for loop executes 10 times.Here, the control variable is initialized first and then it is executed.If the test condition is true, the body of the loop s executed;otherwise the loop is terminated and the execution continues with the statement that immediately follows the loop.After the body of the loop has been executed,the control is transferred to the for statement,where the control variable is updated and then retested.The loop continues till the condition remains true.
The syntax of the for loop is as follows:
for(initialization; condition; updation)
{

body of the loop;

}

  A for loop can also be nested.The problem of printing 1 once and 2 twice can be written using nested for loops as follows:

#include<stdio.h>
void main()
{
int i,j;
for(i=1;i<=10;i++)
{
for(j=0;j<=i;j++)      ------------->INNER LOOP
printf("%d",i);

printf("\n");
}
}  

The inner for loop has only one statement and the outer for loop has two statements.If the body of the loop contains only one statement,there is no need for curly braces to enclose the body of the loop.
The problem which is solved using while loop can also be solved using a for loop.It is a good programming practice to use for loop where the number of times of repetition is known precisely.The for loop is a definite repetition loop where we know in advance exactly how many times the loop will be executed.The while loop is preferred when the number of repetitions is not known before the loop begins executing.

eg:
#include<stdio.h>
void main()
{
char ch;
int count=0;
ch=getchar();
while(ch!='\n')            /*condition*/
{
count++;
ch=getchar();
}
printf("The number of characters entered:%d\n",count);
}


In the above example the number of times the loop will be executed is not known until the user presses the enter key in the keyboard.Once the variable ch obtains its value as the new line character (Enter key pressed),the test condition fails and the control is transferred to the next statement immediately after the body of the while loop.For this problem,the while loop is preferred since the number of characters to be entered by the user is not known precisely.In the above program, the function getchar() is used to read a character at a time from the keyboard and it is a pre-defined function.
  

Tuesday, 1 January 2013

LOOP STATEMENT ( DO_WHILE )

 DO - WHILE STATEMENT

In the while loop, the condition is tested at the entry level.if the condition fails at very first time, the body of the loop will not be executed at all.In case of do_while statement , the condition is tested at the exit level and hence the body of the loop is executed at  least once whether the condition is true or false.At the end of the do_while loop,the condition is tested and if it is true, the loop gets executed once again.This process continues as long as the test condition is true.When the test condition becomes false, the loop is terminated and the control is transferred to the statement immediately after the do_while statement .

eg:
x=14;
do
{
y=x+2;
x- -;
}while(x>0);

In this do_while statement , the two statement y=x+2; and x- - are executed regardless of the initial value of x.Then x>0 is evaluated.If x is greater than 0, the body of the loop is executed again and the condition x>0 is reevaluated.The body of the loop is executed repeatedly as long as x remains greater than 0.Execution of the do_while statement terminates when x becomes 0 or negative.The body of the loop is executed at least once.