Loop in c

Types of loop.
1) for loop
2)while loop
3)do-while loop.

1)for loop.
For loop is used to repeat the block of code.

Syntax of for loop.

for(init;condition; increment/decrement)
{
 //Code
}

init:
First initialise the variable at the starting of loop.

Condition:

If the condition is true,the body of loop will execute.If the condition is false,the body of loop will not execute.

Increment/decrement:

Increment/decrement is done at the end of the loop

Work of for loop

First the initialisation of variable is done
Then condition is check if condition is false ,loop will terminate.if condition is true, the loop will execute.Thenafterincrement/ decrement is done.

The loop will execute till condition is true.

Example of for loop

#include<stdio.h>
int main()
{
   int i;
   for(i =10;i>0;i--)
    {
       Printf("%d",i)
     }
    return 0;
}
  
Output

10 9 8 7 6 5 4 3 2 1


2)while loop


Syntax:

While(condition)
{
   //Code
}

Working of while loop

In while loop the condition is check before the execution of code inside body of while loop.if the condition is false,loop is termimated.if condition is true program will execute.

Example

#include<studio.h>
int main()
{
   int i=0;
   while(i<10)
    {
       Printf("i=%d/n",i);
       i++;
     }
   return 0;
}

Output 

i=0
i=1
i=2
i=3
i =4
i=5
i=6
i =7
i=8
i =9

3) do-while loop.

In for loop and while loop we see that the condition is check in top of loop,but in do-while loop the condition is check in bottom of loop.

So in do-while loop the code inside the body of loop execute at least once.

Syntax:

do
{
    //statement 
}while(condition)

Example:

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

Output


2
3
4
5


  




Pointer in c

What is pointer?
Pointer is a variable that point to the address of  variable.

& Operator use to determine the address of another variable.

The asterisk *used to declare a pointer.

Pointer syntax:data_type *var_name;

Example:int *p// p will hold the address of                                   int variable 
                Float *j // j will hold the address of                                     float variable
               Char *q// q will hold the address of                                   char variable

In above example is a pointer variable not a normal variable.

Explanation:


In above example pointer p point to the integer num,in which the integer number contain value 5. So the value of num =5,the value of p is address of num which is 2293596.

Program of pointer

#include<stdio.h>
#include<conio.h>
void main()//main function 
{
  int i=10;
  int *p; //point to the integer variable
  Printf("address of i is %d",&i);
Printf("value of i is %d",i);
  Printf("address of p is %d",p);
  Printf("value of p is %d",*p);
  getch();
}

Output 

address of i is 65500
Value of i is 10
address of p is 65500
Value of p is 10


Loop in c

Types of loop . 1) for loop 2)while loop 3)do-while loop. 1)for loop . For loop is used to repeat the block of code. Syntax of for ...