Programming1 Lecture 3
Programming1 Lecture 3
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.
Think of the stream insertion operator as an arrow that points toward cout.
4
There are some certain characters in C++ which are can be used with the
escape sequence (\) (escape characters or backslash (\) keys).
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;
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.
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;
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!
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;
return 0;
}
21
In Microsoft Visual C++, the result of a modulus expression is
always the same as the sign of the first operand.
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
system("pause");
return 0; C++ Program
} 26
Arithmetic expressions in C++ must be entered into the computer in straight
line form.
27