Wednesday, September 2, 2009

Quick Tips for Loop Statement in C programming

  1. there are three types of loop such as a) for loop b) while loop and c)do-while loop the purpose of each loop are same ( to repeat some part of the program) . but why there are three type of  loop:


a)      for loop : if the loop will continue finite number of times.
b)      While loop: if the loop will continue unknown number of times .
c)      Do..while loop: if the loop will continue at least one.

  1. zero (0) evaluate to false .

  1. if 0 is used as an expression in while loop , the condition is false and loop does not executed.
  2. If 0 is used as an expression in do while the condition is false and loop execute at least once.
  3. If 0 is used as an expression in for loop the condition is false and loop is execute at least once and it is equivalent to do .. while loop.

  1. if we drop all part of the for loop it is infinite loop , and drop all parts of while  and do …while loop is an error.
  2. break statement used only with switch statement and loop statement.
  3. Terminated with semicolon(;) in for loop is infinite loop , and in while loop it is infinite and in do...while loop it is compulsory.
  4. Dropping {} in for loop is not infinite , in while it is infinite but in do while it is necessary and infinite.
  5. the frequent variable only used with loop constant.
  6. use register keyword in frequent used of variable .
     for example : register int i;
                              for(i=0;i<=100;i++)

                                                printf(“%d”,i);
  1.  for loop is pre tested , while is pre tested where as do…while is post tested .for an example: for (;8<2;)>
  2. there are three important factors are necessary for any type of loop .

a.       start value
b.      stop value
c.       step value

  1. if we drop the step value it is an infinite loop.
  2. if the loop counter value is beyond of its range then that loop is infinite loop.
  3. function recursion is the replacement of loop.  

Switch Case using tips in c programming

  1. if the case match with an expression execution start from the case.

Example : void main()
{
  int x=2;
 switch(x)
 {
  case 1:
printf(“one”);
  break;
 case 2:
printf(“two”);
 break;
 }
}

  1. if none of the case are satisfied then the default case is satisfied

example : void main()
                        int x=102;
                        switch(x)
                        {
                                    case 10: printf(“ I am a reader”);
                                    case 11:printf(“I am writer”);
                                    default: printf(“I am programmer”);
}





3.case character must be integral constant.
4.break is used to prevent the falling of control from one case to another case


5. case can in any order.
 6.In the switch statement , the keyword ‘continue ‘ cannot be used in place of ‘break’. The compiler will generate an error message . the use of continue will not take the control to the beginning of switch as one is likely to believe.

7.duplicate case is not allowed.

8. the default case is optional . if there is no match case and there is no default case , then the compiler does not report the error.
9.case character can be constant or constant expression but not variable.

10. if 0 is given as an argument in if , while and do-while , it  evaluates to false but in switch it evaluates as an expression.

11. switch case used for menu driven program

using tips If-Else statement in C programming

  1. if the expression is evaluated with true then it is replace with 1 otherwise  replaced with 0.

Example : if( 5>3)
                 Printf(“YES”);
                        Else
                 Printf(“no”);
  1. if multiple statement are given in the if expression the order of evaluation is form left to right.

Example  int a=5;
                If (a++ ,a=a-5,a--;a)
                  Printf(“yes”);
                Else
                        Printf(“NO”);
3.Nested if can be replace with logical && operator
   Example :-
       Int a=1,b=2,c=3;
      If(a==1)
   If(b==2)
If(c==3)
 Printf(“BOSS”);

  1. ternary operator (?:) sometimes replaced the if statement .

example : if (a>65)
  printf(“hi”;
else
 printf(“bye”);

using ternary operator:
a>65?printf(“hi”);printf(“bye”);
  1. hanging if is not allowed , and it always cause an error misplaced else

       example:
   if(6>2)
printf(“yes”);
printf(“no”);
else

printf(“bye”);

  1. ‘else ‘ is optional part of the if statement

  1. else(:) is compulsory in conditional operator .

example :
 64>3 ? printf(“yes”) : printf(“”);

  1. nested if is differ from multiple if

example :
nested if

int a=1,b=2,c=3;
if(a==1)
if(b==2)
if(c==3)
printf(“BOSS”);

multiple if
int avg=75;
if(( avg>=60)
printf(“first”);
if (avg>=40 && avg<60)

   printf(“second”);
if(avg>=30 && avg<40)


  1. There are other conditional statement like #if , #else , #elseif etc used with preprocessor directive
  2. alwaysuse the “else  if” instead of multiple if.

Int avg=75;
If(avg>=60)
 Printf(“first”);
If(avg>=40 && avg<60)

 Printf(“second”);
If (avg>= 30 && avg<40)>


Use else if

Int avg=75;
If(avg>=60)
 Printf(“first”);
Else If(avg>=40 )
 Printf(“second”);
Else (avg>= 30 )
…………..


            

c programming operator using tips

1.      Precedence decides which operator should be perform first

         Example:-
          P = 6 / 2 + 4 * 3

            (Operator /  and  * enjoy same priority  so it is evaluated from  Left to Right)

          P = 6 / 2  + 4 * 3   [ Operation  of   / ]

          P = 3 +  4 * 3        [ Operation of *]
     
          P = 3 + 12            [ Operation of +]
  
          P =  15
      2. Associative determine the order of evaluation

3. Left to Right  & Right to left are two associative rules used when two or more than two same  operators enjoy the same Priority.
4. There is no operators in C to find out the value of “ A  raised to  power B”.
5. comma ( ,) operator is the lowest priority form the group of 45 operator.
6. parenthesis ( ), [], . and -> having highest priority from group of 45 operators .
7. Sizeof( ) is an operator and a keyword which look like function.
8. Modules ( %) is an operator that works with only integral type.
 Example:-
      Float x ;
     X = 10.0 % 3 ;
     Printf(“ %f ”, x);  //( wrong approach )
 Int x ;
X = 10 / 3 ;
Printf ( “%d”, x) ;  // right approach

9. C does not allow “culprits”  such as ** ,//
  example :
int a=4;
a**;
printf(“%d”, a);

10. unary operator (++,--) Can apply to variable not to constants.
11.unary operator plus (+) is dummy operator.
    Int a=6;
   +a;
printf (“%d”,a);

12. some time ternary operator (?:) replace the conditional if statement.

Like  example:-
Int a=6;
If(a>5)
 Printf(“hello”);
Else
Printf(“hi”);

(a>=5)?printf(“hello”):printf(“hi”);

13.comma and parenthesis are twin brothers.

Example :

While ( (P=getch() )!= EOF)
While ( p=getch() ,!=EOF )
14. Return Keyword cannot be used with ternary operator (?:)
  void main()
  {
 int p=5;
p>65540? Return1 : return 0;
}

15.the bit wise operator work with only integer and character .

16. continuous triple plus (+++) is allowed but not more or less.
  For example:
   Int a=3 b;
    B = a+++5; // means b = a ++ + 5  which is 8

17.      More then one + is allow in same statement but space matter a lot between operator .
18.      ## operator helps to concatenate between tokens

#define m (x) printf(“yes %d”, a##x)
void main()
{
int a1=32;
int a2=30;
m(1);
m(2);
}
19.      Turboc used many punctuation know as separators like (), [],.. etc.
20.      caste operator is an operator which is a signal (force) to compiler to convert one caste to other caste .

         example :-  float x; x =  (float) 5/2;
 21. all operators in C can be overload in c++ but except ?: , .* :: etc



 
      

Blog List