Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CSC 1111 Introduction to Computing using C++ C++ Basics (Part 1)

Similar presentations


Presentation on theme: "1 CSC 1111 Introduction to Computing using C++ C++ Basics (Part 1)"— Presentation transcript:

1 1 CSC 1111 Introduction to Computing using C++ C++ Basics (Part 1)

2 2 Outlines Syntax Data Types Constants / Literals Variables Assignment Operator Identifiers, Reserved Words, Predefined Identifiers

3 3 Language Syntax Like grammar and spellings in English Each language has its own syntax (defined by a set of rules) These rules are very strict and must be obeyed. A program can only be compiled successfully if it is free of syntax errors.

4 4 #include int main() { Cout << "Good morning!"; Cout << "\n"; return 0; } Can you spot the error(s)? 123456789123456789 Some Basic Rules C++ is case sensitive Space, newline, and tab characters are all considered as whitespace characters. Multiple whitespace characters are treated as one whitespace character.

5 5 Data Data are represented as 0's and 1's by the computer. Suppose the following data is stored at certain memory location: 00001111 Can you tell what it represents?

6 6 Data Type A datum has a value and type The data type tells us  What the value represents  How we can manipulate the datum (or how the computers process the datum) ValueType 0.14Real number 'A'Letter 26098400Integer 26098400Phone number

7 7 C++ Common Data Types int – integers (whole numbers) double – real or floating point numbers char – characters string – strings (sequence of characters) bool – Boolean ( true or false )

8 8 Numerical Constants How we can represent numerical values in C++ code. Integers  Numbers without decimal points  0, -100, 2048, 203139  1,000,000 (Wrong! Cannot use comma) Real or floating point numbers  Numbers which can have decimal points  0.0, -100.230, 3.1416,.244, -.9101, 1. A decimal point makes a big difference!  10 represents an integer  10.0 or 10. represents a floating point number  Different types are given different treatments in C++

9 9 Numerical Constants (Trivia) There are other ways to represent integer or real number constants in the C++ program. Can you figure out what they represent?  10e+2  -3.1e-5  12

10 10 Characters and String Constants Characters  Enclosed by a pair of single-quote characters ( ' )  'A', 'B', 'a', '0', '$'  '\n' (New line), '\t' (Tab), '\'' (Single quote)  ' ' (Space) Strings  A sequence of characters  Enclosed by a pair of double-quote characters (")  "0123456789", "\n", "Hello World!"

11 11 Keeping data in the program … Data are stored in memory. How can we read data from or write data to a memory location in a C++ program?  One possible way is through variables

12 12 Variable (vary + able) A variable corresponds to a location in the memory. A variable has a name  We use the name to refer to the memory location A variable has a type  The type determines what kind of value the variable can keep 100 num You can view a variable as a box for storing data.

13 13 #include int main() { int num; num = 20; cout << "My lucky number is "; cout << num; return 0; } 1 2 3 4 5 6 7 8 9 10 11 12 My lucky number is 20 num is a name given to the variable by the programmer. int is the type of the variable. A variable needs to be declared first before we can use it in the program. Line 6 declares that num is a variable that can store integers.

14 14 Declaring Variables Variables need to be declared first before they can be used in the program to store data. Syntax type2 var1, var2, …, varN; type1 variable1; Declaring multiple variables of the same type. Declaring a single variable of type "type1" There are rules regarding what names you can or cannot give to variables (See "Identifier").

15 15 #include int main() { int num; num = 20; cout << "My lucky number is "; cout << num; return 0; } 1 2 3 4 5 6 7 8 9 10 11 12 My lucky number is 20 Assignment Operator ( = ) It copies the value on its right to the variable on its left. Assign 20 to variable num

16 16 #include int main() { int num; num = 20; cout << "My lucky number is "; cout << num; return 0; } 1 2 3 4 5 6 7 8 9 10 11 12 My lucky number is 20 When a variable is not on the left of an assignment operator ( = ), we read the data stored in that variable. Evaluate the value of num and pass the value (20) to cout for printing

17 17 #include int main() { int sum; int integer1, integer2; integer1 = 10; integer2 = 20; sum = integer1 + integer2; cout << integer1 << " + " << integer2 << " = " << sum << "\n"; return 0; } 10 + 20 = 30 A C++ program that adds two numbers and output their sum. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

18 18 #include using namespace std; int main() { int sum; int integer1, integer2; integer1 = 10; integer2 = 20; sum = integer1 + integer2; cout << integer1 << " + " << integer2 << " = " << sum << "\n"; return 0; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ??? suminteger1 integer2 Declared but uninitialized variables can contain any value.

19 19 #include using namespace std; int main() { int sum; int integer1, integer2; integer1 = 10; integer2 = 20; sum = integer1 + integer2; cout << integer1 << " + " << integer2 << " = " << sum << "\n"; return 0; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ?10? suminteger1 integer2

20 20 #include using namespace std; int main() { int sum; int integer1, integer2; integer1 = 10; integer2 = 20; sum = integer1 + integer2; cout << integer1 << " + " << integer2 << " = " << sum << "\n"; return 0; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ?1020 suminteger1 integer2

21 21 #include using namespace std; int main() { int sum; int integer1, integer2; integer1 = 10; integer2 = 20; sum = integer1 + integer2; cout << integer1 << " + " << integer2 << " = " << sum << "\n"; return 0; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 301020 suminteger1 integer2 integer1 + integer2 is evaluated first. The resulting value, 30, is then assigned to sum.

22 22 #include using namespace std; int main() { int sum; int integer1, integer2; integer1 = 10; integer2 = 20; sum = integer1 + integer2; cout << integer1 << " + " << integer2 << " = " << sum << "\n"; return 0; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 301020 suminteger1 integer2 You can pass multiple data to cout in one statement. You can break a very long statement into multiple lines. (See rules about whitespace) 10 + 20 = 30

23 23 Identifiers Contains only:  Letters (' A ' – ' Z ', ' a ' – ' z '),  Digits (' 0 '-' 9 '), and  Underscore characters ' _ ' First character cannot a digit Cannot be one of the reserved words Case sensitive 1.$abc 2._1_abc_1_ 3.1_1 4.Domain-name 5.URL 6.int 7.main 8.Int 9._32 10.c Which are valid identifiers? Names given by the programmers to identify variables, functions, etc. in the program

24 24 Reserved Words Names that have special meaning in the C++ language. We cannot use these names as identifiers. Ref: http://cs.stmarys.ca/~porter/csc/ref/cpp_keywords.htmlhttp://cs.stmarys.ca/~porter/csc/ref/cpp_keywords.html auto const double float int short struct unsigned break continueelse for long signed switch void case default enum goto register sizeof typedef volatile char do extern if return static union while asm dynamic_cast namespace reinterpret_cast try bool explicit new static_cast typeid catch false operator template typename class friend private this using const_cast inline public throw virtual delete mutable protected true wchar_t and bitand compl not_eq or_eq xor_eq and_eq bitor not or xor

25 25 Predefined Identifiers Names that are valid identifiers but they have been used to identify something special or something commonly used in the program. cin endl INT_MIN iomanip main npos std cout include INT_MAX iostream MAX_RANDNULL string Avoid using them as identifiers in your program What would happen if you use these names as identifiers in your program? Some of the commonly seen predefined identifiers

26 26 By now, you should have known … C++ language has rules that you must follow Data have values and types How to represent values of some data in C++ How to declare variables for keeping data How to assign values to variables What names can be given to the variables Next: How to process the data


Download ppt "1 CSC 1111 Introduction to Computing using C++ C++ Basics (Part 1)"

Similar presentations


Ads by Google