1 / 114

Programming Fundamentals

Programming Fundamentals. If you have programming experience (in any language other than C), Click here to skip the Programming Fundamentals part. If you are new to programming…, continue reading. Objectives. Understand the different types of programming languages.

odin
Download Presentation

Programming Fundamentals

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 Fundamentals

  2. If you have programming experience (in any language other than C), • Click here to skip the Programming Fundamentals part. • If you are new to programming…, continue reading.

  3. Objectives • Understand the different types of programming languages. • Understand the basic procedures in a program as input, processing and output. • Understand the importance of variables. • Understand a basic map of the program development cycle.

  4. Computer Components • CPU Central Processing Unit • RAM (Random Access Memory) • Mass storage devices • Input devices • Output Devices

  5. Application Software Word Processors Spreadsheets Painting programs Web browsers, email programs System Software Operating Systems Windows Macintosh OS Unix Linux Drivers Software Software is comprised of instructions that get a computer to perform a task.

  6. Programming Languages • Programming languages allow programmers to code software. • The three major families of languages are: • Machine languages • Assembly languages • High-Level languages

  7. Machine Languages • Comprised of 1s and 0s • The “native” language of a computer • Difficult to program – one misplaced 1 or 0 will cause the program to fail. • Example of code:1110100010101 111010101110 10111010110100 10100011110111

  8. Assembly Languages • A step towards easier programming. • Comprised of a set of elemental commands • tied to a specific processor. • Assembly language code needs to be translated to machine language before the computer processes it. • Example:ADD 1001010, 1011010

  9. High-Level Languages • High-level languages represent a giant leap towards easier programming. • The syntax of HL languages is similar to English. • Historically, we divide HL languages into two groups: • Procedural languages • Object-Oriented languages (OOP)

  10. Procedural Languages • Procedural languages • The focus of such languages is on sequence of activities to be carried out. • Based on specifying the steps the program must take to reach the desired state. • Examples include C, COBOL, Fortran, LISP, Perl, HTML, VBScript

  11. Object-Oriented Languages • Object-oriented languages: • Focus on modeling data. • Programmers code using “blueprints” of data models called classes. • Examples of OOP languages include C++, Visual Basic.NET and Java.

  12. Compiling • All HL programs need to be translated to machine code • so that a computer can process the program. • Programs may be translated using • a compiler: • translated all at once • An interpreter • translated line by line • Compiled programs typically execute more quickly than interpreted programs, but have a slower translation speed. • On the other hand, interpreted programs generally translate quicker than compiled programs, but have a slower execution speed.

  13. Programming Example • Simple programming problem: Convert a length from feet into meters. • Pseudocode: • Input the length of an item, LengthInFeet, in feet and Compute the length of the item in meters: • Write “Enter the length in feet” • Input LengthInFeet • Set LengthInMeters = LengthInFeet * .3048 • Write LengthInMeters

  14. Programming Example • Partial Code in C: • float LengthInFeet; • LengthInMeters = LengthInFeet * .3048; • printf (“%f”, LengthInMeters);

  15. Input & Variables • Input operations get data into the programs • A user is prompted to enter data: • Write “Enter the length in feet” • Input LengthInFeet • Computer programs store data in named sections of memory called variables. • In the example above, the variables are named LengthInFeet & LengthInMeters. • The value of a variable can, and often does, change throughout the program.

  16. Variables • Needed because we might want the program to run using different values: • E.g. we might want to calculate LengthInMeters for different values of LengthInFeet • Each value for which computation is to be carried out will be stored in the variable LengthInFeet one-by-one • Referring to the variable LengthInFeet by its name gives the value stored in it.

  17. Constants • Many a time we need to used fixed values for certain computations. • E.g. in the program given above • the conversion from miles to meters requires the length in miles to be multiplied by .3048 • a value that doesn’t (need to) change • Such values are constants • We may also give names to constants: such constants are called named constants

  18. Types of Data • Numeric Data • Integer data, I.e., whole numbers, 10 25 -45 0 • Floating point data – have a decimal point 23.0, -5.0 • Character data (alphanumeric) • All the characters you can type at the keyboard • Letters & numbers not used in calculations • Boolean data • TRUE/FALSE

  19. Data Processing and Output • LengthInMeters = .3048 * LengthInFeet • The above statement is a processing statement. • Take the value in the variable LengthInFeet, • Multiply by .3048, and • Set the value of the variable LengthInMeters to the result of the multiplication. • Also stated as assign the result of multiplication to the variable LengthInMeters • Write LengthInMeters • Output the value in LengthInMeters to the monitor.

  20. Assignment Statements • If the variable being assigned to already contains a data value, • the previously stored value is overwritten • E.g. the following statements: Age= 12; Age=15; Have an effect of storing 15 in the variable Age. • Reason: The value 12 (stored as an effect of the first assignment statement) gets overwritten after the execution of the second statement.

  21. Assignment Statements • The statement • Age = Age + 1 • Changes the value of the variable Age in such a way that the new value is one greater than the previous value. • Working: Take the value of Age, add 1, and store the result back in the same variable.

  22. Operations on Data • Operator is a symbol that is used to perform certain operations • Data values on which operation is performed are termed as operands • Operators generally work on many types of variables or constants, • though some are restricted to work on certain types. • some may even exhibit different behaviors with different data types • Operators in C may be: • Unary: Work on single operand • Binary: Work on two operands • Ternary: Work on three operands

  23. Operators • C, like most programming languages, supports operators to carry out various kinds of operations • We have operators for: • Arithmetic operations • Type Conversions • Performing Comparisons • Performing Logical Operations • Working on individual bits of data • And many more…

  24. Precedence of Operators • Operator precedence: • A set of rules decide the order of operations in an expression • Like BODMAS rule in mathematics • Operator Precedence clarifies unambiguously, which operations should be performed first in a given expression. • Parentheses i.e. () can always be used to improve clarity

  25. Associativity of Operators • Operator Associativity: • Rule used for two operators of equal precedence (i.e. same importance) • Associativity may be either of the following • left to right: left operator is evaluated first • right to left: right operator is evaluated first

  26. Structured Programming • A method for designing and coding programs in a systematic, organized manner. • It combines • the principles of top-down design, • modularity and • the use of the three accepted control structures of • sequence, • repetition and • selection.

  27. Control Structures • Sequence–in sequential order. • The simplest of control structures – • start at the beginning and continue in sequential order. • Selection – selectively execute statements • Called a branch, • it requires a condition to determine when to execute statements.

  28. Control Structures • Repetition – repeat statements more than once • Called a loop, • It needs a stop condition, • I.e, the program will continue to loop (repeatedly execute a set of statements) until some condition is met.

  29. Counter-Controlled Loop: (Definite Repetition) • If it is known in advance exactly how many times a loop will execute, the repetition is said to be counter-controlled • Counter-controlled loop: • Execute a set of statements repeatedly a specified number of times • The counter may be advanced according to programmers requirements .

  30. Event-Controlled Loop: (Indefinite Repetition) • If it is NOT known in advance exactly how many times a loop will execute, the loop is said to be event-controlled. • An event-controlled loop • Terminates when some event occurs. • The event may be based upon: • User input • Results of calculation • And many more….

  31. Developing a Program: Analyze the problem Design the program Code the program Test the program Steps in Developing a Program

  32. Step-1 Analyze the Problem • Brewster’s Thousands • The problem: Brewster wants to invest money at a local bank. There are many options such as interest rates, terms of deposit, compounding frequencies. He needs a program to compute, for any given initial investment, the final maturity (value) of the deposit. • What are the inputs? (given data) • What are the outputs? (required data) • How will we calculate the required outputs from the given inputs?

  33. Step-2 Design the Program • Create an outline of the program • An algorithm – a step by step procedure that will provide the required results from the given inputs. • Algorithm Examples: Instructions on how to make a cake, use the bank’s ATM, etc.

  34. Step-3 Code the Program • Once the design is completed, write the program code. • Code is written in some programming language such as BASIC, Pascal, C++, Java, etc.

  35. Errors in Development • While writing a program, certain errors may get induced in the code. • Errors may be categorized as : • Syntactical Errors – Incorrect Syntax (wrong grammar), i.e., breaking the rules of how to write the language • Forgetting punctuation, misspelling keyword • The program will not run at all with syntax errors • Semantic Errors: Incorrect Logic: • Using an incorrect formula, incorrect sequence of statements, etc. • The program runs, but does not produce the expected results.

  36. Step-4 Testing the program • Purpose: Locate any errors (bugs) / problems • Most of the work should be done before the phase begins – creating of a testing document – telling what to test & how. • Two types of testing: • Testing for errors • Quality testing • Testing is done throughout the development cycle • Desk-checking, or code walkthrough is performed to locate errors in the code. • Pretend you are the computer and execute your own code. • Ultimate test is to run the program to see if the outputs are correct for the given inputs.

  37. Good Programming Practice • It is best not to take the “big bang” approach to coding. • Use an incremental approach by writing your code in incomplete, yet working, pieces. • For example, for your projects, • Don’t write the whole program at once. • Just write enough to display the user prompt on the screen. • Get that part working first (compile and run). • Next, write the part that gets the value from the user, and then just print it out.

  38. Modular Programming • Determine the major tasks that the program must accomplish. • Each of these tasks will be a module. • Some modules will be complex themselves, and they will be broken into sub-modules, • and those sub-modules may also be broken into even smaller modules. • This is called top-down design

  39. Code Modules • A module is an independent, self-contained section of code that performs a single task. • The main module is the module that drives the application. • It “controls” all other modules. • Typically, the main module calls other modules in order to have them perform certain tasks.

  40. Program Control & Modules • When the main module calls another module, program control transfers to the called module. • Program control cedes back to the main module when the called module finishes.

  41. Structure Chart • A structure chart shows what modules exist and how they are related. • A structure chart is a top-down modular design tool • It is used in structured programming to arrange program modules into a tree • It’s a good idea to keep modules short – about 1 page per module. • We will have very small modules while getting comfortable with programming.

  42. Documentation • Internal Documentation • Comments explain to the reader the logic and decision processes of the programmer. • Comments are ignored by an interpreter or compiler. • External Documentation • External documentation includes a user’s guide and, typically, a more technical system administrator’s guide.

  43. Over to C Fundamentals…

  44. C Fundamentals

  45. Objectives • History of C • Characteristics of C • Converting a C Program to Executable • Dissecting a simple C Program • Defining Variables in C • Printing Out and Inputting Variables • Constants & Literals

  46. History of C • Developed by Brian Kernighan and Dennis Ritchie of AT&T Bell Labs in 1972 • In 1983 the American National Standards Institute began the standardization process • In 1989 the International Standards Organization continued the standardization process • In 1990 a standard was finalized, known simply as “Standard C”

  47. Features of C • C can be thought of as a “high level assembler” • Most popular programming language for writing system software • Focus on the procedural programming paradigm, with facilities for programming in a structured style • Low-level unchecked access to computer memory via the use of pointers • And many more…

  48. Writing C Programs • A programmer uses a text editor to create or modify files containing C code. • Code is also known as source code. • A file containing source code is called a source file. • After a C source file has been created, the programmer must invoke the C compiler before the program can be executed (run)

  49. Getting an executable program • The process of conversion from source code to machine executable code is a multi step process. • If there are no errors in the source code, the processes called compilation & linking produce an executable file • To execute the program, at the prompt, type <executable file name>

  50. Conversion from .C to executable • Preprocessing • Compilation • Linking

More Related