Sunday, 7 August 2016

CONTINUE STATEMENT

/* SKIPPING FROM A LOOP BY CONTINUE STATEMENT */

#include <stdio.h>
int main ()
{
 int a = 10;   /* local variable definition */
 do     /* do loop execution */
 {
  if( a == 15)
  {
   a = a + 1;
   continue; /* skip the iteration using continue*/
  }
  printf("value of a: %d\n", a);
  a++;
 }while( a < 20 );

 return 0;
}
output :
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 16
value of a: 17
value of a: 18
value of a: 19

GOTO STATEMENT

#include <stdio.h>
int main ()
{
 int a = 10;   /* local variable definition */
 LOOP:    /* Label */
 do    /* do loop execution */
 {
  if( a == 15)
  {
   a = a + 1;
   goto LOOP; /* skip the iteration and going exicution to label*/
  }
  printf("value of a: %d\n", a);
  a++;
 }while( a < 20 );
 return 0;
}

output :
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 16
value of a: 17
value of a: 18
value of a: 19

BREAK STATEMENT

#include <stdio.h>
int main ()
{     /* local variable definition */
 int a = 10;   /* while loop execution */
 while( a < 20 )
 {
  printf("value of a: %d\n", a);
  a++;
  if( a > 15)
  { break; } /* terminate the loop using break statement */
 }
 return 0;
}
output :
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15

NESTING OF FOR LOOP

#include<stdio.h>
void main()
{
 int i,j;
 for (i=1;i<=5;i++)  //excuting 1st loop
 {
  for(j=1;j<=i;j++) //excuting 2nd loop
  {   printf("#");    } //printing the # at 'i' times
        printf("\n");   //after 2nd loop printing next line
 }
}

output :
#
##
###
####
#####

FOR LOOP

/* FOR LOOP IN C */

#include<stdio.h>
void main()
{
 for( int a = 10; a < 20; a = a + 1 ) /* for loop execution */
 {
  printf("value of a: %d\n", a);
 }
 return 0;
}

output :
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19

DO WHILE LOOP

#include <stdio.h>
int main ()
{    
 int a = 10;    /* local variable definition */
 do     /* do loop execution */
 {
  printf("value of a: %d\n", a);  /* at 1st printing the RAW */
  a = a + 1;   /* incrementing */
 }while( a < 20 );   /* at last checking the condition */

 return 0;
}

output :
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19

WHILE LOOP

#include <stdio.h>
int main ()
{    /* local variable definition */
 int a = 10;  /* while loop execution */
 while( a < 20 )
 {
  printf("value of a: %d\n", a);
  a++;
 }
 return 0;
}
output :
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19

NESTED SWITCH STATEMENT

#include <stdio.h>
int main ()
{
 int a = 100;
 int b = 200;
 switch(a)
 {
 case 100:
  printf("This is outer part of switch\n");
  printf("Exact value of a is : %d\n", a );
  switch(b)
  {
  case 200:
   printf("This is inner part of switch\n");
   printf("Exact value of b is : %d\n", b );
  }
 }
 return 0;
}

output :
This is outer part of switch
Exact value of a is : 100
This is inner part of switch
Exact value of b is : 200

SWITCH STATEMENT

#include <stdio.h>
int main ()
{
 char grade = 'B';  /* local variable definition */
 switch(grade)
 {
 case 'A' :
  printf("Excellent!\n" );
  break;
 case 'B' :
  printf("Well done\n" );
  break;
 case 'C' :
  printf("Average \n");
  break;
 case 'D' :
  printf("You passed\n" );
  break;
 case 'F' :
  printf("Better try again\n" );
  break;
 default :
  printf("Invalid grade\n" );
 }
 printf("Your grade is %c\n", grade );

 return 0;
}
output :
Well done Your grade is B

ELSE ...... IF LADDER

#include <stdio.h>
int main ()
{
      int a = 100;                        /* local variable definition */
      if( a == 10 )                       /* check the boolean condition */
 {                                              /* if condition is true then print the following */
  printf("Value of a is 10\n" );
 }
 else if( a == 20 )
 {                                              /* if 'else if' condition is true */
  printf("Value of a is 20\n" );
 }
 else if( a == 30 )
 {                                              /* if 'else if' condition is true */
  printf("Value of a is 30\n" );
 }
 else
 {                                              /* if none of the conditions is true */
  printf("None of the values is matching\n" );
 }

 printf("Exact value of a is: %d\n", a );
 return 0;
}

