Pages

Saturday, 2 February 2013

THE CASE CONTROL STRUCTURE

In real life we are often faced  with situations where we are required to make a choice between a number of alternatives rather than only one or  two  for example, which school to join or which hostel to visit or still harder which girl to marry ( you almost always end up making a wrong decision is a different matter altogether) . serious C programming is same ; the choice we are asked to make is more complicated than merely selecting between two alternatives. C provides a special control statement that allows us to handle such cases effectively; rather than using a series of if statements. This control instruction is in fact the topic of this chapter . towards the end of the chapter we would also study a keyword called goto, and understand why we should avoid its usage in C programming.

Decision using switch:

   The control  statement that allows us to make a decision from the number of choices is called a switch, or more correctly a switch-case-default, since these three keywords go together to make up the control statement. The most often appear as follows:

switch ( integer expression )
{
    case constant 1:
               do this ;
    case constant 2:
               do this ;
   case constant  3:
               do this ;
    default:
               do this ;
}

What happens when we run a program containing a switch? first , the integer expression following the keyword switch is evaluated . The value it gives is then matched, one by one, against the constant values that follow the case statements. when a match is found, the program executes the statement following that case, and all subsequent case and default statements as well. if no match is found with any of the case statements, only the statements following the default are executed. a few examples will show how this control structure works.

Consider the following program:

  main ()
  {
        int i = 2
       switch (i)
       {
          case 1:
              printf (" i am in case 1 \n");
          case 2:
              printf ( "i am in case 2 \n");
          case 3 :
              printf ( "i am in case 3 \n");
           default:
              printf ( "i am in default \n");
       }
}

The out put of the program would be:
I am in case 2
I am in case 3
I am in default

The output is definitely not what we expected we didn't expect the second and third line in the above output. The program prints case 2 and 3 and the default case. well, yes. we said the switch executes the case where a match is found and all the subsequent cases and the default as well.
        If you want that only case 2 should get executed, it is upon you to get out of the switch then and there by using a break statement. The following examples shows how this is done . Note that there is no need for a break statement after the default, since the control comes out of the switch any way.

main()
{
     int i=2;
     switch (i)
{
       case 1:
           printf( " i am in case 1");
            break;
       case 2:
             printf ( "i am in case 2");
               break;
       case 3:
             printf ( "i am in case 3" );
               break;
       default:
              printf ( "i am in default");
    }
}

the output of the program would be:

I am in case 2





Thursday, 17 January 2013

Uses of logical operators:

Logical operators are use to combine two or more than two condition. Mean it use when we have more than one checks for a single condition.
We have the following three different logical operators.

1) && ( and operator).

 And operator is use to combine more than one condition and return true or false that depends on the given condition.
If will return true if all given conditions are true and it will return false if any single condition is false. And operator is denoted by double ampersand symbol. ( & &).
 For example:
   1) if ( a> 50 && b<100)
   2) if ( a> 50&& b<100 && c= =20)

2) || ( or operator).

      Or operator is use to combine more than one condition and return true or false that depends on the given condition.
It will return true if any single condition is true and it will return false if all conditions are false. Or operator is denoted by double symbol. ( ||)
For example:
      1) if( a>50&&b<100)
      2) if ( a>50|| b<100|| c= =20)
      3) if( a>50 || b<100&&c= =)

3) ! ( not operator)

       Not operator is denoted by sign (!).
   for example:
       if ( a! =40&&b!<90|| d!>100)etc.

Relational operators:

  <         less than                 1<5
  >        Greater than            50>30
 <=      less than equal to      per>=40
 >=     Greater than equal to   avg <= 90
 ==     equal to                      M==40
!=       not equal to                N! = farid

  

The Decision Control Structure

We all need to alter our actions in the face of changing circumstance. If the weather is fine, then i will go for the game . If highway is busy i would take a diversion. if she say no, i would look else where, you can notice that all these decisions depend on same condition being met.

   C language too much be able to perform different sets of actions - depending on the circumstances. C has three major decision making instructions - the if statement, the if else statement, and the switch statement. A fourth , some what less important structure is the one that used conditional operator. In this lecture we will learn all these ways. ( except switch, which has a separate lecture devoted to it later) in which a C program can react to changing circumstances.

Decision! Decision!

  As mentioned earlier, a decision control instruction can be implemented in C using:

a)  The if statement        b) The if- else statement          c) The conditional operators.

