Wednesday, October 10, 2012

sizeof() Operator

sizeof() Operator :

sizeof operator is a unary operator.

Unary operator: Unary operation is an operation with only one operand that is which takes single input.

sizeof operator is used to know the exact size of any data type (In bytes).

sizeof (Data type);

Example_1 with code:
1:  #include<stdio.h>  
2:  void main(void)  
3:  {  
4:      int i;  
5:      float j;  
6:      char k;  
7:      printf ("int size = %d\n",sizeof(int));  
8:      printf("float size = %d\n",sizeof(float));  
9:      printf("char size = %d\n",sizeof(char));  
10:      printf("int size = %d\n",sizeof(i));  
11:      printf("float size = %d\n",sizeof(j));  
12:      printf("char size = %d\n",sizeof(k));  
13:  }  
Output:
1:  int size = 4  
2:  float size = 4  
3:  char size = 1  
4:  int size = 4  
5:  float size = 4  
6:  char size = 1  


Example_2 with code:

1:  #include<stdio.h>  
2:  struct a  
3:  {  
4:      int a;  
5:      float b;  
6:      int c;  
7:  };  
8:  void main(void)  
9:  {  
10:    struct a obj;   
11:    printf("struct a size = %d\n",sizeof(struct a));  
12:    printf("obj size = %d\n",sizeof(obj)); 
13:  }  
Output:
1:  obj size = 12  
2:  struct a size = 12  


From the output of example 1 we can clearly see that the size of int and size of float is 4 bytes and size of char is 1 byte.

 In example 2 we have declared a structure "a" having members

int a;
float b;
int c;

what would be the size of structure a?

From example one we observed that int and float is of 4 bytes so the size of structure is
4 + 4 + 4 = 12 bytes
so the output of second example of first "printf" is 12.
In the example we have declared a variable of type struct a
struct a obj;
obj has reserved 12 bytes of memory so when we will print the sizeof (obj) then it will also yield 12 bytes.

Stray Pointer

Stray pointer: 

Stray pointers are some times very dangerous because with stray pointer your program can crash. As we can guess from the name "stray" stray pointers are really stray having no home. Let me clear you with an example.

Suppose in your program you have dynamically allocated some memory (Using malloc( )) and then after using that memory you freed that memory (Using free( )).

Now stray pointer will be created if you will access the deleted memory or you will assign some value at that address.

Example with C program:

1: #include<stdio.h>  
2: void main(void)  
3:{  
4:  int i;  
5:  int *ptr;  
6:  ptr = (int *)malloc(sizeof(int) * 5); // Dynamically allocating 20 Bytes of memory  
7:  for (i = 0 ; i < 5 ; i++)  
8:  {  
9:      *(ptr+i) = i;            // Assing values  
10: }  
11: for (i = 0 ; i < 5 ; i++)  
12: {  
13:     printf("value = %d\n",*(ptr+i));  // printing values on console  
14: }  
15: free(ptr);               // Freeing memory   
16: for (i = 0 ; i < 5 ; i++)   
17: {  
18:     *(ptr+i) = i;            //uh oh, This was deleted!!  
19: }  
20:}  

Explanation: 

In the 6th line we have dynamically allocated 20 Bytes (int is of 4 bytes) of memory.

Now pointer "ptr" has the base address of allocated 20 bytes. 
From line 7 to 10 we are assigning values 
From line 11 to 14 we are printing assigned value at console 
In 15th line we freed the memory by calling free function 
From line 16 to 19 That's the main problem here we are assigning the value which is unknown. We are using that memory which does not belong to us.

Monday, October 1, 2012

Fibonacci Series using Recursion

Write a program to print Fibonacci series using recursion?

1:  #include <stdio.h>  
2:  void fibo(int,int,int);  
3:  void main(void)  
4:  {  
5:    int x,y,c = 10;  
6:    x = 0;  
7:    y = 1;  
8:    printf("1 ");  
9:    fibo(x,y,c);  
10:  }  
11:  void fibo(int x,int y,int c)  
12:  {  
13:    int z;  
14:    if (c < 1)  
15:      return;  
16:    else  
17:    {  
18:      z = x + y;  
19:      printf("%d ",z);  
20:      fibo(y,z,c - 1);  
21:     }  
22:  }  

Output:


 1 1 2 3 5 8 13 21 34 55 89  