output :
None of the values is matching
Exact value of a is: 100

NESTED IF STATEMENT

#include <stdio.h>
int main ()
{
       int a = 100;                                    /* local variable definition */
       int b = 200;
       if( a == 100 )                                 /* check the boolean condition */
       {                                                    /* if condition is true then check the following */
              if( b == 200 )
              {                                              /* if condition is true then print the following */
                      printf("The two conditions are true, Value of a is 100 and b is 200\n" );
              }
       }
        printf("Exact value of a is : %d\n", a );
        printf("Exact value of b is : %d\n", b );
      
        return 0;
}

output :
The two conditions are true, Value of a is 100 and b is 200
Exact value of a is : 100
Exact value of b is : 200

IF .... ELSE , STATEMENT

#include <stdio.h>
int main ()
{    
 int a = 100;   /* local variable definition */
 if( a < 20 )     /* check the boolean condition */
 {     /* if condition is true then print the following */
     printf("a is less than 20\n" );
 } 
 else
 {   /* if condition is false then print the following */
     printf("a is not less than 20\n" );
 }
 printf("value of a is : %d\n", a);
 return 0;
}
output :
a is not less than 20;
value of a is : 100

SIMPLE IF STATEMENT

#include <stdio.h>
int main ()
{
 int a = 10;  /* local variable definition */
 if( a < 20 )  /* check the boolean condition using if statement */
 {    /* if condition is true then print the following */
  printf("a is less than 20\n" );
 }
 printf("value of a is : %d\n", a);
 return 0;
}

OUTPUT :
a is less than 20
value of a is : 10

DIFFERENT TYPE OF " DATA TYPE " CONVERSION IN C

#include <stdio.h>
main()
{
 int e = 17, f = 5;
 double PI;
 PI = (double) e / f;
 printf("Value of PI : %f\n", PI );

 int i = 17;
 char c = 'c';    /* ascii value is 99 */

 int a;
 a = i + c;
 printf("Value of a : %d\n", a );
 float b;
 b = i + c;
 printf("Value of b : %f\n", b );
}

output :
Value of PI : 3.400000
Value of a : 116
Value of b : 116.000000

OPERATOR PRECEDENCE IN C




#include <stdio.h>
void main()
{
 int a = 20;
 int b = 10;
 int c = 15;
 int d = 5;
 int e;

 e = (a + b) * c / d;                                            // ( 30 * 15 ) / 5
 printf("Value of (a + b) * c / d is : %d\n", e );
 e = ((a + b) * c) / d;                                         // (30 * 15 ) / 5
 printf("Value of ((a + b) * c) / d is : %d\n" , e );
 e = (a + b) * (c / d);                                         // (30) * (15/5)
 printf("Value of (a + b) * (c / d) is : %d\n", e );
 e = a + (b * c) / d;                                            // 20 + (150/5)
 printf("Value of a + (b * c) / d is : %d\n" , e );
 return 0;
}
OUTPUT :
Value of (a + b) * c / d is : 90
Value of ((a + b) * c) / d is : 90
Value of (a + b) * (c / d) is : 90
Value of a + (b * c) / d is : 50

SIZE OF DATA TYPES IN C




#include <stdio.h>
#include <limits.h>
#include <float.h>
int main()
{
 printf("Storage size for char : %d \n", sizeof(char));
 printf("Storage size for int : %d \n", sizeof(int));
 printf("Storage size for float : %d \n", sizeof(float));
 printf("Storage size for double : %d \n", sizeof(double));
 printf("Minimum float positive value: %E\n", FLT_MIN );
 printf("Maximum float positive value: %E\n", FLT_MAX );
 printf("Precision value: %d\n", FLT_DIG );
 return 0;
}

output :
Storage size for char : 1
Storage size for int : 4
Storage size for float : 4
Storage size for double : 8
Minimum float positive value: 1.175494E-038
Maximum float positive value: 3.402823E+038
Precision value: 6

BITWISE OPERATORS IN C ( & , | , ^ , ~ , << , >> )




