1 csc 1111 introduction to computing using c++ c++ basics (part 1)
DESCRIPTION
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.TRANSCRIPT
1
CSC 1111Introduction to Computing
using C++
C++ Basics(Part 1)
2
OutlinesSyntaxData TypesConstants / LiteralsVariablesAssignment OperatorIdentifiers, Reserved Words, Predefined
Identifiers
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
#include <iostream>
int main() { Cout << "Good morning!";
Cout << "\n"; return 0;}
Can you spot the error(s)?
123456789
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
Data
Data are represented as 0's and 1's by the computer.
Suppose the following data is stored at certain memory location:
00001111Can you tell what it represents?
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)
Value Type0.14 Real number
'A' Letter
26098400 Integer
26098400 Phone number
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
Numerical ConstantsHow 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
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
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
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
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
100num
You can view a variable as a box for storing data.
13
#include <iostream>
int main(){ int num; num = 20; cout << "My lucky number is "; cout << num;
return 0;}
123456789101112
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
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
#include <iostream>
int main(){ int num; num = 20; cout << "My lucky number is "; cout << num;
return 0;}
123456789101112
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
#include <iostream>
int main(){ int num; num = 20; cout << "My lucky number is "; cout << num;
return 0;}
123456789101112
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 numand pass the value (20) to cout for printing
17
#include <iostream>
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.
12345678910111213141516
18
#include <iostream>using namespace std;int main(){ int sum; int integer1, integer2;
integer1 = 10; integer2 = 20; sum = integer1 + integer2;
cout << integer1 << " + " << integer2 << " = " << sum << "\n";
return 0;}
12345678910111213141516
? ? ?sum integer1 integer2
Declared but uninitialized variables can contain any value.
19
#include <iostream>using namespace std;int main(){ int sum; int integer1, integer2;
integer1 = 10; integer2 = 20; sum = integer1 + integer2;
cout << integer1 << " + " << integer2 << " = " << sum << "\n";
return 0;}
12345678910111213141516
? 10 ?sum integer1 integer2
20
#include <iostream>using namespace std;int main(){ int sum; int integer1, integer2;
integer1 = 10; integer2 = 20; sum = integer1 + integer2;
cout << integer1 << " + " << integer2 << " = " << sum << "\n";
return 0;}
12345678910111213141516
? 10 20sum integer1 integer2
21
#include <iostream>using namespace std;int main(){ int sum; int integer1, integer2;
integer1 = 10; integer2 = 20; sum = integer1 + integer2;
cout << integer1 << " + " << integer2 << " = " << sum << "\n";
return 0;}
12345678910111213141516
30 10 20sum integer1 integer2
integer1 + integer2 is evaluated first.
The resulting value, 30, is then assigned to sum.
22
#include <iostream>using namespace std;int main(){ int sum; int integer1, integer2;
integer1 = 10; integer2 = 20; sum = integer1 + integer2;
cout << integer1 << " + " << integer2 << " = " << sum << "\n";
return 0;}
12345678910111213141516
30 10 20sum integer1 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
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. $abc2. _1_abc_1_3. 1_14. Domain-name5. URL6. int7. main8. Int9. _3210. c
Which are valid identifiers?
Names given by the programmers to identify variables, functions, etc. in the program
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.html
auto const double float int short struct unsignedbreak continue else for long signed switch voidcase default enum goto register sizeof typedef volatilechar 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
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 stdcout include INT_MAX iostream MAX_RAND NULL 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
By now, you should have known …C++ language has rules that you must followData have values and typesHow to represent values of some data in C+
+How to declare variables for keeping dataHow to assign values to variablesWhat names can be given to the variables
Next: How to process the data