Sunday, September 30, 2012

Pointer's

You already know that pointer is most difficult topic in c and c++ languages here i will tell you what is pointer and how you can use pointer with arrays,functions and structures etc.

What is pointer :- Pointer is the user defined data type which can hold the address of simple data type like int,char,float and user define data type   like function,pointer etc. and derived data type like array,structure and union. pointer is denoted by *   eg :- int *x;

Before pointer we will discus what are simple variables like int,float and char etc. how simple variables are stored in computer memory.

Example :- int x =10;

                                                                   x
           10          
                                                                   0x5278

Suppose above box is block of computer memory.
Above fig. is showing that how 10 is stored in memory.
What is x?
What is 10?
What is 0x5278?
0x5278 is the address where 10 is stored and x is the name of address (or location) and 10 is the value of x or value at that address.

Pointer declaration  :- How pointer is declared ?
1. int *iptr;
2. char *cptr;
3. float *fptr;
Here iptr is a pointer variable which can hold the address of int type similarly cptr can hold the address of char type and fptr can hold float type.

How pointer is used :-
int x = 20;
int *iptr;
iptr = &x;

--In first statement simple variable is declared.
--In second statement pointer variable is declared.
--In third statement address of value 10 is assigned to iptr means now value of iptr is the address of the value 10 that is 0x5278.
                                                               
                                                                  x

           20            
                                                                  0x5278

                                                                  iptr
        0x5278       
                                                                  0x3678

0x3678 is the address where address of x is stored having name iptr.
Now how we will access the value of x by using iptr?
printf("%d\n",*iptr);
above statement will print the value of x means the output will be 10
Note :- int type pointer will store the address of only int type variable you can't store the address of float,char into int type pointer......
int x = 10;
float *fptr;
fptr = &x;
Third statement is wrong because x is an int type and fptr is a float type and you can't assign the address of int to float type pointer .
But if you want to do this then you will first typecast it.
Important fact about pointer :- 
                                    *&p = p;
 But &*p = p will not compile.
----------------------------------------------------------------------------------------------------------------------------------
Pointer with arrays :-

Array :- Array is a derived data type it is used  to store the homogeneous values and the main fact is arrays are used to allocate the contiguous memory.
Example :- int arr[10] = {10,20,30,40,50,60,70,80,90,100};
above statement will reserve the 20 bytes of contiguous memory. because int is of 2 bytes in 16 bit compiler.
array structure :-
assume the base address is 0x5270

                    arr         
   10       20       30       40      50      60      70      80      90      100   
                    0x5270
What is arr?
What is 0x5270?
What is 10,20,30------100;
0x5270 is the base address of the whole array and arr is the name given to the base address and 10,20-----100 are values of the arr. By using base address you can access the whole values of the arr 
if you will print the arr then it will print the base address
Example :-  printf("%p\n",arr);
output :- 0x5270
1. arr;
2. &arr;
3. arr[0];
Above three statements will give the same value that is the base address 0x5270.
Note :- you can't change the base address. You can't  perform addition, subtraction, multiplication and division with base address.
Example :- arr = arr + 1;
                   arr = arr - 1;
                   arr = arr * 2;
                   arr = arr / 5;
Above all statements are wrong because if you will change the base address then there is no way to access the previous values(elements) of array.

Note :- Array is a constant pointer You can't change the array's base address.

Access the whole array elements using a pointer variable :- 
Example:- 
Int *ptr;
Int arr[10] = {10,20,30,40,50,60,70,80,90,100};
ptr = &array[0];            //  assign base address of "arr" to "ptr"

Assume base address is 0x5270


                    arr
   10       20       30       40      50      60      70      80      90      100   
                    0x5270


                                                                    ptr
     0x5270     
                                                                    0x6488


Above Fig. shows how pointer variable "ptr" holds base address of an array "arr"
Now you can access the all elements of "arr" using pointer variable "ptr"  like
1. printf("%d",*(ptr+i));   or        //  "*" means "value at address"
2. printf("%d",ptr[i]);
Above both statements will print the all elements of array "arr"

----------------------------------------------------------------------------------------------------------------------------------
Pointer with functions :- 

