Variable declaration in 'if' expression follow-up - c++

This is a follow-up to C++, variable declaration in 'if' expression
if( int x = 3 && true && 12 > 11 )
x = 1;
The rules (so far as I can tell) are:
can only have 1 variable declared per expression
variable declaration must occur first in the expression
must use copy initialization syntax not direct initialization syntax
cannot have parenthesis around declaration
1 and 2 make sense according to this answer but I can't see any reason for 3 and 4. Can anyone else?

C++03 standard defines selection statement as:
selection-statement:
if ( condition ) statement
if ( condition ) statement else statement
switch ( condituion ) statement
condition:
expression
type-specifier-seq attribute-specifieropt declarator = initializer-clause
C++11 additionally adds the following rule:
condition:
type-specifier-seq attribute-specifieropt declarator braced-init-list
Generally it means that the declaration you might put inside the condition is in fact nothing more than keeping the value of the condition expression named for further use, i.e. for the following code:
if (int x = 3 && true && 12 > 11) {
// x == 1 here...
x is evaluated as: 3 && true && (12 > 11).
Back to your questions:
3) C++11 allows you now to use direct initialization (with brace initializer) in such case, e.g:
if (int x { 3 && true && 12 > 11 }) {
4) The following: if ((int x = 1) && true) does not make sense according to the definition above as it does not fit the: "either expression or declaration" rule for the condition.

Related

What is this strange expression in GCC/Clang?

I have recently noticed an strange valid C/C++ expression in GCC/Clang which I have never seen before. Here is the example in C++, but similar expression works in C too:
int main(){
int z = 5;
auto x = ({z > 3 ? 3 : 2;}); // <-- expression
std::cout << x;
}
What it does is somehow obvious, but I like to know what it is called. Since it does not worth in MSVC, I guess it is a non-standard extension. But is there anything that works for MSVC too? especially in C?
It's called statement expr, used in GCC. Your expression ({z > 3 ? 3 : 2;}) can be translated to
if (z > 3) {x = 3;} else {x = 2;}
From documentation:
A compound statement enclosed in parentheses may appear as an
expression in GNU C. This allows you to use loops, switches, and local
variables within an expression.
In other word, it provides the ability to put a compound statement in an expression position.
Related post :
Emulating GCC Statement Expressions
Use of ({ ... }) brackets in macros to swallow the semicolon
It is called conditional operator . Return will be depend on condition either condition is true or false.
But in this case:
auto x = ({z > 3 ? 3 : 2;}); // <-- expression
if Z is greater than 3 returns 3 otherwise 2.
Basic syntax : Expression1? expression2: expression3;

Assignment in an 'if' statement. How is the output correct?

Code in case if the image not is visible.
#include <stdio.h>
int fun(int n)
{
if(n=4)
return n;
else
return 2*fun(n+1);
}
int main()
{
printf("%d", fun(2));
}
This is the code snippet and the output is given as 4 by the professor.
How is the output correct?
Is it possible that n=4 is assigned in the 'if-else' statement as the assignment operator is correct, but the "if" condition will not work as the syntax is wrong and the output will be directly given as 4.
The answer is correct, and there aren't any syntax errors.
= is an assignment operator, and in C/C++, (n = 4) is a valid expression that evaluates to true as long as the expression is not (n = 0), because n will then be considered as false by C. Note that in C/C++, 0 is false and everything else is true.
Hence, if (n = 4) is perfectly valid and always evaluates to true. Of course, in the process, there will also be an assignment involved.
Thus, what happens in the code above is that
the integer n is assigned the value 4 in n = 4
(n=4) as an expression returns true.
return n (4).
So the answer is 4.
Assignment in a if-else statement is valid syntax and the branching will depend on the value of n.
Example:
#include <iostream>
int main() {
int n = 2;
if (n = 0) {
std::cout << "Never printed\n";
}
if (n = 4) {
std::cout << "Always printed\n";
}
return 0;
}
Compiler explorer: https://godbolt.org/z/fEYPcq
You should use == operator to make a confront between two compatible values.
if(n = 4) assign 4 to n and then the if statement is always true. So the return value will always be 4.
"Is it possible that n = 4 is assigned in the if-else statement as the assignment operator is correct, but the if condition will not work as the syntax is wrong....?"
The syntax is not wrong and the if condition does work. It is perfectly valid. And yes, the assignment is also valid/correct.
With if (n = 4) you assign the value of 4 to the function-local variable n, although this makes less sense since n is a parameter and is meant to be feed with different values at each call to the function fun().
But I guess the intention of your professor is exactly to demonstrate this trickery, so it makes sense.
So the value of n is not 2 anymore; It is 4.
This is a valid expression for the if condition and evaluates to 1/true since the value/expression to be assigned is or does not evaluate not 0.
Usually a compiler will warn you about doing so nonetheless to avoid any undesired result here by suggesting optional parentheses around the assignment like: if ((n = 4)).
Clang:
warning: using the result of an assignment as a condition without parentheses [-Wparentheses]
GCC:
warning: suggest parentheses around assignment used as truth value [-Wparentheses]
If you explicitly want to remove these warnings, use the -Wno-parentheses flag. But it is recommended not to do so.
Since the if condition is true, it doesn't get to the recursive part in the else condition and the function fun immediately returns n which is 4.
So is the return value of 4 displayed by the call to printf() in the caller.
My favorite to remember that is this:
int war = false;
if (war = true) { launch nuke; }
Credits go to WTP.
Maybe you'll catch the joke. ;-)

