Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Eceg 1052 CH 2

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 34

Basics of C++

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.

 C++ is case sensitive ( i.e. A and a are different. )


 A statement ends by semicolon ( e.g. int x; )
 The compiler ignores all spaces and new line.

E.g. int x = 5; is the same as int x after compiled.


= 5;
Computer Programming 12/20/2022 2
Primitive Data Types
Data type Range of values

byte -128 .. 127 (8 bits)

short -32,768 .. 32,767 (16 bits)

int -2,147,483,648 .. 2,147,483,647 (32 bits)

long -9,223,372,036,854,775,808 .. ... (64 bits)

float +/-10-38 to +/-10+38 and 0, about 6 digits precision

double +/-10-308 to +/-10+308 and 0, about 15 digits precision

char Unicode characters (generally 16 bits per char)

boolean true or false (1 or 0)


Computer Programming 12/20/2022 3
Type Compatibility and Conversion
Widening conversion: In operations on mixed-type
operands, a numeric type of the smaller range is
converted to the numeric type of the larger range
 In an assignment, a numeric type of smaller range
can be assigned to a numeric type of larger range
 kind
→ byte to short to int to long kind to float to double

int i = 'a'; // Same as: int i = (int)'a';


char c = 97; // Same as: char c = (char)97;
int i = (int)5.4; // It is the same as the following:
int i = static_cast<int>(5.4); // so, truncate it to 5
cout << static_cast<double>(1) / 2; // displays 0.5
Computer Programming 12/20/2022 4
ASCII Character Set is a subset of the
Unicode from \u0000 to \u007f

Computer Programming 12/20/2022 5


ASCII Character Set is a subset of the
Unicode from \u0000 to \u007f

Note: the ASCII at \u007F is similar to delete

Computer Programming 12/20/2022 6


Escape Sequences for Special Characters
Description Escape Sequence Unicode
Bell sound \a \u0007
Backspace \b \u0008
Tab \t \u0009
Linefeed \n \u000A
Carriage return \r \u000D
Backslash \\ \u005C
Single Quote \' \u0027
Double Quote \" \u0022
Computer Programming 12/20/2022 7
Increment and Decrement Operators

Computer Programming 12/20/2022 8


Examples

Computer Programming 12/20/2022 9


Declaring and Setting Variables
Declaration Syntax: type variable_name;
E.g. int square; // declaring variable called square.
square = n * n;
Initialization Syntax:
type variable_name = value_or_expression;
E.g. double cube = n * (double)square;
 You can generally initialize local variables where
they are declared.
 All variables get a safe initial value anyway
(zero/null)

Computer Programming 12/20/2022 10


Example

Computer Programming 12/20/2022 11


Input and Output
#include<iostream>
using namespace std;
int main ( ) {
int length, width; Rectangle
int area;
cout<< "Enter the length: ";
cin>> length;
cout<< "Enter the width: ";
cin>> width;
area = length * width;
cout<< "the area of the rectangle is: "<< area<< endl;
cout<< "the perimeter of the rectangle is: "<< (length + width)*2;

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

cout << "Enter length of three sides of triangle: ";


cin >> a >> b >> c;

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

Computer Programming 12/20/2022 14


setw(width) manipulator

Computer Programming 12/20/2022 15


Exercise: write the output.
#include <iomanip> // for setw(m) that will put spaces m-times.
#include <iostream>
using namespace std;
int main ( )
{
int numb; //define loop variable
for(numb=1; numb<=10; numb++) //loop from 1 to 10
{
cout<< setw(4) << numb; //display 1st column
int cube = numb*numb*numb; //calculate cube
cout<< setw(6) << cube << endl; //display 2nd column
}

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?

Computer Programming 12/20/2022 18


continued
int x, a;
a = 2; Why x = 12?
x = ++a * --a * ++a;

 For the expression x = ++a*– –a*++a; you would expect


for a = 2, the x = 3 × 2 × 3, but it is equal to 2 × 2 × 3.
 Because value of a is first increased to 3 and then
decreased to 2 before the first multiplication. It is then
increased to 3 and multiplied to 12.

int x, a = 4;
Why x = 19?
x = ++a + ++a + ++a;

Computer Programming 12/20/2022 19


Computer Programming 12/20/2022 20
Expressions
PART 2 OF CHAPTER 2
Boolean expression
 What is a Boolean expression ?
 It is any expression that is either true or false.

 A Boolean expression consists of two or more


expressions, such as numbers or variables, which
are compared with one of the relational operators.
 Comparison or relational operators are shown below

Computer Programming 12/20/2022 22


Continued
 Notice that:- some of the relational operators are
spelled with two symbols.
 Such two-symbol operators should not have any
space between the two symbols.

Computer Programming 12/20/2022 23


Continued
 You can combine two or more comparisons using
the logic operators, such as: “and”, “or”, also “not”
spelled as &&, || and ! symbols respectively in C++.
 E.g.1, the following Boolean expression is true
provided x is greater than 2 and x is less than 7:
(2 < x) && (x < 7)
 E.g.2, the following Boolean expression is true
provided x is less than 2 and x is greater than 7:
(2 < x) || (x > 7)

Computer Programming 12/20/2022 24


Class Work
 Suppose, when you run the program, you enter the input
2 3 6 from the console. What is the output?

Computer Programming 12/20/2022 25


Continued
 If the first of two expressions joined with the || operator is
true, then you know the entire expression is true, no matter
whether the second expression is true or false.
 The C++ language uses this fact to sometimes save itself the
trouble of evaluating the second subexpression in a logical
expression connected with an && or || since C++ first
evaluates the leftmost of the two expressions.
 If that gives it enough information to determine the final
value of the expression (independent of the value of the
second expression), then C++ does not bother to evaluate the
second expression. This method of evaluation is called short-
circuit evaluation.

Computer Programming 12/20/2022 26


Bitwise Operations

Computer Programming 12/20/2022 27


Operator Precedence

Computer Programming 12/20/2022 28


Continued

Computer Programming 12/20/2022 29


Continued

Computer Programming 12/20/2022 30


Continued

Computer Programming 12/20/2022 31


The bool
Data Type
 The result of the comparison is a Boolean value:
true or false.
 A variable that holds a Boolean value is known as a
Boolean variable.
 The bool data type declares a variable with the
value either true or false.
 Internally, C++ uses 1 to represent true and 0 for
false. If you display a bool value to the console, 1 is
displayed if the value is true and 0 if it is false.

Computer Programming 12/20/2022 32


Continued
 Assigning a numeric value to a bool variable in C++,
Any nonzero value evaluates to true and zero value
evaluates to false.

bool b1 = -1.5; // Same as bool b1 = true


bool b2 = 0; // Same as bool b2 = false
bool b3 = 1.5; // Same as bool b3 = true
 Show the printout of the following code:

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 )

2, Both of the expressions are evaluating to 1, and


what will be the value of x latter, computing to
each expression separately?
3, !((y < 3) || (y > 7)) = !(y < 3) && !(y > 7)

Computer Programming 12/20/2022 34

You might also like