Eceg 1052 CH 2
Eceg 1052 CH 2
Eceg 1052 CH 2
PART 1 OF CHAPTER 2
Basics of C++
In C++ programming, what follows after // symbols
on the same line is considered as a comment,
Descriptions can be enclosed in between /* and */
as multiple lines of comments.
It is done for the convenience of the reader.
system("pause");
return 0;
}
Computer Programming 12/20/2022 12
Input and Output
#include<iostream>
#include<math.h> Area of a triangle having sides a,
using namespace std; b and c is given by,
int main( ) s = ( a + b + c )/2
{ area = ( s * (s-a) * (s-b) * (s-
float a, b, c, s, area; c) )1/2
s = (a + b + c) / 2;
area= sqrt( s * (s-a) * (s-b) * (s-c) );
Triangle
cout << "Area = " << area << endl;
return 0;
}
Computer Programming 12/20/2022 13
Named Constants
Syntax:
const datatype CONSTANTNAME = value;
Circle
system("pause");
return 0;
}
Computer Programming 12/20/2022 16
#include <stdio>
What looks the output like?
int main ( ) { Study how it became 👇
int n = 4, k = 2;
cout << ++n << endl; cout << n << endl;
cout << n++ << endl; cout << n << endl;
cout << -n << endl; cout << n << endl;
cout << --n << endl; cout << n << endl;
cout << n-- << endl; cout << n << endl;
cout << n + k << endl; cout << n << endl;
cout << k << endl;
cout << n << k << endl; cout << n << endl;
cout << " " << n << endl; cout << " n" << endl;
cout << "\n" << endl;
cout << " n * n = "; //CAREFUL!
cout << n * n << endl;
cout << 'n' << endl;
return 0;
}
Computer Programming 12/20/2022 17
continued
int x, a, b, c;
a = 2; What will be
b = 4;
c = 5; the value of
x = a-- + b++ - ++c; x?
int x, a = 4;
Why x = 19?
x = ++a + ++a + ++a;
bool b = true;
int i = b;
cout << b << endl;
cout << i << endl;
Computer Programming 12/20/2022 33
Quiz 1
1, Explain the differences exist in the expressions
below, given that x = 7 :
( 6 < x++ ) || ( ++x < 8 )
vs.
( 6 < x++ ) | ( ++x < 8 )