Program Structure, Actions and Data Types

Week 1-2
In this class, you will learn about:
• Program Structure, Actions and Data Types
• Data Type: Variables
• Output and Input action: function cout<< &
cin>>
• Assignment
• Simple calculations
Reference: Programming with C++, Theory and
Problems of, Second Edition; John R Hubbard
(McGraw Hill, 2000, Schaum's Outline Series)
Program Structure, Actions and
Data Types
• C++ Program Structure
• #include <iostream>
• main() {
• cout<<"HEY, you, I'm alive!"; // output a string.
• }
– The program is saved as a file that should be named. In this
example, the name of the file is cout1a.cpp. .cpp
– The #include is called a preprocessor directive which tells the
computer compiler to include the header file iostream.h in our
program cout1a.cpp.
– The next part is main().
– The body of the
– The lines for the body of a program is called statements, for
example, cout
– Comment lines
Data Type: Variables
• Variables
• Name of a variable
– A variable has a name, which is given by the programmer by
declaration or definition. When the name is given, the memory
for it is specified.
• Rules for creating names of variables
– Rule 1: a name starts with a letter.
– Rule 2: a name is unique, which should be different from other
words in the program.
– Rule 3: names are sensitive to letters upper case or lower case.
Doug is not equivalent to doug.
– Rule 4: names should be meaningful.
Data Type: Variables
•
•
•
•
For example, int
alice;
name of variable:
alice
type: int(eger)
tail:
;
Data Types
Part of Integral Type
Float type example
• It shows that the 32 bits it uses to store a float are
partitioned into 3 parts: 23 bits for the mantissa, 8 bits for
the exponent, and 1 bit for the sign. The 23-bit mantissa
produces a floating-point value with 6 significant digits,
and the 8-bit exponent yields a range in magnitude from
about 10–37 to about 3 × 1038. i.e.,
0.0000000000000000000000000000000000001 < |x| <
300,000,000,000,000,000,000,000,000,000,000,000,000
for any variable x declared to have type float.
A new variable : char (character
data)
• Definition
– A char variable is defined to hold a single character
that appears on the keyboard.
– Difference between a char and a string is that a char
is a single character while a string is a collection of
serial characters.
• Declaration
– char c;
• Input a char variable from keyboard
• A space cannot be input from keyboard
A new variable : char (character
data)
A new variable : char (character
data)
Output action: function cout<<
•
•
•
More actions by using a back slash \
The sign \ is not a normal character but a
control character to instruct the computer to
take an action corresponding to next character.
See table below,
Escape characterEffect\nNewline\tHorizontal
tab\rCarriage return\bBackspace\aBell or
beep\\Backslash\”Double quote\’Single
quote\?Question mark
Input action: input function cin>>
Assignment
• We have seen that one way of assigning a
variable with a value is to input a number of
digits on keyboard. Another important way is to
use an assignment statement. For example
(assignment1.cpp),
Prefix and postfix increment
•
•
•
•
Example:k = ++x;//prefix increment
is equivalent to:
x = x + 1; //increment n first
k = x;//assign x’s value to k
• Example:k = x++;//postfix increment
• is equivalent to
• k = x;//assign x’s value to k
• x = x + 1;//and then increment x
Applying the Pre-increment and
Post-increment Operators
int main()
{ // shows the difference between m++ and ++m:
int m,n;
m = 44;
n = ++m; // the pre-increment operator is applied to m
cout << "m = " << m << ", n = " << n << endl;
m = 44;
n = m++; // the post-increment operator is applied to m
cout << "m = " << m << ", n = " << n << endl;
}
COMPOSITE ASSIGNMENT
OPERATORS
• The standard assignment operator in C++
is the equals sign =. In addition to this
operator, C++ also includes the following
composite assignment operators: +=, -=,
*=, /=, and %=.
• When applied to a variable on the left,
each applies the indicated arithmetic
operation to it using the value of the
expression on the right.
Applying Composite Arithmetic
Assignment Operators
int main()
{ // tests arithmetic assignment operators:
int n=22;
cout << "n = " << n << endl;
n += 9; // adds 9 to n
cout << "After n += 9, n = " << n << endl;
n -= 5; // subtracts 5 from n
cout << "After n -= 5, n = " << n << endl;
n *= 2; // multiplies n by 3
cout << "After n *= 2, n = " << n << endl;
n / = 3; // divides n by 9
cout << "After n /= 3, n = " << n << endl;
n %= 7; // reduces n to the remainder from
dividing by 4
cout << "After n %= 7, n = " << n << endl;
}
Simple calculations
Simple calculations
Integer Calculation Example
int main()
{ // tests operators +,-,*,/,and %:
int m=54;
int n=20;
cout << "m = " << m << " and n = " << n << endl;
cout << "m+n = " << m+n << endl; // 54+20 = 74
cout << "m-n = " << m-n << endl; // 54-20 = 34
cout << "m*n = " << m*n << endl; // 54*20 = 1080
cout << "m/n = " << m/n << endl; // 54/20 = 2
cout << "m%n = " << m%n << endl; // 54%20 = 14
Return 0;
}