Chapter 1 Input Output
Chapter 1 Input Output
com 1
CHAPTER 1
Introduction to computer programming
(Input, Output, Operators, and Expressions)
1.7 output
• To print the value of an integer variable we need to send it
to cout stream through << operator
• Both the << operator and cout stream are responsible for
printing
• You can connect more than one << operator in
one cout statement and each of the printed elements
may be of a different type and a different nature
Reference: www.netacad.com 5
1.7 output
• To break the line being sent to the screen using two ways:
• First, we can use one of the control characters called “newline” and
coded as \n (note: we use two characters to write it down but the
compiler sees it as one character).
• The newline character forces the console to complete the current line
and to start a new one.
• Second, using a manipulator called endl (as “end line”). Using endl
We can achieve exactly the same effect as \n.
• Example:
• This snippet causes the console to display the following three lines of
text:
1
2
3
Reference: www.netacad.com 6
1.7 Input
• C++ program allow getting information from the user,
transfer it to the program, and then use it for calculations.
1.7 Input
• Using cin stream with the operator >>; C++ language
program get data from a human and store it in variables.
• C++ program can ask the user to enter a value from the
keyboard and the program stores it in a specified variable
• For example: cin>>MaxPrice;
1.7 Input
• Example:
Reference: www.netacad.com 9
1.4 Operators
• An operator is a symbol of the programming language,
which is able to operate on the values
• For example, an assignment operator is (=)
• Used to assign values to variables X= 4
• Other operators available in C++ language associated
with widely recognizable arithmetic operations.
Reference: www.netacad.com 10
1.4 Multiplication ( * )
• An asterisk * is a multiplication operator.
• For example:
• k will be set to the value of 120, while the z variable will be set to
0.625.
Reference: www.netacad.com 11
1.4 Division ( / )
• A slash / is a divisional operator. The value in front of the
slash is a dividend, while the value behind the slash is
a divisor.
• For example:
• when you try to execute that code, the result of the operation is not
a number. It’s a special featured value named inf (as in infinitive).
Generally, this kind of illegal operation is a so-called exception.
Reference: www.netacad.com 13
1.4 Addition ( + )
• The addition operator is the + (plus) sign.
• For example:
1.4 Subtraction( - )
• The subtraction operator is the – (minus) sign.
• this operator has another meaning – it can change the sign of a
number.
• For example:
1.4 Remainder ( % )
• Its representation in the C++ language is the % (percent)
character.
• It’s a binary operator (it performs the modulo operation) and
both arguments cannot be floats.
• For example:
1.4 Priorities
• The hierarchy of priorities is the phenomenon that causes some
operators to act before others is known as the priority.
• The C++ language precisely defines the priorities of all operators and
assumes that operators of larger (higher) priority perform their
operations before the operators with lower priority.
• For example:
1.4 Bindings
• The binding of the operator determines the order of computations
performed by some operators with equal priority, put side by side in
one expression.
• Most operators in the C++ language have the left-sided binding,
which means that the calculation of this sample expression is
conducted from left to right.
• For example:
• Result is:
Reference: www.netacad.com 22
1.4 Parentheses
• Parentheses can change the natural order of calculation. Sub
expressions in parentheses are always calculated first.
• You can use as many parentheses as you need and we often
use them to improve the readability
• For example:
Expressions
• If all operands are integers
• Expression is called an integral expression
• Yields an integral result
• Example: 2 + 3 * 5
Mixed Expressions
• Mixed expression:
• Has operands of different data types
• Contains integers and floating-point
• Examples of mixed expressions:
2 + 3.5
6 / 4 + 3.9
5.4 * 2 – 13.6 + 18 / 2
Type Conversion
Cast operator: provides explicit type conversion
static_cast<dataTypeName>(expression)
Implicit conversion
• cout is able to recognize the actual type of its element even
when it is an effect of a conversion.