0% found this document useful (0 votes)
41 views4 pages

Chapter 4 C++ Expression, Statements, Standard

Uploaded by

j04900298
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views4 pages

Chapter 4 C++ Expression, Statements, Standard

Uploaded by

j04900298
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

CC02 MODULE FUNDAMENTALS OF PROGRAMMING

CHAPTER 4: C++ EXPRESSION, STATEMENTS, STANDARD


INPUT AND OUTPUT

Objectives:

a.) Know what expression is.


b.) Know how to construct block of statements
c.) How to use the standard input and output in a complex program.

BLOCK AND COMPOUND STATEMENT


Any place you can put a single statement, you can put command statement, also called a
block statement. A block begins with an opening brace ({) and ends with a closing brace
(}).

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

BASIC OUTPUT / INPUT


Using the standard input and output library, we will be able to interact with the user by
printing messages on the screen and getting the user's input from the keyboard.
C++ uses a convenient abstraction called streams to perform input and output operations
in sequential media such as the screen or the keyboard. A stream is an object where a program
can either insert or extract characters to/from it. We do not really need to care about many
specifications about the physical media associated with the stream - we only need to know it will
accept or provide characters sequentially.

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.

STANDARD OUTPUT (cout)


By default, the standard output of a program is the screen, and the C++ stream object
defined to access it is cout.

cout is used in conjunction with the insertion operator, which is written as << (two "less than"
signs).

1 cout <<"Output sentence"; // prints Output sentence on screen


2 cout << 120; // prints number 120 on screen
3 cout << x; // prints the content of x on screen

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:

1 cout <<"Hello"; // prints Hello


2 cout << Hello; // prints the content of Hello variable

The insertion operator (<<) may be used more than once in a single statement:

cout <<"Hello, "<<"I am "<<"a C++ statement";

This last statement would print the message:


Hello, I am a C++ statement

Additionally, to add a new-line, you may also use the endl manipulator. For example:

1 cout <<"First sentence."<< endl;


2 cout <<"Second sentence."<< endl; Would print out:

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.

STANDARD INPUT (cin)


The standard input device is usually the keyboard. Handling the standard input in C++ is
done by applying the overloaded operator of extraction (>>) on the cin stream. The operator must
be followed by the variable that will store the data that is going to be extracted from the stream.
For example:

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:

Walang Forever si:


Si ___________ ay walang forever.

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:

is equivalent to: cin >> a >> b; 1 cin >> a;


2 cin >> b;

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.

For more knowledge Fundamentals of Programming, please check the


link provided;

➢ 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

You might also like