Showing posts with label The C Programming Language. Show all posts
Showing posts with label The C Programming Language. Show all posts

Tuesday, November 30, 2010

CONTROL STATEMENTS IN C

DOWNLOAD PROGRAMS1
DOWNLOAD PROGRAMS2
C provides two styles of flow control:
  • Branching
  • Looping
Branching is deciding what actions to take and looping is deciding how many times to take a certain action.

Branching:

Branching is so called because the program chooses to follow one branch or another.

if statement

This is the most simple form of the branching statements.
It takes an expression in parenthesis and an statement or block of statements. if the expression is true then the statement or block of statements gets executed otherwise these statements are skipped.
NOTE: Expression will be assumed to be true if its evaulated values is non-zero.
if statements take the following form:
Show Example
if (expression)
  statement;

or

if (expression)
  {
    Block of statements;
  }

or

if (expression)
  {
    Block of statements;
  }
else
  {
    Block of statements;
  }

or

if (expression)
  {
    Block of statements;
  }
else if(expression)
  {
    Block of statements;
  }
else
  {
    Block of statements;
  }

? : Operator

The ? : operator is just like an if ... else statement except that because it is an operator you can use it within expressions.
? : is a ternary operator in that it takes three values, this is the only ternary operator C has.
? : takes the following form:
Show Example
if condition is true ? then X return value : otherwise Y value;

switch statement:

The switch statement is much like a nested if .. else statement. Its mostly a matter of preference which you use, switch statement can be slightly more efficient and easier to read.
Show Example
switch( expression )
     {
        case constant-expression1:      statements1;
        [case constant-expression2:     statements2;]    
        [case constant-expression3:     statements3;]
        [default : statements4;]
     }

Using break keyword:

If a condition is met in switch case then execution continues on into the next case clause also if it is not explicitly specified that the execution should exit the switch statement. This is achieved by using break keyword.
Try out given example Show Example

What is default condition:

If none of the listed conditions is met then default condition executed.
Try out given example Show Example

Looping

Loops provide a way to repeat commands and control how many times they are repeated. C provides a number of looping way.

while loop

The most basic loop in C is the while loop.A while statement is like a repeating if statement. Like an If statement, if the test condition is true: the statments get executed. The difference is that after the statements have been executed, the test condition is checked again. If it is still true the statements get executed again.This cycle repeats until the test condition evaluates to false.
Basic syntax of while loop is as follows:
Show Example
while ( expression )
{
   Single statement 
   or
   Block of statements;
}

for loop

for loop is similar to while, it's just written differently. for statements are often used to proccess lists such a range of numbers:
Basic syntax of for loop is as follows:
Show Example
for( expression1; expression2; expression3)
{
   Single statement
   or
   Block of statements;
}
In the above syntax:
  • expression1 - Initialisese variables.
  • expression2 - Condtional expression, as long as this condition is true, loop will keep executing.
  • expression3 - expression3 is the modifier which may be simple increment of a variable.

do...while loop

do ... while is just like a while loop except that the test condition is checked at the end of the loop rather than the start. This has the effect that the content of the loop are always executed at least once.
Basic syntax of do...while loop is as follows:
Show Example
do
{
   Single statement
   or
   Block of statements;
}while(expression);

break and continue statements

C provides two commands to control how we loop:
  • break -- exit form loop or switch.
  • continue -- skip 1 iteration of loop.
You already have seen example of using break statement. Here is an example showing usage of continue statement.
#include 

main()
{
    int i;
    int j = 10;

    for( i = 0; i <= j; i ++ )
    {
       if( i == 5 )
       {
          continue;
       }
       printf("Hello %d\n", i );
    }
}
This will produce following output:
Hello 0
Hello 1
Hello 2
Hello 3
Hello 4
Hello 6
Hello 7
Hello 8
Hello 9
Hello 10

Tuesday, November 23, 2010

BITWISE OPERATORS IN C

C Provides six operators for bit manipulation.
bitwise AND
bitwise inclusive OR
bitwise exclusive OR
<< left shift
>> right shift
one's complement (unary)


1.left shift <<;
                     a<<n ;  n number of bits will shifted toward left. 
                     Ex; 8<<2 
                     binary representation of 8 ; 00001000 << 2 
                     00001000<<2 ;
                    -------------------
                     00100000 ;  answer is 32  



