0% found this document useful (0 votes)
24 views

Basics of C++

The document provides information on C++ programming concepts including data types, variables, constants, operators, Boolean expressions, and logic. Key points include: - Common C++ data types like int, char, float, and their ranges. Widening conversion allows implicit conversion between compatible types. - Comments begin with // for single line or /* */ for multi-line. C++ is case sensitive. - Variables are declared with a data type and initialized optionally. Constants are declared with const. - Relational and logic operators are used to build Boolean expressions that evaluate to true or false. - The bool data type represents true and false values internally as 1 and 0 but displays as 1 and 0

Uploaded by

on.bonimos
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Basics of C++

The document provides information on C++ programming concepts including data types, variables, constants, operators, Boolean expressions, and logic. Key points include: - Common C++ data types like int, char, float, and their ranges. Widening conversion allows implicit conversion between compatible types. - Comments begin with // for single line or /* */ for multi-line. C++ is case sensitive. - Variables are declared with a data type and initialized optionally. Constants are declared with const. - Relational and logic operators are used to build Boolean expressions that evaluate to true or false. - The bool data type represents true and false values internally as 1 and 0 but displays as 1 and 0

Uploaded by

on.bonimos
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

PART 1 OF CHAPTER 2

▪ 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 3/20/2021 2
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 3/20/2021 3
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 3/20/2021 4
Computer Programming 3/20/2021 5
Note: the ASCII at \u007F is similar to delete

Computer Programming 3/20/2021 6


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 3/20/2021 7
Computer Programming 3/20/2021 8
Computer Programming 3/20/2021 9
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 3/20/2021 10


Computer Programming 3/20/2021 11
#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 3/20/2021 12
#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-c) )1/2
float a, b, c, s, area;

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 3/20/2021 13
Syntax:
const datatype CONSTANTNAME = value;
𝐸. 𝑔. 𝒂𝒓𝒆𝒂 𝒐𝒇 𝑪𝒊𝒓𝒄𝒍𝒆 = 𝝅 × 𝒓𝒂𝒅𝒊𝒖𝒔 × 𝒓𝒂𝒅𝒊𝒖𝒔

Circle

Computer Programming 3/20/2021 14


setw(width)

Computer Programming 3/20/2021 15


#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 3/20/2021 16
#include <stdio>
int main ( ) {
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 3/20/2021 17
int x, a, b, c;
a = 2;
b = 4;
c = 5; x
x = a-- + b++ - ++c;

Computer Programming 3/20/2021 18


int x, a;
a = 2; x
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;
x = ++a + ++a + ++a;
x

Computer Programming 3/20/2021 19


Computer Programming 3/20/2021 20
PART 2 OF CHAPTER 2
▪ 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 3/20/2021 22


▪ 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 3/20/2021 23


▪ 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 3/20/2021 24


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

Computer Programming 3/20/2021 25


▪ 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 3/20/2021 26


Computer Programming 3/20/2021 27
Computer Programming 3/20/2021 28
Computer Programming 3/20/2021 29
Computer Programming 3/20/2021 30
Computer Programming 3/20/2021 31
bool

▪ 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 3/20/2021 32


▪ 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 3/20/2021 33
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 3/20/2021 34

You might also like