Function :- Function is build of three things.
1. Function declaration.
2. Function call.
3. Function definition.
Example :- 
// parameterized function with return value.
#include<stdio.h>
int sum(int,int);           // Function declaration
void main()
{
         int x = 10;
         int y = 20;
         int c;
         c = sum(x,y);        //Function call
         printf("%d\n",c);
         return 0;
}
int sum(int a,int b)    // Function definition
{
          int z;
          z = a+b;
          return(z);      // Return statement
}
Output :- 30

Pointer with function :-
Using pointer we can pass the address of the variable
Example :-

#include<stdio.h>
int sum(int *,int *);       // pointer type parameters
void main()
{
         int x = 10;
         int y = 20;
         int c;
         c = sum(&x,&y);  // passing address of x and y
         printf("%d",c));
         return 0;
}
int sum(int *a,int *b)   // Now address of x and y is in a and b
{
          int z;
          z = (*a)+(*b);      //Adding "value at a" and "value at b".
          return(z);             //Returning sum of "value at a" and "value at b"
}

Note :- Only using pointer you can return more than one values.


Thursday, September 6, 2012

Pattern

Program to output below pattern?


1:  #include <stdio.h>  
2:  void main(void)  
3:   {  
4:       int i,j;  
5:       for(i = 1 ; i < 5 ; i++)   
6:       {  
7:            for(j = 1 ; j <= i ; j++)  
8:            {  
9:                  if((j % 2) == 0)   
10:                        printf("1 ");  
11:                  else  
12:                        printf("0 ");  
13:            }  
14:            printf("\n");  
15:       }  
16:  }   

Output:

 

Saturday, July 14, 2012

Pattern Diamond

Write a program to output Diamond?

1:  #include<stdio.h>  
2:  #define PARA 12  
3:  int main() {  
4:       int i,d;  
5:       i = (PARA/2);  
6:       d = (PARA/2);  
7:       int row,col;  
8:       for(row = 0 ; row <= (PARA/2) ; row++) {  
9:               for(col = 0 ; col <= PARA ; col++) {  
10:                        if(col == i || col == d) {  
11:                                 printf("*");  
12:                        }  
13:                        else {  
14:                                 printf(" ");  
15:                        }  
16:               }  
17:               i++;  
18:               d--;  
19:               printf("\n");  
20:       }  
21:       i = 1;  
22:       d = (PARA-1);  
23:       for(row = 0 ; row < (PARA/2) ; row++) {  
24:                for(col = 0 ; col <= PARA ; col++) {  
25:                      if(col == i || col == d) {  
26:                              printf("*");  
27:                      }  
28:                      else {  
29:                              printf(" ");  
30:                      }  
31:                }  
32:                i++;  
33:                d--;  
34:                printf("\n");  
35:       }  
36:  }  

Output:-









Note:- You can increase or decrease the size of diamond. For this just change the value of MACRO "PARA"
i.e #define PARA 12
But the value of PARA should be EVEN....

Friday, July 13, 2012

Pattern English Alphabet X

Write a Program to output this pattern English Alphabet X?

1:  #include<stdio.h>  
2:  int main() {  
3:     int x = 0,y = 5,row,col;  
4:     for(row = 0 ; row < 6 ; row++) {  
5:        for(col = 0 ; col < 6 ; col++) {  
6:          if(col == x || col == y) {  
7:             printf("*");  
8:          }  
9:          else {  
10:             printf(" ");  
11:          }  
12:        }  
13:        x++;  
14:        y--;  
15:        printf("\n");  
16:     }  
17:  }  

Output:-



Pattern English Alphabet Z

Write a Program to output this pattern English Alphabet Z?


                              
1:  #include<stdio.h>  
2:  int main() {  
3:    int x = 5,row,col;  
4:    for(row = 0 ; row < 6 ; row++) {  
5:        for(col = 0 ; col < 6 ; col++) {  
6:            if(row == 0 || row == 5) {  
7:                printf("* ");  
8:            }  
9:            else {  
10:                if(col == x) {  
11:                  printf("* ");  
12:                }  
13:                else {  
14:                  printf(" ");  
15:                }  
16:            }  
17:         }  
18:         x--;  
19:         printf("\n");  
20:     }  
21:  }  

Output:-



Thursday, July 12, 2012

Pattern Square

Write a program to output this pattern in C?



