answersLogoWhite

0


Best Answer

No. An if statement does not require an elseclause and the expression(s) do not return anything to the caller, whereas the conditional operator always executes one of two expressions and always returns the value of the executed expression back to the caller. The executed expression may be yet another conditional operator, thus allowing simulation of nested ifs and if...else if... else statements.

Consider the following example:

int x = rand();

if( x > 100 ) x = 100;

We can achieve the same result with a conditional operator:

int y = rand();

y = y>100 ? 100 : y;

However, if we were to expand this statement to an if statement we would not get the original if statement shown above:

int z = rand();

if( z > 100 ) z = 100;

else z = z;

The else clause is clearly unnecessary in this case, so the original if statement would be the statement of choice here.

As a general rule, if you can make use of the return value in a conditional operator, and must return one of at least two values, then use the conditional operator. Otherwise use an if statement.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Can a conditional operator replace an if statement always?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Why ternary operator is not overloaded?

The ternary operator (known as the conditional operator in C++) cannot be overloaded because it is impossible to pass a test operand and two expression operands (either or both of which may be comma-separated) to a function. You can only pass values or references as arguments to a function. Even if it were possible, built-in functions and operators that rely on the conditional operator would likely break. Like all the other operators that cannot be overloaded (sizeof, typeid, ::, . and .*) the results must always be predictable because built-in operators and functions rely on them so heavily.


What are unconditional statements?

An unconditional statement is one which would happen always (unconditionally).Conditional statement:if(x > 5)print "Hello!"Unconditional statement:print "Hello!"This term also comes up when speaking of assembly instructions. An unconditional jump would be a line of assembly which always executes the jump. This is in contrast to a conditional jump (also known as a branch) which will execute the jump only if some statement evaluates to true.


Which operator is called ternary operator?

A ternary operator is an operator that requires three operands, as opposed to a binary operator that requires two operands and a unary operator that requires just one operand. C++ has just one ternary operator, the conditional ternary operator: <boolean expression> ? <expression #1> : <expression #2>; If the boolean expression evaluates true, the first expression is evaluated, otherwise the second expression is evaluated. A typical usage of this operator is to return the larger (or smaller) of two values of type T: template<typename T> T max (T a, T b) {return a<b ? b : a}; template<typename T> T min (T a, T b) {return a<b ? a : b}; These are really nothing more than notational shorthand for the following: template<typename T> T max (T a, T b) {if (a<b) return b; else return a; }; template<typename T> T min (T a, T b) {if (a<b) return a; else return b;}; However, because ternary expressions are evaluated, the return value of the expression can be used in more complex expressions: int a=42, b=0; // ... int c = ((a>b ? a : b) = 1); In the above expression, whichever is the larger of a and b will be assigned the value 1 which will also be assigned to c. Thus a and c become 1 while b remains 0.


What is the for loop in c plus plus?

In C, also in C++, the for loop executes zero or more times, until the test expression is false...for (init-expression; test-expression; loop-expression) statement;/* init-expression executed once, at beginning *//* statement executes zero or more times, until test-expression is false, test first *//* loop-expression evaluated at end of each iteration */


Is sizeof an operator or function why?

You cannot overload the sizeof() operator because that could introduce uncertainty in its evaluation. The sizeof() operator must always produce an accurate and logically predictable result, thus all user-intervention is completely forbidden.

Related questions

Is the inverse of a conditional statement is always true?

No.


A conditional statement is always logically equivalent to its?

Contrapositive


What is a true statement that combines a true conditional statement and its true converse?

always true


What is a true statement that combines a true conditional statement and is its true converse?

always true


Do you know if the reverse of a conditional statement is always true?

false


Choose the statement that are always logically equivalent?

a conditional and its contrapositive


Which statement always has the same truth value as the conditional?

The statement "if not p, then not q" always has the same truth value as the conditional "if p, then q." They are logically equivalent.


The converse and inverse of a conditional statement are logically equivalent?

This is not always true.


Is if you like math then you like science an inverse?

In order to determine if this is an inverse, you need to share the original conditional statement. With a conditional statement, you have if p, then q. The inverse of such statement is if not p then not q. Conditional statement If you like math, then you like science. Inverse If you do not like math, then you do not like science. If the conditional statement is true, the inverse is not always true (which is why it is not used in proofs). For example: Conditional Statement If two numbers are odd, then their sum is even (always true) Inverse If two numbers are not odd, then their sum is not even (never true)


What is a statement that is always true forwards and backwards?

A bi-conditional statement can be true or false. If it is true, then both forward and backward statements are true. See Bi-conditional StatementIn English grammarThe statement, Love you! could be true too if said/written backward as You love!


Is the converse of a true conditional statement always false?

No. Consider the statement "If I'm alive, then I'm not dead." That statement is true. The converse is "If I'm not dead, then I'm alive.", which is also true.


Is The converse of a biconditional statement is always true?

No, not always. It depends on if the original biconditional statement is true. For example take the following biconditional statement:x = 3 if and only if x2 = 9.From this biconditional statement we can extract two conditional statements (hence why it is called a bicondional statement):The Conditional Statement: If x = 3 then x2 = 9.This statement is true. However, the second statement we can extract is called the converse.The Converse: If x2=9 then x = 3.This statement is false, because x could also equal -3. Since this is false, it makes the entire original biconditional statement false.All it takes to prove that a statement is false is one counterexample.