Why is for(;;); an infinite loop? [duplicate]

This question already has answers here:
Why do for(;;) loops behave like infinite loops?
(2 answers)
Closed 5 years ago.
I've written infinite loops like this numerous times over the years in both C and C++, but today is the first time I really thought about it -- why is it an infinite loop when the condition clause is empty? One would expect you'd have to write something like for(;true;); to get a valid infinite loop?
while(); doesn't compile nor does while(;);
Anyways, I like the for(;;); syntax and use it often, but is it a special case to treat an empty condition block as true or are there other cases in C or C++ where an empty condition expression is interpreted as true?
The C Standard explicitly describes this behavior of for loops:
C11 Draft Standard §6.8.5.3 2
Both clause-1 and expression-3 can be omitted. An omitted expression-2
is replaced by a nonzero constant.
Similarly, for C++:
C++14 Draft Standard §6.5.3
2
Either or both of the condition and the expression can be omitted.
A missing condition makes the implied while clause equivalent to
while(true).
The behaviors are defined by the language (C++). For the for loop, the condition part is optional:
Syntax
formal syntax:
attr(optional) for ( init-statement condition(optional) ; iteration_expression(optional) ) statement
and (emphasis mine)
The above syntax produces code equivalent to:
{
init_statement
while ( condition ) {
statement
iteration_expression ;
}
}
Except that
3) Empty condition is equivalent to while(true)
For while loop, the condition part is necessary, it can't be ommitted.
Syntax
attr(optional) while ( condition ) statement

What does this ? operator in C++ do? [duplicate]

This question already has answers here:
What does the question mark character ('?') mean in C++?
(8 answers)
Closed 7 years ago.
Consider this function
template<class T> inline bool cx(T &a, T b) {return a < b ? a = b, 1 : 0;}
Can anyone explain what exactly this is doing? It seems different from the typical condition ? true : false format.
We could make it more clear like so:
return a < b ? (a = b, 1) : 0;
The parenthesized bit means "assign b to a, then use 1 as our value".
Comma-separated lists of values in C and C++ generally mean "evaluate all of these, but use the last one as the expression's value".
This combination is a little tricky, because it combines a comma operator with the conditional expression. It parses as follows:
a < b is the condition,
a = b, 1 is the "when true" expression
0 is the "when false" expression
The result of the comma operator is its last component, i.e. 1. The goal of employing the comma operator in the first place is to cause the side effect of assigning b to a.
You can execute several expression using ,
In this case if a < b, then assign b to a and return 1. According C++ grammar:
conditional-expression:
logical-or-expression
| logical-or-expression ? expression : assignment-expression
where
expression:
assignment-expression
| expression , assignment-expression
assignment-expression:
conditional-expression
| logical-or-expression assignment-operator initializer-clause
| throw-expression
The , operator just evaluates all the expressions, left to right, and evaluates to the value of the rightmost expression.
Your code is the same as...
if (a < b)
{
a = b;
return 1;
}
else
{
return 0;
}
Read it as:
if ( a < b )
{
a = b;
return ( 1 );
}
else
{
return ( 0 );
}
a < b ? a = b, 1 : 0 is parsed as (a < b) ? (a = b, 1) : 0, a normal conditional operator. When a < b is true, a = b, 1 is evaluated by assigning b to a and then "returning" 1. The net effect of cx(a,b) is thus to assign the larger value to a and to return 1 if a changed, 0 otherwise.

