Thursday 18 May 2017

Elements of programming-8




Do while loop:

 In while looping condition is checked at the entry of the loop.if the condition is false the loop will terminate even before the first iteration of loop. In while loop the condition is checked at the end of the loop. So there is a guarantee the loop will run at least one time Even if the condition is false.

Difference Between while and do-while loop.


While loop


Do-while loop

The condition checked at the entry of the loop

The condition is checked at the exit of the loop


Zero time execution is possible

The loop will run atleast ontime on any condition


 Example 

#include <stdio.h>
int main()
{
    int i=1;
 do
    {
        printf("%d\n",i);
        i++;
    }  while(i<=10);

    return 0;
}

Output:

Thankyou
-----Muthu karthikeyan,Madurai.

Thursday 4 May 2017

Elements of programming-7




Loops :
A block of statements executed repeatedly until a condition is true.this is called loop.
There are three types of loop.
  1. 1.       While loop
  2. 2.       Do—while loop
  3. 3.       For loop
While loop
A  variable is initialized first then it is  compared with a value. Until that condition is true the statements inside the loop  will be executed repeatedly. The counter variable will be incremented or decremented  inside the loop.
Example-1
#include <stdio.h>
int main()
{
    int i=1;
    while(i<=10)
    {
        printf("%d\n",i);
        i++;
    }
    return 0;
}
Output:

Example-2:

#include <stdio.h>


int main()
{
    int i=1,sum=0;
    while(i<=10)
    {
        sum=sum+i;
        i++;
    }
    printf("sum from 1+2--+3=%d",sum);
    return 0;
}
Output:


--------------will continue,
Thanking you
------Muthu karthikeyan,Madurai