1:  #include<stdio.h>  
2:  int main() {  
3:       int col,row;  
4:       for(row = 0 ; row < 5 ; row++) {  
5:             for(col = 0 ; col < 5 ; col++) {  
6:                 if(row == 0 || row == 4) {  
7:                         printf("* ");  
8:                  }  
9:                  else {  
10:                        if(col == 0 || col == 4) {  
11:                            printf("* ");  
12:                         }  
13:                         else {  
14:                             printf(" ");  
15:                         }  
16:                  }  
17:             }  
18:             printf("\n");  
19:        }  
20:  }   

Output:-  






Note:- You can change the value of row and col to increase or decrease the size of pattern...  

Wednesday, July 4, 2012

Calculate CRC

Program to calculate CRC in C language.......

1:   #include<stdio.h>  
2:   #define MS 14  
3:  // MS Message Size in Bits  
4:  // DS Divisor Size in Bits  
5:  int main() {  
6:     int divisor4[50] = {1,0,0,1,1};  
7:     int divisor24[50] = {1,0,1,0,1,1,1,0,1,0,1,1,0,1,1,0,1,1,1,0,0,1,0,1,1 };  
8:     int message[150] = {1,1,0,0,0,1,1,0,1,0,1,1,0,1};  
9:     int msgcopy[150];  
10:     int divisor[50];  
11:     int i,j,DS;  
12:     printf("At sender side\n");  
13:     printf("Enter the CRC you want to use in Bits 4Bit,8Bit,16Bit,24Bit,32Bit\n");  
14:     scanf("%d",&DS);  
15:   // Checking the Size of the divisor in bits  
16:     if(DS == 4) {  
17:         for(i = 0 ; i < DS+1 ; i++) {  
18:               divisor[i] = divisor4[i];  
19:          }  
20:     }  
21:     else if(DS == 24) {  
22:          for(i = 0 ;i < DS+1 ; i++) {  
23:                divisor[i] = divisor24[i];  
24:           }  
25:     }  
26:    // Copying message to another array  
27:     for(i = 0 ; i < MS ; i++) {  
28:           msgcopy[i] = message[i];  
29:      }  
30:    // Appending zero's at the end of the message  
31:     for(i = MS ; i < MS+DS-1 ; i++) {  
32:           message[MS] = 0;  
33:      }  
34:    // Calculating CRC  
35:      i = 0;  
36:      while(i < MS) {  
37:         if(message[i] == 0) {  
38:            i++;  
39:         }  
40:         else {  
41:            for(j = 0 ; j < DS+1 ; j++) {  
42:                  message[i+j] = message[i+j]^divisor[j];  
43:               //  printf("message[%d+%d] = %d\n",i,j,message[i+j]);  
44:             }  
45:          }  
46:      }  
47:     //Appending CRC at the end of the message  
48:      printf("CRC is\n");  
49:      for(i = MS ; i < MS+DS ; i++) {  
50:              msgcopy[i] = message[i];  
51:              printf("msgcopy[%d] = %d\n",i,msgcopy[i]);  
52:      }  
53:     printf("Message after appending CRC at the end\n");  
54:     for(i = 0 ; i < MS+DS ; i++) {  
55:              printf("msgcopy[%d] = %d\n",i,msgcopy[i]);  
56:      }  
57:      for(i = 0 ; i < MS+DS ; i++) {  
58:              message[i] = msgcopy[i];  
59:      }  
60:    // Checking that CRC is correct or not  
61:     printf("At receiver end\nChecking that CRC is correct or not\n");  
62:     i = 0;  
63:     while(i < MS+DS) {  
64:             if(message[i] == 0) {  
65:               i++;  
66:             }  
67:             else {  
68:               for(j = 0 ; j < DS+1 ; j++) {  
69:                   message[i+j] = message[i+j]^divisor[j];  
70:                //  printf("message[%d+%d] = %d\n",i,j,message[i+j]);  
71:               }  
72:            }  
73:      }  
74:   // CRC, After appending CRC at the end of the message  
75:     printf("CRC after appending Original CRC at the end of the message\n");  
76:     for(i = MS ; i < MS+DS ; i++) {  
77:              printf("message[%d] = %d\n",i,message[i]);  
78:     }  
79:  }  
Output :-
1:  At sender side  
2:  Enter the CRC you want to use in Bits 4Bit,24Bit  
3:  4  
4:  CRC is  
5:  msgcopy[14] = 1  
6:  msgcopy[15] = 0  
7:  msgcopy[16] = 0  
8:  msgcopy[17] = 1  
9:  Message after appending CRC at the end  
10:  msgcopy[0] = 1  
11:  msgcopy[1] = 1  
12:  msgcopy[2] = 0  
13:  msgcopy[3] = 0  
14:  msgcopy[4] = 0  
15:  msgcopy[5] = 1  
16:  msgcopy[6] = 1  
17:  msgcopy[7] = 0  
18:  msgcopy[8] = 1  
19:  msgcopy[9] = 0  
20:  msgcopy[10] = 1  
21:  msgcopy[11] = 1  
22:  msgcopy[12] = 0  
23:  msgcopy[13] = 1  
24:  msgcopy[14] = 1  
25:  msgcopy[15] = 0  
26:  msgcopy[16] = 0  
27:  msgcopy[17] = 1  
28:  At receiver end  
29:  Checking that CRC is correct or not  
30:  CRC after appending Original CRC at the end of the message  
31:  message[14] = 0  
32:  message[15] = 0  
33:  message[16] = 0  
34:  message[17] = 0  

