Wednesday, March 22, 2017

C++ Part 39

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.
Post a Comment