Pages

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.

Friday, 4 January 2013

C Instructions

C Instructions:

   There are basically three types of instruction in C.
     1) Type declaration instruction.
     2) Arithmetic instruction.
     3) Control instruction.

1) Type declaration instruction:

              This instruction is used to declare the types of variables being used in the program. Any variable used in the program must be declared before using it in any statement. The type declaration statements is written at the beginning of the main () function.
     For example:
            int a;
           float b;
          char name ; etc.

2) Arithmetic instruction:

           A C arithmetic instruction consist of a variable name on the left hand side of = and variable name and constant are on the right hand side of = the variables and constants appearing on the right hand side of = are
connected by arithmetic operator like +, -,*,/ and etc.
   For example:
        int a,b,c;
          a=10
         b=20+60
         c=a+b 

Thursday, 3 January 2013

To Explain The C Program

To explain the program:

1) main ():

          main () is a collective name given to a set of statements. This name has to be main (), it can not be any thing else. All statements that belongs to main () are enclosed with a pair of braces { } as shown below.

 main()
{
  statement 1;
  statement 2;
}

Technically speaking main () is a function has a pair of parentheses () associated with it.

2) printf ():

       It is also a function. It has divided in to two parts.Its is used for output purpose to print some thing on the screen.
     printf( "sting",variable list);
                part 1       part 2


part 1 :

    In part one we use the following 
1) Escape sequences.
2) Format specifier.
3) Literal  
4)  Field width.

Now we discuss only 1 and 2.


1) Escape sequences :

 1) \n It is used for one line break or new line.
 2) \b It is used for bake space.
 3) \f It is used for form feed.
 4) \\ It is used for backslash.
 5) \t It is used for tab.
 6) \r It is used for carriage return.
 7) \a It is used for alert beep.

2) Format Specifier :

    There are many format specifier but now we can discuss only three type of format specifier.

 1) %d it is used for integer specification.
 2) %f it is used for float or real specification.
 3) %c it is used for character specification.

Part 2:

     In part 2 we enter variable names etc.

What is scanf(): 

     scanf is an input statement. it is used to get value at the run time of a program .

For example:

 main()
{
int a;
printf(" enter value");
scanf(" %d",&a);
}

In the above example we see that scanf () function has two parts in first part we give format specifier and in the second part we give variable name.

Note:

   Put ampersand (&) before the variable in the scanf function must because & is an address operator.
It gives the location number used by the variable in memory.


Wednesday, 2 January 2013

C Keywords or Reserve Word

Keywords or Reserve word:

       Keywords are the words whose meaning has already been explained to the c compiler ( or in a broad sense to the computer). The keywords are also called reserve words.
           The keywords can not be used as a variable name because if we do so we are trying to assign a new meaning to keywords which are not allowed by the computer. However it would be safer not to mix up the variable names and the keywords.

       auto     double     int      struct
       break   else         long    switch
      case       enum   register  typedef
      char       extern  return     union
     const      float    short      unsigned
    continue  for      signed     void
     default    goto   sizeof       volatile
       do        if        static       while


Note:

     Note that compiler vendor ( like Microsoft ,Borland etc) provides their own keywords a part from the ones mentioned above.These include extended keywords like near, far, asm etc.




The First C Program :

Armed with the knowledge about the types of variable, constant and keywords. The next logical step is to combine them to form instructions.
      However, instead of this we would write our first  C program now.

First Remember Some Rules:

  1)  Each instruction in a C program is written as a separate statement there fore a complete C program would comprise of a series of statements.
2) All statements are entered in a small case letters.
3) Every C statement must end with  a ( ;) act as a statement terminator.
  
       Let us write down our first c program :
Program no: 1


#include<stdio.h>
void main ()
 printf(" welcome to my home");

Program no: 2

#include<stdio.h>
void main ()
{
int a ;
a=10;
printf("%d",a);
}
     

Variable in C ++

Variable:

                      An entity that may very during program exaction is called a variable. Variable names are names given to locations in memory. These locations can contain integer, real or character constants. In any language, the type of variables that is can support depends on the types of constant that it can handle. This is because of a particular type of variable can hold only the same type of constant.

Types of C Variable:

1) Integer Variable.
2) Float  Variable.
3) Character Variable.

1) Integer Variable:

          An integer variable can hold only an integer constant value. Integer variable declare by reserve word (int) mean integer.
     For example:
             int a :
    int is the type of variable and a is the name of the variable.
     a=5, d=745, t=43 etc.

2) Float Variable:

        A real variable can hold only real constant value. Float variable declare by reserve word ( float ).
        For example :
              float a;
            a=7.2, g=1.342, d=421.67 etc.

3) Character Variable:

         Character variable can hold only a character constant. Character variable is declared by reserved word ( char) mean character.
   For example:
         char a;
      a=*b*, a=*5*, a= *+*

Note:
     in character its not (*) its inverted commas.


Rules for constructing variable names:

1) A variable name is any combination of 1 to 8 characters but we can give up to 256.
2) The first character in the variable name must be an alphabet or underscore.
3) No commas or blanks are allowed with in a variable name.
4) No special symbol other than an underscore can be used in a variable name.
5) Variable name must be meaning full.
      For example:
              si_int  , hra,  pop_e_89, jack, etc.

Note:
       These rules remain same for all types of primary and secondary variables.