Topic 2: Type, Operators and Expressions
Topic 2: Type, Operators and Expressions
Topic 2: Type, Operators and Expressions
Welcome to c++!
c++!
Comments
◦ Text surrounded by /* and */ is ignored by the C++ compiler
◦ Used to describe program
1 /* A first program in C++ */
2 #include directive tells the compiler to read the file
3 #include <iostream> iostream
4
5 using namespace std;
6 Tells the compiler to locate names such as cout
7 int main() /* function main begins program execution */ in the “standard name space”
8{
9 cout<< “Welcome to c++!\n”;
10
11 return 0; /* indicate that program ended successfully */
12 } /* end function main */
Welcome to c++!
c++!
#include <iostream>
◦ # - Preprocessor directive
Tells computer to load contents of a certain file
◦ <iostream>
The file contains the definition for the stream input/output package
Eg. Cout – print the string on the screen
1 /* A first program in C++ */
2
3 #include <iostream>
4
5 using namespace std;
6
7 int main() /* function main begins program execution */
8{
9 cout<< “Welcome to c++!\n”; Left brace declares beginning of main function
10
11 return 0; /* indicate that program ended successfully */
12 } /* end function main */
int main()
◦ C++ programs contain one or more functions, exactly one of
which must be main
◦ Parenthesis used to indicate a function
◦ int means that main "returns" an integer value
◦ Braces ({ and }) indicate a block
The bodies of all functions must be contained in
braces
Print a Line of Text - cout
return 0;
◦ A way to exit a function
◦ return 0, in this case, means that the program
terminated normally
1 /* A first program in C++ */
2
3 #include <iostream>
4
5 using namespace std;
6
7 int main() /* function main begins program execution */
8 { cout statement starts printing from where the
9 cout << “Welcome to”; last statement ended, so the text is printed on
10 cout <<c++!\n”;
one line.
11
12 return 0; /* indicate that program ended successfully */
13 } /* end function main */
Welcome to c++!
c++!
1 /* A first program in C++ */
2
3 #include <iostream>
4
5 using namespace std;
6
7 int main() /* function main begins program execution */
8 {
9 cout << “Welcome\n to \n c++!\n”;
10
11 return 0; /* indicate that program ended successfully */
12 } /* end function main */
Welcome
to
c++!
c++!
1 /* A first program in C++ */
2
3 #include <iostream>
4
5 using namespace std;
6
7 int main() /* function main begins program execution */
8 {
9 cout << “Welcome “<<endl;
10 cout << “to c++!\n”;
10
11 return 0; /* indicate that program ended successfully */
12 } /* end function main */
Welcome
to c++!
c++!
int integer1; Variables – a location in
int integer2;
memory where a value
can be stored for use by
int sum;
the program
An identifier that can
consists of letters, digits,
Data types – and underscore (_) that
int means that does not begin with a
these variables digit
will hold any Case sensitive
integer values
General form for definitions of variables:
datatype variable;
OR
datatype variable1, variable2,……,
variable n;
Eg.
int integer1, integer2, sum;
C++’s Keywords
Keywords
◦ Special words reserved for C++
◦ Cannot be used as identifiers or variable names
Keywords
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while
int - stores integers (-32767 to +32768)
unsigned int – 0 to 65535
char – holds 1 byte of data (-127 to 128)
unsigned char – holds 1 byte (0 to +255)
long – usually double int (signed)
unsigned long – positive double int
float – floating point variable
double – twice of a floating point variable
cin obtains a value from the user
and assigns it to integer1
Assignment Operator
To print the sum:
OR
Addition + f+7 f + 7
Subtraction – p–c p - c
Multiplication * bm b * m
Division / x
x y or or x ÷ y
y x / y
Remainder % r mod s r % s
Precedence of Arithmetic Operators.
if ( payCode = 4 )
cout << "You get a bonus!\n" ;
c++;
cout << c ;
Operator Sample expression Explanation
Equality operators
= == x == y x is equal to y
≠ != x != y x is not equal to y
Relational operators
0 0 0
0 nonzero 0
nonzero 0 0
nonzero nonzero 1
0 0 0
0 nonzero 1
nonzero 0 1
nonzero nonzero 1
0 1
nonzero 0