Programming1 Lecture 3

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

Fundamentals of

C++

LECTURE 3
Computer Programming I
CMPE 121
✓ Special Characters
✓ Numbering System
✓ Identifiers
✓ Integer Literals
✓ Floating-Point Literals
✓ Boolean Literals
✓ Character Literals
✓ String Literals
✓ Arithmetic operators and expressions
Tony Gaddis, Starting Out with C++: From Control Structures
through Objects, 8th Edition
✓ Chapter 2 (from page 32 to 82)
cout << "Hello World" << endl;
Output in C++
are accomplished with streams of characters. Thus, when the preceding statement is
executed, it sends the stream of characters "Hello World!" to the standard
output stream object cout (std::cout) which is “connected” to the screen by the
default value.
cout is a predefined identifier for the standard output stream object that stands for
console output.

cout << "Programming is great fun!";

Think of the stream insertion operator as an arrow that points toward cout.

cout "Programming is great fun!";

4
There are some certain characters in C++ which are can be used with the
escape sequence (\) (escape characters or backslash (\) keys).

Control characters: Punctuation characters:

➢ \n = Newline ➢ \" = Double quote


➢ \b = Backspace ➢ \' = Single quote
➢ \t = Horizontal tab ➢ \\ = backslash
➢ \r = Return
➢ \a = Alarm

5
#include <iostream>
using namespace std;
int main()
{
cout << "Hello" << " everybody!" << endl;
cout << "\"Hello\"" << endl;
cout << "A\t+\tB\t=" << endl;
cout << 'F' << '\n';
cout << "A+B" << '\r' << "C-\n";
cout << "Hello\n";
cout << "It is OK\?\n";
cout << "'" << endl;
cout << "Its" << '\b' << " is OK\?\n";

return 0;
}

6
Variables represent storage locations in the computer’s memory.
Literals are constant values that are assigned to variables.
✓ Variables allow you to store and work with data in the
computer’s memory. They provide an “interface” to RAM.

int number;

This is called a variable definition.


It tells the compiler the variable’s name and the type of data it
will hold.
The word int stands for integer, so number will only be used to
hold integer numbers.

7
8
9
Regardless of which style you adopt, be consistent and make your
variable names as sensible as possible. C++ is case sensitive.
• The first character must be one of the letters a through z, A through
Z, or an underscore character (_).
• After the first character you may use the letters a through z or A
through Z, the digits 0 through 9, or underscores.
• Uppercase and lowercase characters are distinct.
• C++ Key words can not be used as Identifier

10
11
Variables represent storage locations in the computer’s memory.
Literals are constant values that are assigned to variables.

✓ An integer literal can be a decimal, octal, or hexadecimal constant.


✓ A prefix specifies the base or radix:
▪ 0x or 0X for hexadecimal
▪ 0 for octal
▪ nothing for decimal.
✓ A constant written without a decimal point is recognized by the
compiler as an integer number.

12
The size of a variable is the number of bytes of memory it uses.
Typically, the larger a variable is, the greater the range it can hold.

13
#include <iostream>
using namespace std;
int main()
{
cout << 11 << endl; // decimal-literal
cout << 011 << endl; // octal-literal
cout << 0x11 << endl; // hexa-literal
cout << 0b11 << endl; // binary-literal since C++ 14

return 0;
}

14
Floating-point literals are the decimal numbers which are can be used either
in decimal form or exponential form. Any number with decimal dot is
recognized by the compiler as a floating-point literal constant.
#include <iostream>
using namespace std;
int main()
{
cout << 3.1415 << endl; // floating-point literal
cout << .1415 << endl; // floating-point literal
cout << 3.0 << endl; // floating-point literal
cout << 3. << endl; // floating-point literal
cout << 31415E-4 << endl; // floating-point literal
cout << 31415E-4L << endl; // floating-point literal
cout << 31E2 << endl; // floating-point literal

return 0;
}

15
Boolean literals can be taken the values of either true or false. Uppercase
form of them are not be considered as Boolean literals.

#include <iostream>
using namespace std;
int main()
{
cout << true << endl; // Boolean literal
cout << false << endl; // Boolean literal

system("pause");
return 0;
}

