1 / 21

Introduction to Computing

Programming is an activity that is carried out through some programming language.

josh
Download Presentation

Introduction to Computing

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming is an activity that is carried out through some programming language. Programming is instructing the computer how to solve a computational problem. In a computational problem, there will be some input, and for each input there is some desired output. A program instructs how to obtain the desiredoutput from the given input. Introduction to Computing

  2. Notion of a program A program is a sequence of instructions to the computer. For example, a program for adding two numbers may be that sequence of instructions which tells the computer how to carry out the algorithm for addition we had learnt in school. Given any two numbers to be added, the computer executes the program to find the result. Here the input is the two numbers, and the output is their sum.

  3. Other examples A more complex example of a computational problem is the monthly pay-bill computation. Here, the input is various data about the employees of a firm--- their attendance record, overtime record, salary, overtime rate etc. The output is the amount to be paid to each employee. The program bodies the method of computing monthly salary.

  4. A program is a series of easy to follow steps: A computer knows how to carry out some simple instructions. Let us explain this idea by an example. Suppose we have a computer that knows how to carry out the instruction drawline (x1, y1, x2, y2) (x1, y1) and (x2, y2) are co-ordinates of any two points on the screen. This instruction makes the computer draw on the screen a straight line from point (x1, y1) to point (x2, y2).

  5. Complex tasks from simple tasks: The four instructions below drawline ( 0, 0, 0,10) drawline ( 0,10,10,10) drawline (10,10,10, 0) drawline (10, 0, 0, 0) when executed, will draw a square on the screen. Thus by performing a series of simple instructions, each drawing a straight line, we have managed to get a more complex task, drawing a square, done.

  6. Expressions, assignment Suppose a program has the variables x, y, z, and tp.Let us say that these variables have values 5, 3, -9, and 50.Consider 2 * ( x + y ) + tpThis is an expression, and it evaluates to a value, in this case, its value is 66. A fundamental instruction is assignment which assigns the value of an expression to a variable.The general form of assignment instruction is variable= expressionwhich tells the computer to first evaluate expression to get a value, and then assign this value to the variable.

  7. Sequencing of instructions is important Remember that executing a program is executing a sequence of instructions, which is what the program is. A program starts its execution by first executing the first instruction, then the second instruction, and so on. Execution of the program ends when there are no more instructions left to be executed. The order of sequencing of instructions is important. For example, the effect of two sequences. Sequence 1 y = x + y x = x + 1

  8. Sequencing is important: cont'd and Sequence 2 x = x + 1 y = x + y are different, even though both have the same two assignments.To see this, suppose we start with the case: x has 3, y has 5. Then effect of sequence 1 is:x has 4, y has 8.But the effect of sequence 2 will result in: x has 4, y has 9.

  9. Other kinds of instructions There are instructions which do something if some condition holds. For example,if ( x == y ) z = z + 1The effect of this instruction is to increment z by 1 only if x and y both have the same value, otherwise no value gets changed.An example of another form of such a conditional instruction is if ( x == y ) z = z + 1 else z = z - 1This instruction increments z by 1 if x and y have same value, else it decrements z by 1.

  10. Expressions evaluating to true/false In the above ( x == y ) is also an expression; its value is, however, not a number but true or false.Such expressions are called boolean expressions

  11. Iterative Instructions In a program, some action might need to be repeated till some condition is met. Iterative instructions help us in achieving this. One important instruction in this class has the formwhile( B ) S where B is a boolean expression, and S is a sequence of one or more instructions. On reaching such a statement, first B is tested, if false nothing gets done. But if B is true, S is done, and then B is tested again. If false, the work is over, otherwiseS is done again, and so on. S gets done repeatedly till B becomes false.

  12. Example Here is a `program', using `instructions‘ that we have seen. a = x b = y while( a != b ) if( a > b ) a = a - b else b = b - aHow do we understand what this program is doing? We can see that there are four variables, x, y, a, and b, and the instructions modify a and b.To understand what the program does, we need to observe how the values of the variables change.

  13. When to observe To see changes, we would like to observe the values of variables just before and just after each instruction.But after an instruction, usually, another instruction follows.Therefore, we observe the variables just before an instruction is about to be executed.

  14. Example: cont'd

  15. Example: Cont’d When both a,b have the same value, (here it was 2), the test in while instruction evaluates to false. That ends the while instruction. After this, our example program had no other instructions to execute, therefore, the execution of the program ends. What we have done is to hand-execute the program. The final value of a in this case is 2. Had we started with x as 12 and y as 8, the final value of a would have been 4. In general, the final value of a will be the gcd of x and y, provided both are greater than 0. Therefore, the program is doing something useful! To compute gcd of x and y, we can execute the program, and print the final value of a as the result.

  16. Examples of Problems • Now, we shall discuss some problems which is to be solved using the computers. • We shall design a method to solve the problem, and then implement the method using high level programming language constructs.

  17. Checking case class upperCaseCharacter{ public static void main(String arg[]){ char c=‘0’; boolean isuppercase, islowercase; isuppercase = ((c >= ‘A’) && (c <= ‘Z’)); islowercase = ((c >= ‘a’) && (c <= ‘z’)); System.out.ptintln(“The character is ” + c + “. Uppercase: ” + isuppercase); } }

  18. Digits in AP class digitsInAP{ public static void main(String arg[]){ int x = 121; int d0, d1, d2; boolean isAP; d0 = x % 10; d1 = (x/10) % 10; d2 = x/100; isAP = ((d0+d2) == (2*d1)); System.out.println(“The number is: ” + x + “. Digits in AP? ” + isAP); } }

  19. Divisibility by 4 class divisibleByFour{ public static void main(String arg[]){ int x = 121; int lastTwoDigits; boolean isdivBy4; lastTwoDigits = x%100; isdivBy4 = ((lastTwoDigits % 4) == 0); System.out.println(“The number is: ” + x + “. Divisible by 4? ” + isdivBy4); } }

  20. Exchanging numbers class swap1{ public static void main(String arg[]){ int x = 121; int y = 200; int temp; System.out.println(“x: ” + x + “, y: ” + y); temp = x; x = y; y = temp; System.out.println(“x: ” + x + “, y: ” + y); } }

  21. Exchanging numbers class swap2{ public static void main(String arg[]){ int x = 121; int y = 200; System.out.println(“x: ” + x + “, y: ” + y); x = x + y; // Could overflow y = x – y; x = x – y; System.out.println(“x: ” + x + “, y: ” + y); } }

More Related