chap 3 c++
Post on 22-Aug-2015
245 Views
Preview:
TRANSCRIPT
CHAPTER THREE
MORE ON:
INPUT AND OUTPUT,
INCREMENT AND DECREMENT
OPERATORS,
PREPROCESSOR DIRECTIVES,
INPUT OUTPUT MANIPULATION,
DEBUGGING (FIXING SYNTAX ERRORS)
LECTURER: Samson Mekbib
INTRODUCTION
The keywords for input and output operations are stored in the standard library called iostream
Input = cin, output = cout
To use the cout and cin commands in C++, you need to include the following two lines:
#include <iostream>
Using namespace std;
The first line which start with # is a preprocessor directive
Lines that begin with # are preprocessed by the preprocessor before the program is compiled
The second line using namespace tells C++ to use the standard names cout and cin
3
STRING DATA TYPE
To use the string type in C++, we need to access its definition from
the header file string
To process strings effectively, C++ provides the type string
Include the following preprocessor directive:
#include <string>
4
CONSTANTS
Some data must stay the same throughout a program
In C++, you can use a named constant to instruct the program to mark those
memory locations in which data is fixed throughout program execution.
Named constant: A memory location whose content is not allowed to change during
program execution
The syntax to declare a named constant is:
Const dataType identifier;
Identifier = value;
An equivalent form of the above syntax is:
Const dataType identifier = value;
Examples:
const int No_of_students = 54;
const float Pay_Rate = 15.75 5
INPUT (READ) STATEMENT
Syntax
cin >> variable >> variable …;
cin is used with >> to gather input
This is called an input (read) statement
Using more than one variable in cin allows more than one value to be read at a time
Example:
cin >> feet >> inches;
This will input tow values from the keyboard (specified by the user) and places them in variables feet and inches respectively.
6
OUTPUT
Syntax:
cout << expression or manipulator << expression or manipulator …;
Is call an output statement
The stream operator is <<
Text to be displayed must be typed in between double quotations
“””
To print out values of identifiers like variables and constants we
write them without “”
7
MANIPULATOR
A manipulator is used to format the output
Example: endl causes insertion point to move to the beginning of
the next line.
Instead of endl the new line character ‘\n’ could be used
8
SPECIAL STATEMENTS IN C++
A C++ statement such as
num = num + 2;
means “evaluate whatever is in num, and 2 to it, and assign the
new value to the memory location num.”
The expression on the right side will be evaluated first and then
that value will be assigned to the variable.
Example:
num = 6;
num = num + 2;
After running the second statement C++ will assign 8 to the
variable num.
10
SPECIAL STATEMENTS CONTD.
Example: Suppose that num1, num2 and num3 are int variables and the following statements are executed in sequence
1. num1 = 18;
2. num1 = num1 + 27;
3. num2 = num1;
4. num3 = num2/5;
5. num3 = num3 / 4; The output at the end of each step will be as follows:
Before statement 1 no value was assigned to num1, num2, num3
After statement 1 num1 equals 18 and no value assigned to num2 and num3
After statement 2 num2 equals 45 and no value assigned to num2 and num3
After statement 3 num1 equals 45, num2 equals 45 and no value assigned to num3
After statement 4 num1=45, num2=45, num3 = 9
After statement 5 num1 = 45, num2 = 45, num3 = 2 11
INCREMENT AND DECREMENT
OPERATORS
Often used by programmers and they are useful programming tools
Suppose count is an int variable. The statement
count = count + 1;
Increments the value of count by 1.
Such statements are frequently used to keep track of how many times certain things have happened
To simplify such statements C++ provides the increment operator, ++, which increases the value of a variable by 1, and the decrement operator, --, which decreases the value of a variable by 1
Syntax of pre-increment: ++variable
Syntax of post-increment: variable++
Syntax of pre-decrement: --variable
Syntax of post-decrement: variable--
12
EXAMPLE - 1 ON PRE AND POST
- INCREMENT
Consider the following statement:
x = 5;
y = ++x;
The first statement assigns the value 5 to x. To evaluate the second statement, which uses the pre-increment operator, first the value of x is incremented to 6, and then this value 6 is assigned to y. After second statement execute both x and y will have the value 6.
Now consider the following statements:
x=5;
y = x++;
As before the 1st statement assigns 5 to x. In the 2nd statement since post-increment is used, the value 5 before increment is assigned to y, and x is incremented to 6. Hence after the second statement x = 6 and y= 5.
13
EXAMPLE - 2 ON PRE AND POST
- INCREMENT
Consider the following statement:
a = 5;
b = 2 + (++a);
The first statement assigns the value 5 to a. To evaluate the second
statement, which uses the pre-increment operator, first the value of a
is incremented to 6, then 2 is added to it and the value 8 is assigned
to b. Therefore, after the second statement executes, a is 6 and b is
8.
Now consider the following statements:
a = 5;
b = 2 + (a++);
After the second statement is executed the value of a is 6 while the
value of b is 7.
14
MORE PREPROCESSOR
DIRECTIVES
In a C++ system, a preprocessor program executes automatically
before the compiler's translation phase begins
Preprocessor directive statements will not end with a semicolon (;)
So far we have seen the preprocessor directives #include
<iostream> and #include <string> that allows us to handle input out
operations and using the string variables respectively in C++
Other preprocessor directive types are #include<filename>, which
are used to include saved C++ files with extension either .cpp or .h
Another preprocessor directive type is #include<iomanip> that
allows some C++ output manipulation commands like setw().
The manipulator set width is used for setting field output in C++
Output manipulators we saw so far (\n, \t, ‘\b’ etc.) do not need a
preprocessor directive. But to run the manipulator setw() we need to
write #include<iomanip> at the start of the program 15
SET WIDTH MANIPULATOR
Syntax setw(num)
setw() is library function in C++
setw() is declared inside #include<iomanip>
setw() will set field width
Setw() sets the number of characters to be used as the field width for the next insertion operation
16
DEBUGGING: UNDERSTANDING
AND FIXING SYNTAX ERRORS
Compile a program
• Compiler will identify the syntax errors
• Specifies the line numbers where the error occur
Examples:
18
SYNTAX
Syntax rules- indicate what is legal and what is not legal
Error in syntax are found in compilation
Errors and debugging is common in programming
Examples
int x; //Line 1
Int y; // Line 2: error
float b // line 3: error
double z = w + x; // line 4: error, identifier w not defined
19
USE OF BLANKS
In C++, you use one or more blanks to separate
numbers when data is input
Blanks are used to separate reserved words and
identifiers from each other and from other symbols
Blanks must never appear within a reserved word or
identifier
20
USE OF SEMICOLONS,
BRACKETS, AND
COMMAS
All C++ statements end with a semicolon
Semicolons are also called statement terminator
Brackets like { indicate start of main function and a
closing bracket } indicates end of function main
Commas are used to separate items in a list
21
SEMANTICS
Semantics: set of rules that gives meaning in a language
• It is possible to remove all syntax errors in a program and still
not able to run it
• Even if it runs, it may still not do what we expect it to do
Example: 2 + 3 * 5 and (2 + 3) * 5
are both syntactically correct expressions, but have different
meanings.
22
top related