Can you explain to me: What is the difference between if else and if(...)?...:... As for me, they do the same thing, but the teacher says that the first option is, to put it mildly, a bad code.
2 answers
Obviously, under construction
f(...)?...:... you mean conditional statement
logical-OR-expression ? expression : conditional-expression the result of which is the calculation of the value of the expression.
If the value of a logical expression logical-OR-expression is a nonzero value, then the value of the whole expression will be the value of the expression after the ? . Otherwise, the value of the whole expression will be the value of the conditional-expression after the sign :
That is, like any other expression, this conditional operator can be used in expressions.
As for the if-else clause, this is a C language clause that cannot be used in expressions.
Consider the following demo program written in C.
#include <stdio.h> #include <stdlib.h> #include <time.h> int main(void) { const int MAX_VALUE = 100; srand( ( unsigned )time( NULL ) ); int x = rand() % MAX_VALUE; printf( "%d is %s\n", x, x % 2 == 0 ? "even" : "odd" ); if ( x % 2 == 0 ) { printf( "%d is %s\n", x, "even" ); } else { printf( "%d is %s\n", x, "odd" ); } return 0; } The program generates a random number x in the interval [0, MAX_VALUE), that is, from 0 to 100 (exclusively).
int x = rand() % MAX_VALUE; Then it is checked whether this number is even or odd and the corresponding message is output to the console. This check and message output is performed in two ways.
In the first case, the printf output function uses a conditional operator as an expression that specifies the third argument.
printf( "%d is %s\n", x, x % 2 == 0 ? "even" : "odd" ); ^^^^^^^^^^^^^^^^^^^^^^^^^^^ In the second case, the if-else clause and, accordingly, two printf calls are used, depending on the value of the condition in the if.
if ( x % 2 == 0 ) { printf( "%d is %s\n", x, "even" ); } else { printf( "%d is %s\n", x, "odd" ); } And you could insert other sentences into each nested clause after if or else , because you can use a compound clause as a nested clause.
For example, the above if-else sentence you could write as
if ( x % 2 == 0 ) { printf( "%d is ", x ); printf( "%s\n", "even" ); } else { printf( "%d is ", x ); printf( "%s\n", "odd" ); } In general, you cannot do the same with a conditional operator, since it is an expression consisting of three subexpressions that are calculated in a certain way. For example, you cannot include a loop clause. The only exception is expression clauses, like the specified printf call, which can also be used in expressions.
The output of the program to the console may look, for example, as
80 is even 80 is even A classic example of using a conditional statement in C is to write a macro to calculate the maximum or minimum value of two expressions. For example,
#define max(a, b) ((a) > (b) ? (a) : (b)) In C ++, there is a corresponding standard template function std::max , declared in the header <algorithm> , the body of which consists of only one return clause with an expression representing an expression with a conditional operator
template <class T> constexpr const T& max( const T& a, const T& b ) { return a < b ? b : a; } - "You can't do the same with the conditional operator" - where is the error here: ideone.com/sBkuKU ? - Harry
- @Harry There is no error here. In this sentence with a conditional operator (x% 2 == 0)? (printf ("% d is", x), printf ("% s \ n", "even")): (printf ("% d is", x), printf ("% s \ n", "odd ")); } subexpressions with the comma operator are used. Expressions can be arbitrarily complex and consist of numerous subexpressions. It’s just that in my example in the if-else clause, clauses-expressions are used, and in general, for example, cycles can be used. - Vlad from Moscow
- one@Harry I corrected the answer to make it clearer. - Vlad from Moscow
if is a statement of language. Operator ?: Is an operator in an expression. These are entities of completely different nature, having nothing in common with each other.
Operators are used in expressions and their operands are other expressions (subexpressions). Instructions cannot be used in expressions.
(See also What action is hidden behind a comma? )
if(...)?...:...??? What is it all about? - AnTif else, sometimes with? :? :. - HolyBlackCat