2 - C++ Basics
2 - C++ Basics
• The first three are not allowed because they do not start
with a letter or underscore. The remaining three are not
identifiers because they contain symbols other than
letters, digits and the underscore symbol.
• C++ is a case-sensitive language. E.g the following are
three distinct identifiers.
rate RATE Rate
• The predefined identifiers, such as main, cin, cout and
so forth must be spelled in all lowercase letters.
• There is a special class of identifiers call keywords or
reserved words that have a predefined meaning in C++
and that you cannot use as names for variables or
anything else.
Example:
Syntax:
cin >> Variable_1 >> Variable _2 >> ….;
Examples:
cin >> number >> size
cin >> time_to_go
>> points_needed
• Designing Input and Output
• Input and output (I/O) is the part of the program that the
user sees.
• When the computer executes a cin-statement, it expects
some data to be typed in at the keyboard. If non is typed,
it simply waits.
• The program must tell the user when to type in a number
(or other data item).
• Your program should always have output statements
that prompt for input
• E.g
cout << “Enter the number of books you have \n”;
cout << “what is your name \n”
cout << “Then press return \n”
• 2.3 Data Types and Expressions
• 2.3.1 The Types int and double
• Whole numbers (e.g. 2) are of type int
• Numbers of type int are stored as exact values
• Decimal numbers (e. g. 2.0) are of type double
• Numbers of type double are stored as approximate
values.
• The more complicated notation for constants of type
double is frequently called scientific notation or floating
point notation.
• 2.3.2 Other Number Types
• C++ has other numeric types besides int and double
• The various number types allow for different size
numbers and for more or less precision.
Some Number Types
Type Name Memory Used Size Range Precision
return 0;
}
• 2.4.2 Comparisons Operators
= equal to ==
≠ not equal to !=
< less than <
≤ less than or equal to <=
> Greater than >
≥ greater than or equal to >=
return 0;
}
• A do-while-Loop
#include <iostream.h>
int main()
{
char ans;
do
{
cout << “hello \n”;
cout << “Do you want another greeting?\n”
<< “Press y for yes, n for no, \n)
<< “and then press return:”;
cin >> ans;
return 0;
}
• A do-while loop is similar to while loop except that the loop body is
always executed at least once.
• 2.4.5 Increment and Decrement Operators