Download presentation
Presentation is loading. Please wait.
Published byEgbert Montgomery Modified over 9 years ago
1
Computer Programming Basics Assistant Professor Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea
2
Pre-compiler directive Opening brace Closing brace Opening brace Closing brace Structure of a C++ Program
3
Without namespace Hello World! Namespace std contains all the classes, objects and functions of the standard C++ library. #include int main () { std::cout << "Hello world!\n"; return 0; }
4
Preprocessor Directives #include “I want to use a predefined library called iostream” Always start with a ‘#’ iostream: a library for inputs (from e.g., a user) and outputs (to e.g., the monitor)
5
“using” Directives using namespace std; “I want to use objects in a name group ‘std’ ” Tells the compiler where to look for names in the library Can deal with the situation where two or more objects in different libraries share a same name (naming confliction). –Read Appendix N for more about namespace
6
main function int main() The main body of the program. Compiler first tries to locate “main()” to find where to begin the program In the form of a function –I will cover “function” soon
7
Comment Internal program document Not considered as a program code Start of comment End of comment Start of comment End of comment
8
Nested Block Comments are Invalid
9
Variables Named memory locations that have a type –Named: identifier –Type: needs declaration What you can do with variables –Storing data –Modifying data –Reading data
10
Variables and Identifiers Memory Address of memory: Hard to remember Identifier: name of address
11
Variables and Identifiers Memory studentID studentGrade1 studentGrade2 Identifiers
12
Variables and Identifiers Memory studentID studentGrade studentName Compiler keeps track of [identifier-address] table
13
Variables and Identifiers In program studentID_Total_Grade = studentGrade1 + studentGrade2
14
Naming Identifiers Allowed characters: A-Z, a-z, 0-9, _ (underscore) Not allowed to start with a digit. E.g., 3class (x), class3(o) The identifier cannot duplicate a reserved word. e.g., if, case, while… Good names descriptive but short C++ is case sensitive; PI, Pi and pi are different.
15
Standard Data Types
16
Integer and Floating Point Types 2 or 4 Bytes 4 Bytes 2 Bytes 8 Bytes 10 Bytes 4 Bytes Size of value type depends on computer architecture
17
Maximum/Minimum of Integer Value Type TypeSignByteMinimum valueMaximum value short int/short signed 2 -32,76832,767 unsigned065,535 int (PC) signed 2 -32,76832,767 unsigned065,535 int (Mainframe) signed 4 -2,147,483,6482,147,483,647 unsigned04,294,967,295 long int/long signed 4 -2,147,483,6482,147,483,647 unsigned04,294,967,295
18
Maximum/Minimum of C++ data types
19
Variables Declaration
20
Variable Initialization Variable declaration ≠ variable initialization Should be initialized by a programmer before it is used e.g., int count; declaration (o), initialization(x) char grade = ‘d’; declaration (o), initialization(o)
21
Constants Data values that cannot be changed during program execution E.g., –3.141592 –‘d’ –“Hello word” –‘\0’
23
To Remember A character constant is enclosed by the single quotes. (e.g. ‘a’) Use double quotes for string constants. (e.g. “Jeon, Seokhee”) bool types are treated as a number. True: non-zero. False: zero.
24
Standard streams A mapping between data and input/output device
25
More about cout width(int) function sets the width for printing a value Only works until the next insertion command comes int x = 42; cout.width(5); cout << x << ‘\n’; // Outputs 42
27
More about cout fill(char) function sets the fill character. The character remains as the fill character until set again. int x = 42; cout.width(5); cout.fill(‘*’); cout << x << ‘\n’; // Outputs ***42
29
More about cout precision (int) sets the number of significant digits of float type numbers float y = 23.1415; cout.precision(1); cout << y << '\n'; // Outputs 2e+01 cout.precision(2); cout << y << '\n'; // Outputs 23 cout.precision(3); cout << y << '\n'; // Outputs 23.1
30
More about cout Output Manipulators (not a function) endl - outputs a new line character, flushes output dec - sets int output to decimal hex - sets int output to hexadecimal oct - sets int output to octal #include int x = 42; cout << oct << x << endl; // Outputs 52\n cout << hex << x << endl; // Outputs 2a\n cout << dec << x << endl; // Outputs 42\n
32
Example codes reading (Program 2-2) #include using namespace std; int main (void) { int a; int b; int c; int sum; cout << "Welcome. This program adds\n"; cout << "three numbers. Enter three numbers\n"; cout \n"; cin >> a >> b >> c; // Numbers are now stored in a, b, and c. Add them. sum = a + b + c; cout << "\nThe total is: " << sum << "\n"; cout << "\nThank you. Have a good day.\n"; return 0; }// main Welcome. This program adds three numbers. Enter three numbers in the form: nnn nnn nnn 11 22 33 The total is: 66 Thank you. Have a good day. Welcome. This program adds three numbers. Enter three numbers in the form: nnn nnn nnn 11 22 33 The total is: 66 Thank you. Have a good day.
33
Try to understand other examples in textbook! Program 2-3 ~ 2-13
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.