Presentation is loading. Please wait.

Presentation is loading. Please wait.

The comma as a separator and as an operator

Similar presentations


Presentation on theme: "The comma as a separator and as an operator"— Presentation transcript:

1 The comma as a separator and as an operator

2 Outline In this lesson, we will:
See that comma evaluation is context dependent Understand that Arguments separated by commas may not be evaluated in order In all other circumstances, the comma-separated components are evaluated left to right Know about the comma operator in expressions

3 The comma as a separator
In function calls, the comma separates arguments The order in which the arguments are evaluated is not defined, and thus different compilers will give different results This is the same for the evaluation of the operands of most binary operators #include <iostream> int f( int i ); void g( int m, int n ); int main(); int f( int i ) { std::cout << i << std::endl; } void g( int m, int n ) { // Does nothing int main() { g( f(1), f(2) ); return 0; Output: 2 1

4 The comma as a separator
In all other situations, items separated by a comma are always evaluated left to right, including: Variable declarations and initializations Initialization lists The comma operator in expressions

5 The comma as a separator
In variable declarations and initializations, the comma separates the variables being initialized, but they are evaluated left to right: #include <iostream> int main(); int main() { int a{1}, b{a + 1}, c{b + 2}; std::cout << a << " " << b << " " << c << std::endl; return 0; } Output: 1 2 4

6 The comma as a separator
Similarly, in an initialization list, the initial values are evaluated left to right: #include <iostream> int f( int i ); int main(); int f( int i ) { std::cout << i << std::endl; return i; } int main() { int array[4]{f(1), f(2), f(3), f(4)}; return 0; Output: 1 2 3 4

7 The comma as a separator
Placing them in the wrong order will result in a compiler error: #include <iostream> int main(); int main() { int a{b + 2}, b{c + 1}, c{1}; std::cout << a << " " << b << " " << c << std::endl; return 0; } example.cpp:4:11: error: 'b' was not declared in this scope int a{b + 2}, b{c + 1}, c{1}; ^ example.cpp:4:21: error: 'c' was not declared in this scope

8 Comma as a binary operator
If a comma is used in an expression, it now becomes a binary operator with the following properties: It evaluates the first operand and discards the result It evaluates the second operand and that becomes the value of operator For example: int i{0}; i = 1, 2, 3, 4; std::cout << i << std::endl; i = (1, 2, 3, 4); Question: How would you use parentheses to ensure that i was assigned the value 3? Output: 1 4

9 Comma as a binary operator
The only case where you should be using the comma as a binary operator is perhaps in the third statement of a for-loop: for ( int i{0}, j{1}; i <= 10; ++i, j *= 2 ) { std::cout << 2 << " raised to the power " << i << " equals " << j << std::endl; } Output: 2 raised to the power 0 equals 1 2 raised to the power 1 equals 2 2 raised to the power 2 equals 4 2 raised to the power 3 equals 8 2 raised to the power 4 equals 16 2 raised to the power 5 equals 32 2 raised to the power 6 equals 64 2 raised to the power 7 equals 128 2 raised to the power 8 equals 256 2 raised to the power 9 equals 512 2 raised to the power 10 equals 1024

10 Comma as a binary operator
Question: What is the output of these statements? int i{(1, 2) + (3, 4)}; std::cout << (i, i + 1, i + 2) << std::endl; Note: While you may see many short cuts were you could possibly use the comma operator, please refrain… What is critical is understanding it so that if you see it, you know what is happening

11 Summary Following this lesson, you now
Know that the comma is both a separator and an operator Understand that Arguments separated by commas may not be evaluated in order In all other circumstances, the comma-separated components are evaluated left to right

12 References [1] No references?

13 Colophon These slides were prepared using the Georgia typeface. Mathematical equations use Times New Roman, and source code is presented using Consolas. The photographs of lilacs in bloom appearing on the title slide and accenting the top of each other slide were taken at the Royal Botanical Gardens on May 27, 2018 by Douglas Wilhelm Harder. Please see for more information.

14 Disclaimer These slides are provided for the ece 150 Fundamentals of Programming course taught at the University of Waterloo. The material in it reflects the authors’ best judgment in light of the information available to them at the time of preparation. Any reliance on these course slides by any party for any other purpose are the responsibility of such parties. The authors accept no responsibility for damages, if any, suffered by any party as a result of decisions made or actions based on these course slides for any other purpose than that for which it was intended.


Download ppt "The comma as a separator and as an operator"

Similar presentations


Ads by Google