what's an expression and expression statement in c++?

I've read that usually statements in c++ end with a semi-colon; so that might help explain what an expression statement would be. But then what would you call an expression by giving an example?
In this case, are both just statements or expression statements or expressions?
int x;
x = 0;
An expression is "a sequence of operators and operands that specifies a computation" (that's the definition given in the C++ standard). Examples are 42, 2 + 2, "hello, world", and func("argument"). Assignments are expressions in C++; so are function calls.
I don't see a definition for the term "statement", but basically it's a chunk of code that performs some action. Examples are compound statements (consisting of zero or more other statements included in { ... }), if statements, goto statements, return statements, and expression statements. (In C++, but not in C, declarations are classified as statements.)
The terms statement and expression are defined very precisely by the language grammar.
An expression statement is a particular kind of statement. It consists of an optional expression followed by a semicolon. The expression is evaluated and any result is discarded. Usually this is used when the statement has side effects (otherwise there's not much point), but you can have a expression statement where the expression has no side effects. Examples are:
x = 42; // the expression happens to be an assignment
func("argument");
42; // no side effects, allowed but not useful
; // a null statement
The null statement is a special case. (I'm not sure why it's treated that way; in my opinion it would make more sense for it to be a disinct kind of statement. But that's the way the standard defines it.)
Note that
return 42;
is a statement, but it's not an expression statement. It contains an expression, but the expression (plus the ;) doesn't make up the entire statement.
These are expressions (remember math?):
1
6 * 7
a + b * 3
sin(3) + 7
a > b
a ? 1 : 0
func()
mystring + gimmeAString() + std::string("\n")
The following are all statements:
int x; // Also a declaration.
x = 0; // Also an assignment.
if(expr) { /*...*/ } // This is why it's called an "if-statement".
for(expr; expr; expr) { /*...*/ } // For-loop.
A statement is usually made up of an expression:
if(a > b) // a > b is an expr.
while(true) // true is an expr.
func(); // func() is an expr.
To understand what is an expression statement, you should first know what is an expression and what is an statement.
An expression in a programming language is a combination of one or more explicit values, constants, variables, operators, and functions that the programming language interprets (according to its particular rules of precedence and of association) and computes to produce ("to return", in a stateful environment) another value. This process, as for mathematical expressions, is called evaluation.
Source: https://en.wikipedia.org/wiki/Expression_(computer_science)
In other words expressions are a sort of data items. They can have single or multiple entities like constants and variables. These entities may be related or connected to each other by operators. Expressions may or may not have side effects, in that they evaluate to something by means of computation which changes a state. For instance numbers, things that look like mathematical formulas and calculations, assignments, function calls, logical evaluations, strings and string operations are all considered expressions.
function calls: According to MSDN, function calls are considered expressions. A function call is an expression that passes control and arguments (if any) to a function and has the form:
expression (expression-list opt) which is invoked by the ( ) function operator.
source: https://msdn.microsoft.com/en-us/library/be6ftfba.aspx
Some examples of expressions are:
46
18 * 3 + 22 / 2
a = 4
b = a + 3
c = b * -2
abs(c)
b >= c
c
"a string"
str = "some string"
strcat(str, " some thing else")
str2 = "some string" + " some other string" // in C++11 using string library
Statements are fragments of a program that execute in sequence and cause the computer to carry out some definite action. Some C++ statement types are:
expression statements;
compound statements;
selection statements;
iteration statements;
jump statements;
declaration statements;
try blocks;
atomic and synchronized blocks (TM TS).
Source: http://en.cppreference.com/w/cpp/language/statements
I've read usually statements in c++ ends with a semicon;
Yes usually! But not always. Consider the following piece of code which is a compound statement but does not end with a semicolon, rather it is enclosed between two curly braces:
{ // begining of a compound statement
int x; // A declaration statement
int y;
int z;
x = 2; // x = 2 is an expression, thus x = 2; with the trailing semicolon is an expression statement
y = 2 * x + 5;
if(y == 9) { // A control statement
z = 52;
} else { // A branching statement of a control statement
z = 0;
}
} // end of a compound statement
By now, as you might be guessing, an expression statement is any statement that has an expression followed by a semicolon. According to MSDN an expression statement is a statement that causes the expressions to be evaluated. No transfer of control or iteration takes place as a result of an expression statement.
Source: https://msdn.microsoft.com/en-us/library/s7ytfs2k.aspx
Some Examples of expression statements:
x = 4;
y = x * x + 10;
radius = 5;
pi = 3.141593;
circumference = 2. * pi * radius;
area = pi * radius * radius;
Therefore the following can not be considered expression statements since they transfer the control flow to another part of a program by calling a function:
printf("The control is passed to the printf function");
y = pow(x, 2);
side effects: A side effect refers to the modification of a state. Such as changing the value of a variable, writing some data on a disk showing a menu in the User Interface, etc.
Source: https://en.wikipedia.org/wiki/Side_effect_(computer_science)
Note that expression statements don't need to have side effects. That is they don't have to change or modify any state. For example if we consider a program's control flow as a state which could be modified, then the following expression statements
won't have any side effects over the program's control flow:
a = 8;
b = 10 + a;
k++;
Wheres the following expression statement would have a side effect, since it would pass the control flow to sqrt() function, thus changing a state:
d = sqrt(a); // The control flow is passed to sqrt() function
If we consider the value of a variable as a state as well, modifying it would be a side effect thus all of expression statements above have side effects, because they all modify a state. An expression statement that does not have any side effect is not very useful. Consider the following expression statements:
x = 7; // This expression statement sets the value of x to 7
x; // This expression statement is evaluated to 7 and does nothing useful
In the above example x = 7; is a useful expression statement for us. It sets the value of x to 7 by = the assignment operator. But x; evaluates to 7 and it doesn't do anything useful.
According to The C++ Programming Language by Bjarne Stroustrup Special(3rd) Edition, a statement is basically any declaration, function call, assignment, or conditional. Though, if you look at the grammar, it is much more complicated than that. An expression, in simple terms, is any math or logical operation(s).
The wikipedia links that ok posted in his answer can be of help too.
In my opinion,
a statement *states* the purpose of a code block. i.e. we say this block of code if(){} is an if-statement, or this x=42; is an expression statement. So code such as 42; serves no purporse, therefore, this is *not* a statement.
and,
an expression is any legal combination of symbols that represents a value (Credit to Webopedia); it combines variables and constants to produce new values(Quoted from Chapter 2 in The C Programming Language). Therefore, it also has a mathematical connotation. For instance, number 42 in x=42; is an expression (x=42; is not an expression but rather an expression statement), or func(x) is an expression because it will evaluate to something. On the contrary, int x; is not an expression because it is not representing any value.
I think this excerpt from a technical book is most useful and clear.
Read the paragraphs till the start of 1.4.2 statements would be useful enough.
An expression is "a sequence of operators and operands that specifies a computation"
These are expressions:
1
2 + 2
"hi"
cout << "Hello, World!"
The last one is indeed an expression; << is the output operator, cout (of type ostream) and "Hello, World!" (string literals) are the operands. The operator returns the left-hand operand, so (cout << "Hello, ") << "World!" is also a valid expression but also not a statement.
An expression becomes an expression statement when it is followed by a semicolon:
1;
2 + 2;
"hi";
cout << "Hello, World!";
An expression is part of a statement, OR a statement itself.
int x; is a statement and expression.
See this : http://en.wikipedia.org/wiki/Expression_%28programming%29
http://en.wikipedia.org/wiki/Statement_%28programming%29