Chapter 4 C++ Expression, Statements, Standard
Chapter 4 C++ Expression, Statements, Standard
Objectives:
Sample:
{
This block of code acts as one
temp = a; statement and swaps the values in the
a = b; variables a and b.
b = temp;
}
EXPRESSIONS
Anything that evaluates to a value is an expression in C++. An expression is said to return a value.
Thus, 3+2’ returns the value of 5 and so is an expression. All expressions are statements.
Here are three examples:
3.2 // returns the value 3.2
PI // float const that the value 3.14
Seconds per minute //int const that returns 60
Page 1
CC02 MODULE FUNDAMENTALS OF PROGRAMMING
The standard C++ library includes the header file iostream, where the standard input and
output stream objects are declared.
cout is used in conjunction with the insertion operator, which is written as << (two "less than"
signs).
The << operator inserts the data that follows it into the stream preceding it. In the examples
above it inserted the constant string Output sentence, the numerical constant 120 and
variable x into the standard output stream cout. Notice that the sentence in the first instruction is
enclosed between double quotes (") because it is a constant string of characters. Whenever we
want to use constant strings of characters we must enclose them between double quotes ( ") so
that they can be clearly distinguished from variable names. For example, these two sentences
have very different results:
The insertion operator (<<) may be used more than once in a single statement:
Additionally, to add a new-line, you may also use the endl manipulator. For example:
First sentence.
Second sentence.
Page 2
CC02 MODULE FUNDAMENTALS OF PROGRAMMING
The endl manipulator produces a newline character, exactly as the insertion of '\n' does,
1 // i/o example but it
2 also has
3 #include<iostream.h> an
4 #include<conio.h>
5
6 main ()
7 {
8 clrscr();
9 char a[20];
10 cout<<"Walang Forever si: ";
11 cin>>a;
12 cout<<"Si "<a <<" ay walang forever";
13
14
additional behavior when it is used with buffered streams: the buffer is flushed. Anyway, cout will
be an unbuffered stream in most cases, so you can generally use both the \n escape character
and the endl manipulator in order to specify a new line without any difference in its behavior.
The first statement declares a variable of type int called age, and
1 int age;
the second one waits for an input from cin (the keyboard) in
2 cin >> age;
order to store it in this integer variable.
cin can only process the input from the keyboard once the RETURN key has been pressed.
Therefore, even if you request a single character, the extraction from cin will not process the input
until the user presses RETURN after the character has been introduced.
You must always consider the type of the variable that you are using as a container
with cin extractions. If you request an integer you will get an integer, if you request a character
you will get a character and if you request a string of characters you will get a string of characters.
Sample
Page 3
CC02 MODULE FUNDAMENTALS OF PROGRAMMING
Output:
The user of a program may be one of the factors that generate errors even in the simplest
programs that use cin(like the one we have just seen). Since if you request an integer value and
the user introduces a name (which generally is a string of characters), the result may cause your
program to misoperate since it is not what we were expecting from the user. So when you use
the data input provided by cin extractions you will have to trust that the user of your program will
be cooperative and that he/she will not introduce his/her name or something similar when an
integer value is requested. A little ahead, when we see the stringstream class we will see a possible
solution for the errors that can be caused by this type of user input.
You can also use cin to request more than one datum input from the user:
In both cases the user must give two data, one for variable a and
another one for variable b that may be separated by any valid blank
separator: a space, a tab character or a newline.
➢ https://www.youtube.com/watch?v=qhnARpYSqAA
➢ https://www.youtube.com/watch?v=vtKSE_8gj1E
➢ https://www.youtube.com/watch?v=Rse1FiIDp4I
REFERENCES
❖ https://www.w3schools.com/cpp/cpp_user_input.asp
Page 4