Sunday, April 12, 2009

Cascading of I/O Operators

Cascading of I/O Operators
We have used the extraction operator << repeatedly in the two statements for printing results. The statement
cout<<”Total Value is =”< First sends the string “Total Value is=” to cout and then sends the value of sum. Finally, it sends the new line character so that the next output will be in the new line. The multiple use of << in one statement is called cascading. When cascading an output operator, we should ensure necessary blank spaces between different items. Using the cascading technique, the last two statements can be combine as follows.
Cout<<”Total Value is=”<This is one statement but provides two lines of output.
Total Value is = 15
Average = 7.5

If you want one line of output, the statement will be;
Cout<<”Total Value is=”<Output will be

Total Value is = 15 Average = 7.5
We can also cascade input operator >> as shown below
cin>>n1>>n2>>;
The values are assigned from left to right. That is, if we key in two values, say, 15 and 24 then 15 will be assigned to n1 and 24 will be assigned to n2.

No comments:

Post a Comment