Note :- This is for 4 and 24 Bit polynomial if you want to calculate CRC for 8,16,32 and 64 Bits then this code requires some changes . Try it youself.........
This code is calculating CRC for 14 Bits long message, you can use it for another length but don not forget to change Macro definition i.e 

#define MS 14

Monday, June 25, 2012

Factorial using Recursion

Program to calculate Factorial using Recursion.

A
1: #include<stdio.h>  
2: int factorial(int);  
3: int main() {  
4:     int n,f;  
5:     printf("Enter number you want ot calculate factorial\n");  
6:     scanf("%d",&n);  
7:     f = factorial(n);  
8:     printf("Factorial of %d is = %d",n,f);  
9:     return(0);  
10: }  
11: int factorial(int f) {  
12:     int fact;  
13:     if(f <= 1) {  
14:        return(1);  
15:     }  
16:     else {  
17:       fact = (f*(factorial(f-1)));  
18:       return(fact);  
19:     }  
20: }  

Output :- 

1:  Enter number you want to calculate factorial 5  
2:  Factorial of 5 is = 120  


Calculate Factorial

Program to calculate Factorial.

1:  void fact() {  
2:   int n ,i,f = 1;  
3:      printf("Enter number you want to calculate factorial\n");  
4:      scanf("%d",&n);  
5:   for(i = 1 ; i <= n ; i++) {  
6:   f = f*i;  
7:   }  
8:   printf("\n Factorial of %d is = %d\n",n,f);  
9:  Output :- Enter number you want to calculate factorial 5  
10:           Factorial of 5 is = 120  

Call this function from your main 
Like :-


1:  int main() {     
2:       fact();  
3:  }  

Number is Armstrong or not?

Program to check that the number is Armstrong or not ?

1:  void armstrong() {  
2:      int arm,qou,rem,n;  
3:      for(n = 1 ; n <= 999 ; n++) {  
4:             qou = (n/100);  
5:             arm = (qou*qou*qou);  
6:             rem = (n%100);  
7:             qou = (rem/10);  
8:             rem = (rem%10);  
9:             arm = arm+(qou*qou*qou)+(rem*rem*rem);  
10:             if(n == arm) {  
11:                  printf("%d is armstrong\n",n);  
12:             }  
13:      }  
14:  }  
15:  Output :-  
16:  1 is armstrong  
17:  153 is armstrong  
18:  370 is armstrong  
19:  371 is armstrong  
20:  407 is armstrong  

Call this function from your main
Like :-



1:  void main() { 
2:        armstrong();  
3:  }  

Sunday, June 17, 2012

C Language Basics

In this blog i'll explain C in brief here i will explain only main topics of c like arrays,pointers,structures, functions etc.
 
        Decision control structures

        The loop control structures

Before starting C some things that you have to remember.
1. Size of int,float,char,short,long.

Compiler  int       float       char        short     Long
 16 -Bit 2 Bytes   4 Bytes  1 Byte 2 Bytes 4 Bytes 
 32-Bit 4 Bytes 4 Bytes 1 Bytes 2 Bytes 4 Bytes

2. How to run C programs in Linux "UBUNTU"
Steps :-

1 step :-  open terminal
               Applications -> accessories -> Terminal
2 Step :- Type command
               sudo apt-get install gcc
gcc is a compiler which will compile your C code.
3 Step :- Type command
               sudo apt-get install vim
vim is an editor.
4 Step :- Make a folder on your desktop of any name, lets say c
5 Step :- Type in your terminal
               cd Desktop    then press enter
               cd c                then press enter
               now you have entered to c folder
6 Step :- Open your folder that you have created on your desktop then right click
               Create document -> empty file
               name the file with extension .c like firstcprgrm.c
7 Step :- open the file then write your c code and save it
8 Step :- Go to terminal and type gcc "yourfile.c"
               here gcc firstcprgrm.c
               After compiling your file a file has been created with name a.out this file is used to hold the
               machine code of  a prgrm.
9 Step :- Now type ./a.out
               it will execute your c program
If you don't want to open your file again and again by going to your folder for some modification in your program then don't worry because you can open your file in your terminal .

How to open your file in terminal?
Go to terminal and mount your folder by using cd command. Now type command
 vim "filename.c" file that you want to open This command will open your file in terminal.
But to modify your code or rewrite your code by vim editor is not simple because it works in various modes when you entered your file in terminal then press i now you are in insert mode then press Esc and to save your file press :w then enter, now your file is saved, For to back to terminal press Esc and for quit type :q then press enter, now you are back to terminal you can also save and quit your file at the same time by pressing Esc then type :wq then press enter.

Now we will start c........


What is c? 
C is a powerful programming language developed at AT & T's Bell laboratories of USA in 1972. It was designed and written by a man name Dennis Ritchie . It becomes very popular without any advertisement because this language is very reliable , simple and easy to use.


Decision Control Structures

Decision controls are those which helps you to take decisions There are various decision control structures in c
1. if Statement
2. if - else statement
3. Nested if Statement
4. else if Statement
5. if under else statement

IF statement :-
syntax :-

if(condition)
{
            statement 1;
            statement 2;
            -------------
            -------------
            statement n;
}
if the condition is true then all the statements will be executed otherwise no statement will be executed

Example :-
main()
{
          int x = 10;
          if(x == 10)
          {
                   printf("condition is true\n");
                   printf("hello\n");
           }
            if(x == 2)
            {
                   printf("condition is false\n");
                   printf("bye\n");
            }
}
Output :- condition is true
                hello

In the above example first if condition is true so statements are executed successfully but the second if condition is not true so nothing will be executed.

Note :- If you will not put the braces after if condition the by default only one statement will be executed that is "condition is true" second statement will also be executed but not according to if condition .
for example :- 
main()
{
          int x = 10;
          if(x == 2)
          printf("hello");
          printf("bye");
}
Output :- bye
in the above example hello will not be printed because the condition is not true but the bye will be printed because as i told you after if without any braces by default only one statement will be executed.

if-else statement :-
Syntax :-

if(condition)
{
                   statement 1;
                   statement 2;
                   --------------
                   --------------
                  statement n;
}

else
{
                   statement 1;
                   statement 2;
                   --------------
                   --------------
                  statement n;
}

If condition is true then all the statement that comes under if condition will be executed if condition is not true then statement that comes under else will be executed.

Example :-
main()
{
           int x;
           printf("enter value of x \n");
           scanf("%d",&x);
           if(x == 10)
           {
                    printf("condition is true\n");
                    printf("I am in if block\n");
            }
            else
            {
                    printf("condition is false\n");
                    printf("i am in else block\n");
            }
}

output:-  enter value of x 10
              condition is true
              i am in if block

              enter value of x 9
              condition is false
              i am in else block

Observe that when i entered the value  of x is 10 then condition become true and statements that comes under if block gets executed and when i entered the value of x is 9 then condition become false and statements that comes under else block gets executed .  

Note :- Like "if" ,if there is no braces after else block then only one statement gets executed means only one statement that comes under else will be executed after false condition.

Nested if statement :-
syntax :-
if(condition 1)
{
         statements 1;
         if(condition 2)
         {
                   statements 2;
                   ------------
                   {
                           if(condition n)
                           {
                                       statements n;
                           }
                    }
        }
}

If the condition one is true then statements 1 will be executed and control will go to the condition 2 if condition 2 is also true the statements 2 will be executed and control will go to the next if statement and so on...... and if condition 1 is false then no other if statements will be executed. If conditions and statements continuously gets executed successfully till the condition get false.
else if statement :-
Syntax:-
if(condition 1)
{
        statement 1;
        statement 2;
        ---------------
        ---------------
        statement n;
}

else if(condition 2)
{
        statement 1;
        statement 2;
        ---------------
        ---------------
        statement n;
}

else if(condition 3)
{
        statement 1;
        statement 2;
        ---------------
        ---------------
        statement n;
}
---------------------
---------------------

else if(condition n)
{
        statement 1;
        statement 2;
        ---------------
        ---------------
        statement n;
}

Else if is used when number of condition arises in this if the condition 1 is true then the statements that comes under if block will be executed and other conditions will no be checked . if condition 1 is false then condition 2 is checked if condition 2 is true then statements that comes under esle if(condition 2) will be executed if condition 2 is false then control is passed to the next else if and condition 3 is checked and so on.........

Example :-

main()
{
           int x;
           printf("enter value of x \n");
           scanf("%d",&x);
           if(x == 10)
           {
                     printf("i am in if block\n");
                     printf("hello\n");
            }
            else if(x == 8)
           {
                     printf("i am in 1st else if block\n");
                     printf("hello\n");
            }
            if(x == 9)
           {
                     printf("i am in 2nd else if block\n");
                     printf("hello\n");
            }
            if(x == 7)
           {
                     printf("i am in 3rd else if block\n");
                     printf("bye\n");
            }
}

output :- enter value of x 10
              i am in if block
              hello

              enter value of x 8
              i am in 1st else if block
              hello

              enter value of x 9
              i am in 2nd else if block
              hello

              enter value of x 7
              i am in 3rd else if block
              bye

              enter value of x 11

How we got this output try it yourself and if you have any problem then you can comment on this blog.

If under else :-
Syntax :-


if(condition 1)
{
        statement 1;
        statement 2;
        ---------------
        ---------------
        statement n;
}


else
{
        statements;
        if(condition 2)
        {
               statements;
        }
   
}


If condition 1 is false then control will go to else bock after executing statements of else block condition  2 will be checked if condition 2 is true then statements will be executed otherwise not.
Example :-
main()
{
           int x;
           printf("enter value of x \n");
           scanf("%d",&x);
           if(x == 10)
           {
                    printf("condition is true\n");
                    printf("I am in if block\n");
            }
            else
            {
                    printf("condition is false\n");
                    printf("i am in else block\n");
                    if(x == 5)
                    {
                               printf("condition is true\n");
                               printf("Dixit singla\n");
                      }
            }
}
Output :-enter value of x 10
              condition is true
              i am in if block

              enter value of x 5
              condition is false
              i am in else block
              condition is true
              Dixit singla

Note :- if under else is same as else if this is just the another way of use of else if statement else if is better because it is easy and short.


Loop control structures 

Some times condition arises when computer  has to perform set of operations repeatedly. Computer will perform set of operations repeatedly for specific number of times or until a condition is being satisfied. this can be done through loop control instructions.
There are three main loop control instructions are :
1. For loop instruction
2. While loop instruction
3. Do - While loop instruction

For loop instruction :-
Syntax :-
for( initialize counter ; test value ; increment counter)
{
          do this;
          and this;
}
In the for loop three things are main that are
1. initialize counter
2. test value
3. increment counter
let us understand these three things with an example hope you will understand better
Example :-
main()
{
            int i;
            for(i = 0 ; i < 5 ; i++)
            {
                        printf("%d     ",i);
             }
             printf("\n loop ends");
}

Output :- 0     1     2    3     4
                loop ends
How we got this output ?
first of all i is initialized to zero then the condition is checked that is , i < 5 condition is true and statement is executed and 0 will be printed on the console then i is incremented to 1 and again condition is checked that i < 5 again condition is true and 1 will be printed on the console and so on till false condition.

i = 0     initialization
i < 5     condition is checked that is true 0 is less than 5
            0 will be printed on the console
i++       i++ means i = i + 1  that is i is incremented to 1
now i = 1
i < 5      condition is checked and it is true
             1 will be printed on the console
i++        i is incremented to 2
now i = 2

i < 5     condition is checked that is true 2 is less than 5
            2 will be printed on the console
i++       i is incremented to 3
now i = 3
i < 5     condition is checked that is true 3 is less than 5
            3 will be printed on the console
i++       i is incremented to 4
now i = 4
i < 5     condition is checked that is true 4 is less than 5
            4 will be printed on the console
i++       i is incremented to 5
now i = 5

i < 5     condition is checked and now it is false 5 is not < 5 , 5 is equal to 5
            so printf statement will not be executed
And now the lop ends and control will be passed to the next statement after for loop and "loop ends" will be printed on the console.

Note :- Like "if" statement if there is no braces after for loop then only one statement will be considered after for loop.

Nested for loop :-
for loop can be nested 
syntax :-
for( initialize counter ; test value ; increment counter)
{
          do this;
          and this;
          for( initialize counter ; test value ; increment counter)
         {
                      do this;
                      and this;
         }
}
Example :-
main()
{
          int i,j;
          for(i = 0 ; i < 4 ; i++)
          {
                    for(j = 0 ; j < 2 ; j++)
                    {
                                printf("%d  %d\n",i,j);
                     }
                     printf("out of inner loop");
          }
           printf("out of outer loop");
}
Output :- 0  0
               0  1
               out of inner loop
               1  0
               1  1
               out of inner loop
               2  0
               2  1
               out of inner loop
               3  0
               3  1
               out of inner loop
               out of outer loop
Give it your try..............

While loop instruction :-

Syntax :-
initialization;
while(condition)
{
             do this;
             and this;
             increment;
}
Example :-
main()
{
         int x = 1;
         while(x <= 4)
         {
                printf("%d   ",x);
                i++;
         }
         printf("\nend of while loop");
}
Output :- 1  2  3  4
                end of while loop
i = 1
x<=4  condition true
1 will be printed on the console
i++ now value of i is 2

i = 2
x<=4  condition true
2 will be printed on the console
i++ now value of i is 3

i = 3
x<=4  condition true
3 will be printed on the console
i++ now value of i is 4

i = 4
x<=4  condition true
4 will be printed on the console
i++ now value of i is 5
i = 5
condition false 5 is not less than equal to 4 so control is passed to the next statement after while loop that is "printf("end of while loop");"
after executing this statement end of loop will be printed on the console
Note :- like for loop you can also nested the while loop like this
Example :- 
main()
{
       int x = 1,y = 1;       
       while(x <= 4)
        {
                 while(y <= 2)
                  {
                           printf("%d  %d\n",x,y);
                           y++;
                   }
                   x++;
        }
        printf("END\n");
}
Output :- 1  1
                 1  2
                 END
x = 1
y = 1
x <= 4 condition true
y <= 2 condition true
1  1 will be printed on the console
y++ , now y = 2
y <= 2 
1  2 will be printed on the console
y++, now y = 3
y <=2  condition false
control will come out from this loop 
x++ , now x = 2
x <=4 condition true
y <=2 condition false because we did not change the y , still the value of y is 3 so condition is false and control will not enter to the inner while loop and x is incremented to 3 ad so on.........

Do While loop :-
Syntax :-
do
{
            do this;
            and this;
}while(condition);

In do-while loop at least once all the statements will be executed then the condition will be checked .
The main difference between while and do-while loop is the place where the condition is tested . In while loop first the condition is tested and if the condition is true then the statements will be executed and in do-while first the statements will be executed then the condition is checked.
Means that do-while would execute its statements at least once, even if the condition fails for the first time .The while, on the other hand will not execute its statements if the condition fails for the first time.
Example 1:-
main()
{
          int x = 1;
          do
          {
                   printf("%d   ",x);
           }while(x >= 2);
           printf("\n out of do while loop\n");
}
Output :- 1
                out of do while loop
As you can see from the output x = 1 in the first pass condition is false but 1 will be printed on the console so we can say that do while would execute its statements at least once even if the condition fails for the first time.
Example 2 :-

main()
{
          int x = 1;
          do
          {
                   printf("%d   ",x);
                   x++;
           }while(x <= 4);
           printf("\n out of do while loop\n");
}
Output :- 1   2   3  4
                out of do while loop
x =1
1 will be printed on the console
x++ now x = 2
x <= 4  condition true

2 will be printed on the console
x++ now x = 3
x <= 4  condition true
3 will be printed on the console
x++ now x = 4
x <= 4  condition true
4 will be printed on the console
x++ now x = 5
x <= 4  condition false
control will relinquish the while loop and move to the next statement after the while loop and
"out of the do while loop" will be printed on the console