Introduction to Programming 1 Introduction to Programming A



![Programming Languages • FORTRAN – (FORmula TRANslation [1957]) Designed for mathematical operations • BASIC Programming Languages • FORTRAN – (FORmula TRANslation [1957]) Designed for mathematical operations • BASIC](https://slidetodoc.com/presentation_image/760884a1a6b836911ec6588ee1e519cc/image-4.jpg)
![Programming Languages • Pascal – (Named for the mathematician [1970’s]) Designed to teach programming Programming Languages • Pascal – (Named for the mathematician [1970’s]) Designed to teach programming](https://slidetodoc.com/presentation_image/760884a1a6b836911ec6588ee1e519cc/image-5.jpg)






























- Slides: 35
Introduction to Programming 1
Introduction to Programming • A computer is a machine • It must be controlled • A program is the tool we use to control a computer • A computer program is a sequence of instructions that is used to operate a computer to produce a specific result. 2
Introduction to Programming Input Data Process the Data Output Results 3
Programming Languages • FORTRAN – (FORmula TRANslation [1957]) Designed for mathematical operations • BASIC – (Beginners All-purpose Symbolic Instruction Code [1960’s]) Designed for small interactive type programs • COBOL – (Common Business Oriented Language [1960’s]) Designed for business applications. 4
Programming Languages • Pascal – (Named for the mathematician [1970’s]) Designed to teach programming concepts to students • C – (upgrade from B, developed from BCPL [1970’s]). General purpose language 5
More about C • General purpose language • May be used to create simple interactive programs, or solve highly sophisticated and complex problems • C has many tools built into language; also it is quite easy to “add” tools/procedures to the language • C was designed “…to do work…” 6
Our first C program #include <stdio. h> int main() { printf("Hello World!n"); return 0; } 7
Our first C program # indicates pre-processor directive include is the directive stdio. h is the name of the file to "insert" into our program. The <> mean it is part of the c development system #include <stdio. h> int main() { printf("Hello World!n"); return 0; } 8
Our first C program main is the name of the primary (or main) procedure (this is different from the file name). All ANSI C programs must have a main routine named main The () indicate that main is the name of a procedure. All procedure reference must be followed with () #include <stdio. h> int main() { printf("Hello World!n"); return 0; } 9
Our first C program { } enclose a "block". A block is zero or more statements C statements. #include <stdio. h> int main() { printf("Hello World!n"); return 0; } 10
Our first C program printf() is a "built-in" function (actually it's defined in stdio. h). "Hello World!" is the string to print. More formally, this is called the control string or control specifier. #include <stdio. h> int main() { printf("Hello World!n"); return 0; } Every statement must end with a "; ". Preprocessing directives do not end with a "; " (but must end with a return). 11
Our first C program The int tells the compiler our main() program will return an integer to the operating system; the return tells what integer value to return #include <stdio. h> int main() { printf("Hello World!n"); return 0; } 12
Variations #1 of first program #include <stdio. h> int main() { printf("Hello"); printf("there"); printf("World!"); return 0; } 13
Variations #2 of first program #include <stdio. h> int main() { printf("Hellon"); printf("theren"); printf("World!n"); return 0; } 14
Variations #3 of first program #include <stdio. h> int main() { printf("Hellontheren. World!n"); return 0; } 15
Variations #4 of first program #include <stdio. h> int main(){printf ("Hellontheren. World!n"); return 0; } Note while this is syntactically correct, it leaves much to be desired in terms of readability. 16
C program #2 #include <stdio. h> int main() { printf("%f plus %f equals %fn", 15. 0, 2. 0, 15. 0 + 2. 0); printf("%f minus %f equals %fn", 15. 0, 2. 0, 15. 0 -2. 0); printf("%f times %f equals %fn", 15. 0, 2. 0, 15. 0*2. 0); printf("%f divided %f equals %fn", 15. 0, 2. 0, 15. 0/2. 0); return 0; } 17
C program #2 (rev 2) #include <stdio. h> int main() { printf("%f plus %f equals %fn", 15. 0, 2. 0, 15. 0 + 2. 0); printf("%f minus %f equals %fn", 15. 0, 2. 0, 15. 0 -2. 0); printf("%f times %f equals %fn", 15. 0, 2. 0, 15. 0*2. 0); printf("%f divided %f equals %fnn", 15. 0, 2. 0, 15. 0/2. 0); printf("%d plus %d equals %dn", 15, 2, 15 + 2); minus %d equals %dn", 15, 2, 15 -2); times %d equals %dn", 15, 2, 15*2); divided %d equals %dnn", 15, 2, 15/2); printf("%d printf("%d divided divided by by by %d %d %d equals equals %dn", 1, 10, 1/10); %dn", 4, 10, 4/10); %dn", 5, 10, 5/10); %dn", 6, 10, 6/10); %dn", 9, 10, 9/10); %dnn", 10, 10/10); printf("5 printed as a float is %fn", 5); printf("2000000 printed as a float is %fn", 2000000); printf("5. 0 printed as a decimal integer is %dn", 5. 0 f); printf("2000000. 0 prtd as a decimal intr is %dn", 2000000. 0 f); 18 return 0; }
Data Types and Arithmetic Operations 19
Four Types of Basic Data • • Integer Floating point (single precision) Double Precision Character int float double char 20
Integer Constants • Any positive or negative number without a decimal point (or other illegal symbol). • Legal 5 -10 +25 1000 253 -26351 +98 • Illegal 2, 523 (comma) 6. 5 (decimal point) $59 (dollar sign) 5. (decimal point) 21
Range of Integers (Machine Dependent) unsigned • char 0 255 -128 +127 • short int short 0 65535 • int 0 to 4294967295 long int -32768 + 32767 – 2147483648 2147483647 22
Floating Point and Double Precision Constants • Any signed or unsigned number with a decimal point • Legal 5. . 6 +2. 7 0. 0 -6. 5 +8. 43. 4 • Legal (exponential notation) 1. 624 e 3 7. 32 e-2 6. 02 e 23 1. 0 e 2 -4. 23 e 2 +4. 0 e 2 1. 23 e-4 +11. 2 e+7 • Illegal $54. 23 5 6, 349. 70 25 1 e 5 5. 3 e 3. 2 1. 0 E 5 23
Floating Point and Double Precision Constants • Range of float (single precision) ± 1. 175494351 E – 38 ± 3. 402823466 E + 38 • Range of double ± 2. 2250738585072014 E – 308 ± 1. 7976931348623158 E + 308 24
Significant Digits Type Number of Bytes Number of Significant Digits float 4 6– 7 double 8 15 -16 25
Precision Type Size of Exponent Size of Mantissa float 8 bits (one bit = sign) 23 bits double 11 bits (one bit = sign) 52 bits 26
Accuracy vs. Precision (Digital Clock) 07: 30: 45 What is the Precision? What is the Accuracy? 27
Accuracy vs. Precision • Precision – is how closely you can read a value from a given device • Accuracy – is how close a given device is to some fixed standard 28
Character Constants • Stored in ASCII or UNICODE • Valid character constants ’A’ ’B’ ’d’ ’z’ ’ 1’ ’!’ ’+’ ’>’ ’? ’ ’ ’ • Invalid character constants ’VIALL’ ’’ ’CR’ ’’’’ ’”’ ’ 2’ ’#’ ’LF’ ”Q” 29
Character Constants (Escape Sequences) Char ’b’ Meaning Backspace Char ’’’ Meaning Single Quote ’f’ Formfeed ’”’ Double Quote ’n’ Newline ’nnn’ Octal constant ’r’ Return ’xnn’ Hex constant ’t’ Tab ’\’ Backslash 30
Arithmetic Operations Operation Addition Subtraction Multiplication Division Modulus Division (Remainder) Operator + * / % 31
Arithmetic Operations 3+7 18 - 3 12. 62 + 9. 8. 08*12. 3 12. 0/ 2. 0 Note – Spaces may be used freely between a constant and an operator. Spaces may not be used within a constant. 10/5 10/3 10 % 3 12 % 5 32
Results of Arithmetic Operations 3+7 18 10 - 3 15 12. 62 + 9. 8 22. 42 . 08*12. 3 0. 984 12. 0/ 6. 0 2. 0 10/5 2 10/3 3 10 % 3 1 12 % 5 2 (not 3. 333…) 33
Summary of Arithmetic Operators Operation Operator Type Operand Result Addition + Both integers Integer One non-integer Double precision Both integers Integer One non-integer Not Allowed integer float/double Subtraction Multiplication Division Modulus Negation * / % - Binary Binary Unary 34
Operator Precedence and Associativity Operator Associativity Innermost ( ) Left to right Unary - Right to left * Left to right + / % - Left to right 35