2.Right shift <<;
                     a>>n ;  n number of bits will shifted toward right. 
                     Ex; 8>>2 
                     binary representation of 8 ; 00001000 >>2 
                     00001000>>2 ;
                    -------------------
                     00000010;  answer is 2 

Another way for calculating left and right shift:  a<<n = a*2^n;  a >> n =  a/2^n ; 

3.Bitewise AND &;
                               It is just like logical AND(&&) but we apply AND operation at binary level. 

Example:   2 & 3 ;
2; 00000010
3; 00000011   &
---------------------

    00000010  answer is 2 



4.Bitewise OR |;
                               It is just like logical OR(||) but we apply OR operation at binary level. 

Example:   2 | 3 ;
2; 00000010
3; 00000011   |
---------------------
    00000011  answer is 3 

5.Exclusive OR ^;
                             The ^ (caret) operator performs a bitwise exclusive-OR on two integers. Each bit in the result is 1 if  one is 1 but not both, result will be 0 if both are 0 or 1. 
Example:   2 ^ 3 ;
2; 00000010
3; 00000011   ^
---------------------
    00000001  answer is 1 

6.One's complement ~;
                               The ~ (tilde) operator performs a bitwise complement on its single integer operand. (The ~ operator is therefore a unary operator, like ! and the unary -&, and * operators.). at binary level 0 will be converted into 1 and 1 will converted into 0. 

~ 2 ;  00000010 ~
---------------------------
          11111101  answer is -3

Sunday, November 21, 2010

OPERATORS in C

a+b; 
In this mathematical expression a, b are called operands.
+ is treated as Operator. 


What is operand?? 
Operand can be a variable or constant. just like 2, 3, 44.99 so....... on. 


Then what is Operator??
Operator specifies what has to be done on the operands for instance addition. 


C provides large set of operators, which can be classified as 
1. Arithmetic Operator
2. Relational Operator
3.Logical Operator. 
4. Increment and Decrement Operators 
5. Conditional Operators. 


1. Arithmetic Operator: 


x % y produces the remainder when x is divided by y, and thus is zero when y divides x exactly. The % operator cannot be applied to a float or double. The direction of truncation for / and the sign of the result for % are machine-dependent for negative operands. 


The binary + and - operators have the same precedence, which is lower than the precedence of *, / and %, which is in turn lower than unary + and -. Arithmetic operators associate left to right.                                                                           


Binary Operator: which needs at least 2 operands. Ex; a+b, a*b, 2/3.....
Unary Operator: which needs only one operand. 


2.Relational Operators:




Operator

Meaning

<

is less than

<=

is less than or equal to

>

is greater than

>=

Equality Operators:

is greater than or equal to



==

is equal to

!=



is not equal to


Relational operators  have the same precedence. Just below them in precedence are the equality operators
Relational operators have lower precedence than arithmetic operators.


3.Logical Operators:
We use Logical Operators to combine 2 or more expressions.


&&        Logical AND
||             Logical OR


Logical Operators have less precedence than Relational Operators.  Logical AND has More PRECEDENCE than Logical OR. 


NoTe: !  negation operator converts a non-zero to 0 and zEro to Non-zeRo.
Ex: 1) !0 ----> 1
      2) 33! ---> 0


! is unary operator. 


4.Increment and Decrement Operators:


Operator            Meaning 
 ++                     Increment 
  - -               Decrement 


The increment operator ++ adds 1 to its operand, while the decrement operator -- subtracts 1. 

The unusual aspect is that ++ and -- may be used either as prefix operators (before the variable, as in ++n), or postfix operators (after the variable: n++). In both cases, the effect is to increment n. But the expression ++n increments n before its value is used, while n++ increments n after its value has been
used. This means that in a context where the value is being used, not just the effect,

++n and n++ are different. If n is 5, then

x = n++;

sets x to 5, but

x = ++n;
sets x to 6. In both cases, n becomes 6. The increment and decrement operators can only be applied to variables; an expression like (i+j)++ is illegal.

5.Conditional Operator:
C has one last operator which we haven't seen yet. It's called the conditional or ``ternary'' or ?: operator, and in action it looks something like this:


 average = (n > 0) ? sum / n : 0

The syntax of the conditional operator is
 e1 ? e2 : e3
and what happens is that e1 is evaluated, and if it's true then e2 is evaluated and becomes the result of the expression, otherwise e3 is evaluated and becomes the result of the expression.


Ex:
int result = (10>9) ? 1:2 ;
in these example expression one is true so second expression will be executed. Finally 1 will be assigned to result.