The comma as a separator and as an operator

Slides:



Advertisements
Similar presentations
Recursive binary search
Advertisements

For loops.
Templates.
Introduction to classes
Static variables.
Default values of parameters
Pointers.
Anatomy of a program.
Switch statement.
Binary search.
Do-while loops.
Namespaces.
Command-line arguments
Pointer arithmetic.
Console input.
Dangling pointers.
Hello world!.
This.
Arithmetic operators.
Floating-point primitive data types
Dynamically allocating arrays within structures
Break statements.
Linked Lists.
Wild pointers.
Console output.
Logical operators.
Bucket sort.
The call stack and recursion and parameters revisited
The ternary conditional operator
Dynamically allocating structures
Bit-wise and bit-shift operators
Sorting algorithms.
Command-line arguments
Passing pointers as parameters to and from functions
Templated Linked Lists
Dynamically allocating arrays
Insertion sort.
Problems with pointers
Protecting pointers.
Code-development strategies
Throwing exceptions.
Anatomy of a program.
Console output.
Insertion sort.
Pointers as arguments and return values
Reference variables, pass-by-reference and return-by-reference
Addresses and pointers
Default values of parameters
Pointer arithmetic.
Class variables and class functions
Operator overloading.
The std::string class.
Templates.
Insertion sort.
Sorted arrays.
Sorting algorithms.
Issues with classes.
Dangling pointers.
Dynamic allocation of classes
Encapsulation.
Destructors.
Counting sort.
Searching and sorting arrays
Protecting pointers.
Arithmetic operators.
Data structures: class
Constructors.
This.
Recursive binary search
Presentation transcript:

The comma as a separator and as an operator

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

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

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

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

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

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

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

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

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

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

References [1] No references?

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 https://www.rbg.ca/ for more information.

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.