Pages

Showing posts with label computer. Show all posts
Showing posts with label computer. Show all posts

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.

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.
     

Thursday, 20 December 2012

Constant in C Language

Constant:

              The alphabets, numbers and special symbol when properly a combined from constants and variables and keywords. A constant is an entity that does not change where as a variable is an entity that may change.

Types of C Constant:

C constant can be divided in two major categories :
1) Primary constant.
2) Secondary constant.


These constant are further divide as 

Primary constant :                                            

a) Integer constant
b) Real constant
c) Character constant
d) Sting constant.

Secondary constant :

a) Array 
b) Pointer
c) Structure
d) Union
e) Enum etc.



Rules for constructing integer constants:

1) An integer constant must have at least one digit.
2) It must have one decimal point.
3) It can either be positive or negative.
4) If no sign than integer is considered to be positive.
5) No commas or blanks are allowed with in an integer constant.
6) The allowable range of an integer constant is -32768 to 32767.
   For example:
                    512, +126, -24 etc.

Rules for constructing real constants:

Real constant are often called floating points constant. The real constants could be written in two forms Fractional form and Exponential form.

1) A real constant must have digit.
2) It must have decimal point.
3) It could be either positive or negative.
4) Default sign is positive.
5) No commas or blanks are allowed with in a real constant.
    For example:
               +56.23, -456.01,76.875,-53792.1 etc  

Exponential form of representation of real constant :

In exponential form of representation of real constant is represented in two parts . The part appearing before "e" is called mantissa, where as the part following "e" is called exponent.

Following rules must be observed while constructing real constant expressed in exponential form.

1)  The mantissa part and the exponential part should be separated by a letter "e".
2) The mantissa part may have positive or negative sign.
3) Default sign of mantissa part is positive.
4) The exponent must have at least one digit, which must be a positive or negative integer. Default sign is positive.
5) Range of real constants expressed in exponential form is -3.4e38 to 3.4e38.
       For Example:
                      +3.2e-5,4.1e8,-0.2e+3 etc.

Rules for constructing character constant :

1) A character constant is a single alphabet, a single digit or a single special symbol enclosed with in single inverted commas. both the inverted commas should point to the left.
2) The maximum length of character constant can be 1 character.
  for example 
                    *A*  
       


Monday, 17 December 2012

C Language

What is c?

       c is a programming language developed at AT and TS bell laboratories of USA in 1972. It was designed written by a men named Dennis Ritchie.

Getting stated with c:

     However there is a close analogy between learning English and C language. The classical method of learning English is to first learn alphabets used in the language, then learn to combine these alphabets to form words which in turn are combined to form sentences and sentences are combined to form paragraphs
            Learning C is similar and easier instead of straight away learning how to write programs we must first know what alphabets, numbers and specials symbols are used in C, then how using them constants, variables and keywords are constructed and finally how these combined to form instruction. A group of instructions would be combined later on to form a program.This will explain by given structure.

   Steps in Learning English Language:

          Alphabets.......... > Words....... > Sentences....... > Paragraphs.


Steps In Learning C Language:

(1st  step)                       (2nd step)   

  Alphabets                 Constants                         (3rd step)             (4th step) 

      Digits                       Variables ............. >  Instruction......... > Program.

    Special symbols          Key words.


The Character set:

               A character denotes by alphabet , digit or special symbol used to represent information .

                                                                            Character code:

Alphabets  =     A, B ............Y,Z                      (65-90)

                            a , b.............y,z                      (97-122)

Digits =          0,1,2,3,4,5,6,7,8,9                    (48-57)

Special symbols =    ~ ` ! @ # $ % ^ & * ( ) + _ { } " : > < ? |   ( 0-47, 58-64,9196123-127 )      

       

            

Sunday, 16 December 2012

what is software

Software:

                 All computer program are called software that contain instruction for the system that what to do and how to do. in this part of computer system which consist of program and techniques that are necessary to get the hardware to work. we can see them but we can not touch them. we made software for computer because computer is a stupid device. it can not work without software.

Types of software: 

         Generally there are two types of software
      1) Appliction software.
      2) System software.

1) Application software:

                  These are that software that is used to perform any specialized functions and these are the programs which are used for the official use or for general purpose. without system software application software can not be installed.

Types of application software:

              There are two types of application software.
     1.1) General purpose software or application packages.
     1.2) Special purpose software or customized packages.

1.1) General purpose software:

       General purpose software is that software which is used for general purpose. General purpose software has enough features to accomplish a wide variety of tasks and they are easily available in the market and any one can used it according to his necessary.
for example:
                 Ms word, excel, auto cad, games etc.

2.1) Special purpose software:

       Special purpose software performs a very specific task and can not be change or programmed to perform a different task. It is specific for company institution, organization and even for a person.
for example:
    Bank software etc.

2) System software:

        All the software used to operate and maintain computer system are called system software.Without a system software, a computer is just an expensive hunk of junk. It work as a company administrator or as a class monitor.

 Types of system software:

     1) Operating system software.
     2) Translators.

2.1) Operating system software:

          A set of program used to control and monitor over all activites of a system is called operating system software.

Function of operating system software:

 1)  It provides interface or bridge between user and computer.
 2)  It is a heart of the system.
 3)  It manages hardware.
 4)  It manages software.
 5)  It manages memory.
 6)  It manages files. for example save, open and edit etc.

2.2) Language Translator: 

         Translator translate the users software into language that the "CPU" can understand. In other word language translators  are system software that converts application software into a specific machine language.

Types of Translator:

 1)  Compiler.
 2)  Interpreter.
 3)  Assembler.

2.2.1 Compiler: 

              Compiler are used for high level languages like c, c++ and java etc. Compiler check a program as a hole and notify all errors and then convert all the instruction to machine language or binary language and it is faster then the interpreter.

2.2.2) Interpreter:

            Interpreter is also used for high level languages like basic language etc. Interpreter check instruction step by step and then notify error other wise convert into machine language or binary language.

2.2.3) Assembler:

           The translator program that translates an assembly code into the computers machine code is called assembler. Assemblers are used for low level languages like Assemble language.