/* using bitwise operators ( & , | , ^ , ~ , << , >> ) */
#include <stdio.h>
void main()
{
 unsigned int a = 60; /* 60 = 0011 1100 */
 unsigned int b = 13; /* 13 = 0000 1101 */
 int c = 0;
 c = a & b;  /* 12 = 0000 1100 */
 printf("Line 1 - Value of c is %d\n", c );
 c = a | b;   /* 61 = 0011 1101 */
 printf("Line 2 - Value of c is %d\n", c );
 c = a ^ b;   /* 49 = 0011 0001 */
 printf("Line 3 - Value of c is %d\n", c );
 c = ~a;   /*-61 = 1100 0011 */
 printf("Line 4 - Value of c is %d\n", c );
 c = a << 2;   /* 240 = 1111 0000 */
 printf("Line 5 - Value of c is %d\n", c );
 c = a >> 2;   /* 15 = 0000 1111 */
 printf("Line 6 - Value of c is %d\n", c );
}

output:
Line 1 - Value of c is 12
Line 2 - Value of c is 61
Line 3 - Value of c is 49
Line 4 - Value of c is -61
Line 5 - Value of c is 240
Line 6 - Value of c is 15

CONDITIONAL OPERATOR ( expr1 ? expr2 : expr3 )



/* conditional operator expr1 ? expr2 : expr3 */
#include <stdio.h>
void main()
{
 int a=10,b=20,c=10,d;
 d = a==b ? a : b ;   /*Line-1*/
 printf("The value of d in Line-1 is %d\n", d);
 d = a==c ? a : b ;   /*Line-2*/
 printf("The value of d in Line-2 is %d\n", d);
 int large = a>b ? a : b ;  /* large no. checking by conditional operater */
 printf("The large no. is : %d ", large);
}

output :
The value of d in Line-1 is 20
The value of d in Line-2 is 10
The large no. is : 20

INCREMENT AND DECREMENT OPERATORS IN C ( ++ , -- )



/* all increment and decrement operators in C ( ++ , -- ) */

#include <stdio.h>
void main()
{
 int a = 21;
 int c ;
 c = a++;
 printf("Line 1 - Value of c is %d\n", c );
 c = a--;
 printf("Line 2 - Value of c is %d\n", c );
}

output :
Line 1 - Value of c is 22
Line 2 - Value of c is 20

ASSIGNMENT OPERATOR ( = ) AND SHORTHAND ASSIGNMENT OPERATOR ( += , -= , *= , /= , %= etc. )



#include <stdio.h>
void main()
{
 int a = 21;
 int c ;
 c = a;
 printf("Line 1 - = Operator Example, Value of c = %d\n", c );
 c += a;
 printf("Line 2 - += Operator Example, Value of c = %d\n", c );
 c -= a;
 printf("Line 3 - -= Operator Example, Value of c = %d\n", c );
 c *= a;
 printf("Line 4 - *= Operator Example, Value of c = %d\n", c );
 c /= a;
 printf("Line 5 - /= Operator Example, Value of c = %d\n", c );
 c = 200;
 c %= a;
 printf("Line 6 - %= Operator Example, Value of c = %d\n", c );
 c <<= 2;
 printf("Line 7 - <<= Operator Example, Value of c = %d\n", c );
 c >>= 2;
 printf("Line 8 - >>= Operator Example, Value of c = %d\n", c );
 c &= 2;
 printf("Line 9 - &= Operator Example, Value of c = %d\n", c );
 c ^= 2;
 printf("Line 10 - ^= Operator Example, Value of c = %d\n", c );
 c |= 2;
 printf("Line 11 - |= Operator Example, Value of c = %d\n", c );
}

output :
Line 1 - = Operator Example, Value of c = 21
Line 2 - += Operator Example, Value of c = 42
Line 3 - -= Operator Example, Value of c = 21
Line 4 - *= Operator Example, Value of c = 441
Line 5 - /= Operator Example, Value of c = 21
Line 6 - %= Operator Example, Value of c = 11
Line 7 - <<= Operator Example, Value of c = 44
Line 8 - >>= Operator Example, Value of c = 11
Line 9 - &= Operator Example, Value of c = 2
Line 10 - ^= Operator Example, Value of c = 0
Line 11 - |= Operator Example, Value of c = 2