The if statement:

       Like most languages, C uses the keyword if to implement the decision control instruction.
The general form of if statement look like this.

if( condition)
 {
    do this;
}
The condition following the keyword if is always enclosed with in a pair of parentheses. If the condition what ever is true, then the statement is executed. If the condition is not true then the statement is not executed: instead the program skips past it.
     But who do we express the condition it self in c? as a general rule, we express a condition using C relational operator. The relational operators allow us to compare two values to see weather they are equal to each other unequal , or weather one is greater than other.

Multiple statement with in if :

if multiple statements are to be executed then they must be placed with in a pair of braces.

For example:

     if (condition)
     {
     statement 1;
     statement 2;
     statement 3;
  }

Nested if:

    Nested if mean that there is another if condition in if.
 For example:
  if ( condition)
{
    if ( condition)
      do this;
}


The if-else statement:

   If else is used when if statement fails than the else statement will execute.

Example1:                                              Example 3:
   if( condition)                                         if ( condition)
       do this;                                             {
     else                                                     do this;
       do this;                                              do this;
                                                                 }
Example 2:                                                 else
if ( condition)                                                {
do this;                                                              do this;
else                                                                   do this;
{                                                                  }
  do this;
  do this;
}

Note:

       Had there been only one statement in the if and one statement in else then we dropped the pair of braces but if there are more than one statement then we close the if and else in braces separately.

Nasted if-else statement:

    It is like nested if but we make another if -else is nested if else program.
For example:
 main()
     int i:
    printf ("enter either 1 or 2");
    scanf ( " %d",a);
if (i= =1)
    printf( " you would go to heaven");
else
   {  
     if( i= =2)
    printf ("hell was created with you in your mind");
    else
    printf( " how about mother earth"):
  }
}

Note:

   That the second if-else construct is nested in the first else statement. If the condition in the first if  statement is false, then the condition in the second if statement is checked . If it is false as well, then the final else statement is executed.

Tuesday, 15 January 2013

Expression in C Language

Expression:

              Expression is the combination of operators and operands.
               4+1 is expression now 2 is operand and + is operator.
Expression is divided in to two parts.

1) Operands:

              Operands are of two types constant for example ( 1, 2, 3) and variable for example ( a, b, c)

2) Operators:

             Any special symbol or computer program that perform specific task is called operator.

         Types of Operators:

   1.1) Unary Operator.
   2.1) Binary Operator.
   3.1) Ternary Operator.



1.1) Unary operator:

                Unary operator need a single value for example ( -5, +8) etc.

2.1) Binary operator:

           Binary operator need two value for example: ( 7-3, 6/7, 2*8) etc

3.1) Ternary operator:

          Ternary operator takes in a boolean value, and two statements and returns the return value of the first statement if the boolean value is true, and the return value of the second statement if the boolean value is false.

for example:
     z= (x >y)? x:y assigns x to z if x is greater than y, and otherwise assigns y to z ( the statement sets z equal to the maximum of x and y)
some programmers regard using  this ternary operator as a bad practice, though it can be useful in certain circumstance to avoid excessive if statement.

Priority of operators:

  Priority mean that in equation what value is first solve.

1) ()   

     In the equation C solve first those values which are enclosed in the bracket.

2) /, *,%

    The priority of  these three is same but C solve that operator first which is to the most left side to in the equation.

3) +,-

    The priority of these two are same but C solve that one first which is to the most left side in the equation.

4) = 

   last priority is of = .

We know some sign that for what purpose they are used but %, =, and ( ) we can not know now we can discus one by one .
for example:
 5/2 = 2.5 but when we put % sign then the answer is 5/2 = 1 because it show the reminder. It can not be applied on float. on using % the sign of the remainder is always same as the sign of the numerator. thus -5%2 yield -1, where 5/-2 yield 1.
= this sign is used to assign value to variable etc.
for example:
    a =67, t = 50 , a+d=u-t etc.

some example are there.
1) i = 2*3/4+4/4+8-2+5/8
 step wise solution:
    i=2*3/4+4/4+8-2+5/8
    = 6/4+4/4+8-2+5/8
    = 1+4/4+8-2+5/8
    = 1+1+8-2+5/8
    = 1+1+8-2+0
    = 2+8-2+0
    = 10-2+0 
    = 8+0
 i = 8 ans.
  
In the above equation 6/4 gives 1 and not 1.5 because both are integer values and 5/8 gives 0 because these both are integer value.