Ternary operator if I avoid writing expression 2 it works but if I not write expression 3 it gives an error duplicate

0 votes

Code

#include<iostream>

int main()
{
    int a=4,b,c;
    a==3 ? : b=a*a ;     // Works fine
    a==4 ? c=a*a : ;     // ERROR Expected Primary expression before ;
}

1st conditional statement

 I did not write 'Expression 2' but it will not produce error 

2nd Conditional statement

 I did not write 'Expression 3' it gives an error 

so why it is differentiating in 'Expression 2' and 'Expression 3' ?


Jun 28, 2022 in C++ by Nicholas
• 7,760 points
1,567 views

1 answer to this question.

0 votes
6.8 Conditionals with Missing Opponents

In a conditional statement, the middle operand might be omitted.

If the first operand is nonzero, the value of the conditional expression is returned.

As a result, the expression

x ?: y

If x is nonzero, it has the value of y; otherwise, it has the value of x.

This example is exactly the same as

x ? x: y

The option to omit the middle operand is not really beneficial in this basic scenario.

When the first operand has or may have a side effect (if it is a macro parameter), it becomes beneficial.

The side effect would then be performed twice by repeating the operand in the middle.

By omitting the middle operand, the previously computed value is used without the unwanted implications of recomputing it.
answered Jun 28, 2022 by Damon
• 4,960 points

Related Questions In C++

0 votes
0 answers

Code Not working in VS Code but works in OnlineGDB

I am practicing about primeSieve with C++ language in VS Code 1.57.1. Can ...READ MORE

Jun 1, 2022 in C++ by Nicholas
• 7,760 points
720 views
0 votes
1 answer

How do I erase an element from std::vector<> by index?

You might perform the following to remove a single element: std::vector<int> vec; vec.push_back(6); vec.push_back(-17); vec.push_back(12); // Deletes the second element (vec[1]) vec.erase(std::next(vec.begin())); Alternatively, to remove many elements at once: // ...READ MORE

answered Jun 10, 2022 in C++ by Damon
• 4,960 points
1,482 views
0 votes
0 answers

How can I terminate an infinite loop in Turbo C?

I get caught in an infinite loop.  How can I break this cycle?  Cntrl-C was attempted, however it had little effect.  I have no idea how to stop it. main() { ...READ MORE

Jul 13, 2022 in C++ by Nicholas
• 7,760 points
2,319 views
0 votes
1 answer

"We do not use C++ exceptions" — What's the alternative? Let it crash?

If you don't utilise exceptions, by definition, ...READ MORE

answered Aug 2, 2022 in C++ by Damon
• 4,960 points
502 views
0 votes
1 answer

setuptools: build shared libary from C++ code, then build Cython wrapper linked to shared libary

There is a seemingly undocumented feature of setup that ...READ MORE

answered Sep 11, 2018 in Python by Priyaj
• 58,020 points
686 views
0 votes
1 answer

setuptools: build shared libary from C++ code, then build Cython wrapper linked to shared libary

There is a seemingly undocumented feature of setup that ...READ MORE

answered Sep 21, 2018 in Python by Priyaj
• 58,020 points
2,386 views
0 votes
1 answer

How to pass large records to map/reduce tasks?

Hadoop is not designed for records about ...READ MORE

answered Sep 25, 2018 in Big Data Hadoop by Frankie
• 9,830 points
1,448 views
0 votes
1 answer

Invalid method parameters for eth_sendTransaction

params needs to be an array, try {"jsonrpc":"2.0","method":"eth_se ...READ MORE

answered Sep 28, 2018 in Blockchain by digger
• 26,740 points
1,854 views
0 votes
1 answer

Ternary operator ?: vs if...else

It's not any faster.  There is one difference when you can initialize a constant variable using an expression: const int x = (a<b) ? b ...READ MORE

answered Jun 7, 2022 in C++ by Damon
• 4,960 points
1,086 views
0 votes
1 answer

Is 'If Else' statement indentation important or not in C++? [duplicate]

White space has no effect on the understanding of code in C and C++.  That is not to say that the programmer should be unconcerned about its misuse. The easiest method to demonstrate what the above code truly represents is to specify all of the inferred braces directly, as seen below.  The 'if then' or 'otherwise' clause only affects one line of code in the if statement with no brackets. This is one of the reasons why people strive to insist on 'proper coding standards' to guarantee that other people can clearly grasp the programmer's flow and meaning. while(c != cols) { ...READ MORE

answered Jun 27, 2022 in C++ by Damon
• 4,960 points
555 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP