Review Part II
Starting New Lines in Output
To start a new output line, you can include \n in a quoted string, as in the
following example:
cout << "You have definitely won\n"
<< "one of the following prizes:\n";
Recall that \n is typed as two symbols with no space in-between the two
symbols.
Alternatively, you can start a new line by outputting endl. An
equivalent way to write the above cout statement is as follows:
cout << "You have definitely won" << endl
<< "one of the following prizes:" << endl;
Outputting Values of Type double
If you insert the following “magic formula” in your program, then all
numbers of type double (or any other type that allows for digits after the
decimal point) will be output in ordinary everyday notation with two digits
after the decimal point:
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
You can use any other nonnegative whole number in place of the 2 to
specify a different number of digits after the decimal point. You can even
use a variable of type int in place of the 2
cin Statements
A cin statement sets variables equal to values typed in at the keyboard.
SYNTAX cin >> Variable_1 >> Variable_2 >> . . . ;
EXAMPLE cin >> number >> size;
cin >> time_to_go
>> points_needed;
The “and” Operator &&
You can form a more elaborate Boolean expression by combining two
simple tests using the “and” operator &&.
SYNTAX (FOR A BOOLEAN EXPRESSION USING &&)
(Comparison_1) && (Comparison_2)
EXAMPLE
(WITHIN AN if-else STATEMENT)
if ( (score > 0) && (score < 10) )
cout << "score is between 0 and 10\n";
else
cout << "score is not between 0 and 10.\n";
If the value of score is greater than 0 and the value of score is also less
than 10, then the first cout statement will be executed; otherwise, the
second cout statement will be executed.
The “or” Operator ||
You can form a more elaborate Boolean expression by combining two
simple tests using the “or” operator ||.
SYNTAX (FOR A BOOLEAN EXPRESSION USING ||)
(Comparison_1) || (Comparison_2)
EXAMPLE
(WITHIN AN if-else STATEMENT)
if ( (x == 1) || (x == y) )
cout << "x is 1 or x equals y.\n";
else
cout << "x is neither 1 nor equal to y.\n";
If the value of x is equal to 1 or the value of x is equal to the value of y (or
both), then the first cout statement will be executed; otherwise, the
second cout statement will be executed.
Navid Plus is a Tech blog focusing on Science material that I learn day through my College and job. Most likely, blog posts contain C++ Programming, Security, Networking, and Math. Let's have some fun.
Subscribe to:
Post Comments (Atom)
Digital Design Part 3
4th→ assembler translates it to the machine language. 1.6 [20] <§1.6> Consider two diļ¬erent implementations of the same instru...
-
An array is used to process a collection of data all of which is of the same type, such as a list of numbers or a list of names. An array...
-
One way to think about execution time is that it equals the number of instructions executed multiplied by the average time per instruc...
No comments:
Post a Comment