16
Character literal is any single symbol. There are some certain characters in
C++ which are can be used with escape sequence.
The single quote character (') is a delimiter for a character literal.

#include <iostream>
using namespace std;
int main()
{
cout << 'A' << endl; // character literal
cout << 'f' << endl; // character literal
cout << '5' << endl; // character literal
cout << '+' << endl; // character literal

return 0;
}

17
The most commonly used method for encoding characters is
ASCII, which stands for the American Standard Code for Information
Interchange.

#include <iostream>
using namespace std;
int main()
{
char letter;

letter = 'A';
cout << letter << endl;

letter = 66;
cout << letter;

return 0;
}

18
C++ does not have a built-in data type able to do this, standard C++
provides something called the string class that allows the programmer to
create a string type variable.

#include <iostream>
#include <string> // Required for the string class.
using namespace std;

int main()
{
string movieTitle;

movieTitle = "Despicable Me";

cout << "My favorite movie is " << movieTitle << endl;

return 0;
}

19
Every variable has a scope. The scope of a variable is the part of the
program where the variable may be used.
The first rule of scope you should learn is that a variable cannot be used in
any part of the program before the definition
#include <iostream>
using namespace std;

int main()
{
cout << value; // ERROR! value not defined yet!

int value = 100;

cout << value; // NO ERROR! inside scope

return 0;
}
20
In this method, constant is defined with const keyword.
An initialization value must be given when defining a constant with
the const qualifier, or an error will result when the program is
compiled.
A compiler error will also result if there are any statements in the
program that attempt to change the value of a named constant.

#include <iostream>
using namespace std;
int main()
{
const float PI = 3.14;

PI = 4.0; // ERROR! value can not changed

return 0;
}
21
In Microsoft Visual C++, the result of a modulus expression is
always the same as the sign of the first operand.

* If the operands of the division operator are both integers


result will be integer.
22
#include <iostream>
using namespace std;
int main()
{
cout << 10 + 3 << endl;
cout << 10 - 3 << endl;
cout << 10 * 3 << endl;
cout << 10 / 3 << '\n';
cout << (int)11 / 5 << '\n';
cout << (float)11 / 5 << '\n';
cout << 10.0 / 3 << '\n';
cout << 10 / 3.3 << '\n';
cout << 10 % 3 << '\n';
cout << "10+3=" << 10 + 3 << '\n';
cout << 5 << '+' << 1 << '=' << 5 + 1 << endl;

system("pause");
return 0; 23
}
Precedence of Arithmetic Operators:
➢ Parentheses () are evaluated first. The expression in the innermost
parentheses is evaluated first if the parentheses are nested.
➢ After parentheses multiplication (*), division (/), modulus (%)
operators are evaluated.
➢ Addition (+) and Subtraction (-) are evaluated last.
➢ The operators with the same precedence are evaluated left to right.

3 * 5 + 2 = 17 (12 / (8 – 2)) = 2
3 * (5 + 2) = 21 8 + (7 - 9) = 6
5 + 3 * 4 – 2 = 15 9 + 3 + 4 – 2 = 14
6 * 8 / 4 = 12 16 / 4 * 2 = 8
6 * (8 / 4) = 12 4 / 2 - 2 = 0
C++ Operators can be divided into 3 levels according to their precedence
➢ First: ()
➢ Second: * , / , %
24
➢ Third: + , -
20 / 5 * 2 = 8 Subexpressions of the same precedence are evaluated from
left to right

4 * 6 / 2 = 12 Subexpressions of the same precedence are evaluated from


left to right

6/3*2=4 Subexpressions of the same precedence are evaluated from


left to right

9.0 / 3 / 2 = 1.5 Subexpressions of the same precedence are evaluated from


left to right

3+5–2=6 Subexpressions of the same precedence are evaluated from


left to right

9–6–2=1 Subexpressions of the same precedence are evaluated from


left to right

9 - (6 - 2) = 5 Parenthesised expressions have the highest priority

9 / 3 + 3 * 3 = 12 Firstly division, then multiplication and then addition

12 / (3 + 3) * 3= 6 Firstly parenthesis, then division and then multiplication

3*2/2+2–5=0 Firstly multiplication, then division, then addition and then


subtraction 25
#include <iostream>
using namespace std;
int main()
{
cout << 4 * 6 / 2 << endl;
cout << 6 / 3 * 2 << endl;
cout << 9 / 3 / 2 << endl;
cout << 3 + 5 - 2 << endl;
cout << 9 - 6 - 2 << '\n';
cout << 9 - (6 - 2) << '\n';
cout << 9 / 3 + 3 * 3 << endl; Output
cout << 12 / (3 + 3) * 3 << endl;
cout << 3 * 2 / 2 + 2 - 5 << endl;
cout << 3 * 2 / 2 + 2 - 8 / 4 << endl;

system("pause");
return 0; C++ Program
} 26
Arithmetic expressions in C++ must be entered into the computer in straight
line form.

27

You might also like