Questions tagged [inline-if]

12 questions
57
votes
5 answers

Python lambda with if but without else

I was writing some lambda functions and couldn't figure this out. Is there a way to have something like lambda x: x if (x<3) in python? As lambda a,b: a if (a > b) else b works ok. So far lambda x: x < 3 and x or None seems to be the closest i have…
root
  • 76,608
  • 25
  • 108
  • 120
38
votes
8 answers

Math.Max vs inline if - what are the differences?

I was working on a project today, and found myself using Math.Max in several places and inline if statements in other places. So, I was wondering if anybody knew which is "better"... or rather, what the real differences are. For example, in the…
chezy525
  • 4,025
  • 6
  • 28
  • 41
26
votes
3 answers

Can I write an inline if with HTML content?

I want to write something like: @( checkCondition ? "Right!" : "") But it is showing the source code instead the HTML, there is a easy way to do this? Thank you!
Santiago
  • 2,190
  • 10
  • 30
  • 59
9
votes
3 answers

Is there any difference between '?:' and an if statement in objective c?

Is there a difference between using the '?:' conditional and the simple 'if-then-else' statement? Is it simply another way to do it, or does it actually use less space/take less time to read than 'if' statements? Example: If statement: if…
8
votes
3 answers

If and Inline if, what are the advantages and disadvantages?

I'm a little curious about the difference between if and inline if, in Python. Which one is better? Is there any reason to use inline if, other than the fact that it's shorter? Also, is there anything wrong with this statement? I'm getting a…
user2850589
5
votes
4 answers

How to express conditional execution inside Python lambdas?

What I found out: In Dive in to Python I read about the peculiar nature of and and or operators and how shortcircuit evaluation of boolean operators may be used to express conditionals more succinctly via the and-or trick that works very much like…
2
votes
3 answers

How to write strongly typed lambda expressions?

I want to write a lambda expression within an inline if statement. But inline if statement must have strong type results. MyType obj = someObj.IsOk ? null : () => { MyType o = new MyType(intVal); o.PropertyName = false; return o; }; Of…
Robert Koritnik
  • 103,639
  • 52
  • 277
  • 404
1
vote
4 answers

inline if and interfaces (polymorphism)

public class Foo : IFooBarable {...} public class Bar : IFooBarable {...} So why then will this not compile... int a = 1; IFooBarable ting = a == 1 ? new Foo() : new Bar(); but this will... IFooBarable ting = a == 1 ? new Foo() : new…
gingerbreadboy
  • 7,386
  • 5
  • 36
  • 62
0
votes
1 answer

Excel VBA inline IF statement makes variable 0

I am trying to reduce the amount of code from If... Then... Else statements and trying to use IIf(expression, true, false) for setting variables. I am having a problem where somehow the variable gets set to 0 and it's quite annoying. For…
icebird76
  • 742
  • 3
  • 14
  • 36
0
votes
1 answer

vb.net inline IF with OR... not evaluating

I'm working on a small problem where I'm trying to show/hide a panel based on two criteria A specific data field must not be blank The specific data filed must also not equal "Not Relocatable" Unfortunately this doesn't seem to be working for me…
Chase Florell
  • 46,378
  • 57
  • 186
  • 376
-2
votes
1 answer

C++ inline if fails in switch

I just found out the hard way an inline if (A?B:C) does not work as expected in a switch statement. where A a boolean, B and C both integer unequal to 0. The result of this statement is 0 when placed inside a switch. I found a stackoverflow post [1]…
puccha
  • 13
  • 1
  • 2
-3
votes
1 answer

implementation of if command in variables

here is my code: var a=false; var b=false; var c=true; var d=false; // var x = a ? a : (b ? b : (c ? c: false)) ; // for(i=0;i<11;i++){ document.write(x); } the inline if command check's which variable (a,b,c) is true then equal's x to that and…
mk rowling
  • 203
  • 1
  • 12