Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Short hand If statement without else

Tags:

c#

lambda

linq

I am trying to have a short hand for an if statement as I am building an expression query and if test is null the accessor causes an error.

test != null ? test.Contains("mystring") : NO_VLAUE

I am looking for:

test != null ? test.Contains("mystring")

otherwise ignore.

I know I can use a ?? for is null but is there an inverse.

Thanks in advance.

like image 302
markpcasey Avatar asked Nov 16 '11 14:11

markpcasey


People also ask

How do you represent an if statement in shorthand?

Use the ternary operator to use a shorthand for an if else statement. The ternary operator starts with a condition that is followed by a question mark ? , then a value to return if the condition is truthy, a colon : , and a value to return if the condition is falsy.

What is the alternative of if else?

The Ternary Operator One of my favourite alternatives to if...else is the ternary operator. Here expressionIfTrue will be evaluated if condition evaluates to true ; otherwise expressionIfFalse will be evaluated. The beauty of ternary operators is they can be used on the right-hand side of an assignment.

Can I use if without else JavaScript?

You don't need an else statement, you can use ng-if . The ng-if statement is also available without an else condition.

How do you write the if else condition?

Use if to specify a block of code to be executed, if a specified condition is true. Use else to specify a block of code to be executed, if the same condition is false. Use else if to specify a new condition to test, if the first condition is false. Use switch to specify many alternative blocks of code to be executed.

What is the use of short hand if else in C++?

C++ Short Hand If Else Previous Next Short Hand If...Else (Ternary Operator) There is also a short-hand if else, which is known as the ternary operator because it consists of three operands. It can be used to replace multiple lines of code with a single line. It is often used to replace simple if else statements:

When to use the if/else statement in JavaScript?

On the other hand if you need a code to execute “A” when true and “B” when false, then you can use the if / else statement.

How to write one line if without else statement in Python?

You can write Python one line if without else statement by just avoiding an else. For it just writes the if statement in a single line! No needed tricks (like using the semicolon) that help you create one-liner statements. If body with only one statement, it’s just as simple as avoiding the line break.

Can “if statements” exist without their “else” counterparts?

In the main text of this section you mention if/else statements but never mention that “if statements” can exist without their “else” counterparts…or at least my code worked like that. Why? An if statement looks at any and every thing in the parentheses and if true, executes block of code that follows.


2 Answers

It sounds like you want test != null && test.Contains("mystring")

Your question as asked doesn't make sense. All expressions, including the conditional operator, must have a value. What would you expect that expression to evaluate to if test is null?

You probably want it to be false if test is null.
In other words, you want it to be true if test isn't null and it contains mystring.

like image 142
SLaks Avatar answered Oct 21 '22 09:10

SLaks


It sounds like you might want:

test != null && test.Contains("mystring")

That will evaluate to false if test is null - is that what you want? Basically you need to say what you want to happen if test is null, as otherwise it can't be used as an expression.

like image 29
Jon Skeet Avatar answered Oct 21 '22 10:10

Jon Skeet