Questions tagged [conditional-statements]

"In computer science, conditional statements, conditional expressions and conditional constructs are features of a programming language which perform different computations or actions depending on whether a programmer-specified boolean condition evaluates to true or false. Apart from the case of branch prediction, this is always achieved by selectively altering the control flow based on some condition." -- Wikipedia

Read on Wikipedia

19093 questions
2285
votes
28 answers

How to write a switch statement in Ruby

How do I write a switch statement in Ruby?
readonly
  • 343,444
  • 107
  • 203
  • 205
569
votes
15 answers

How to write inline if statement for print?

I need to print some stuff only when a boolean variable is set to True. So, after looking at this, I tried with a simple example: >>> a = 100 >>> b = True >>> print a if b File "", line 1 print a if b ^ SyntaxError: invalid…
Ricky Robinson
  • 21,798
  • 42
  • 129
  • 185
558
votes
35 answers

Check whether a String is not Null and not Empty

How can I check whether a string is not null and not empty? public void doStuff(String str) { if (str != null && str != "**here I want to check the 'str' is empty or not**") { /* handle empty string */ } /* ... */ }
user405398
456
votes
4 answers

Check if something is (not) in a list in Python

I have a list of tuples in Python, and I have a conditional where I want to take the branch ONLY if the tuple is not in the list (if it is in the list, then I don't want to take the if branch) if curr_x -1 > 0 and (curr_x-1 , curr_y) not in myList:…
Zack
  • 13,454
  • 24
  • 75
  • 113
401
votes
33 answers

Laravel Checking If a Record Exists

I am new to Laravel. How do I find if a record exists? $user = User::where('email', '=', Input::get('email')); What can I do here to see if $user has a record?
Ben
  • 5,627
  • 9
  • 35
  • 49
356
votes
32 answers

How to use conditional statement within child attribute of a Flutter Widget (Center Widget)

So far whenever I needed to use a conditional statement within a Widget I have done the following (Using Center and Containers as simplified dummy examples): new Center( child: condition == true ? new Container() : new Container() ) Though when…
Marko
  • 4,857
  • 5
  • 17
  • 21
275
votes
5 answers

How do I negate a test with regular expressions in a bash script?

Using GNU bash (version 4.0.35(1)-release (x86_64-suse-linux-gnu), I would like to negate a test with Regular Expressions. For example, I would like to conditionally add a path to the PATH variable, if the path is not already there, as…
David Rogers
  • 4,010
  • 3
  • 29
  • 28
273
votes
8 answers

Replace all elements of NumPy array that are greater than some value

I have a 2D NumPy array. How do I replace all values in it greater than a threshold T = 255 with a value x = 255? A slow for-loop based method would be: # arr = arr.copy() # Optionally, do not modify original arr. for i in range(arr.shape[0]): …
NLi10Me
  • 3,182
  • 2
  • 13
  • 15
271
votes
4 answers

What command means "do nothing" in a conditional in Bash?

Sometimes when making conditionals, I need the code to do nothing, e.g., here, I want Bash to do nothing when $a is greater than "10", print "1" if $a is less than "5", otherwise, print "2": if [ "$a" -ge 10 ] then elif [ "$a" -le 5 ] then echo…
Village
  • 22,513
  • 46
  • 122
  • 163
239
votes
4 answers

Why is the use of len(SEQUENCE) in condition values considered incorrect by Pylint?

Considering this code snippet: from os import walk files = [] for (dirpath, _, filenames) in walk(mydir): # More code that modifies files if len(files) == 0: # <-- C1801 return None I was alarmed by Pylint with this message regarding the…
E_net4
  • 27,810
  • 13
  • 101
  • 139
205
votes
5 answers

How to combine multiple conditions to subset a data-frame using "OR"?

I have a data.frame in R. I want to try two different conditions on two different columns, but I want these conditions to be inclusive. Therefore, I would like to use "OR" to combine the conditions. I have used the following syntax before with lot…
Sam
  • 7,922
  • 16
  • 47
  • 62
203
votes
3 answers

Are "elseif" and "else if" completely synonymous?

Are elseif and else if completely synonymous, or is there a difference? Does Zend have an accepted "standard" on which one to use? While I personally dislike seeing elseif in the code, I just need to know if they're synonymous and the PHP manual…
Theodore R. Smith
  • 21,848
  • 12
  • 65
  • 91
199
votes
7 answers

Conditional Replace Pandas

I have a DataFrame, and I want to replace the values in a particular column that exceed a value with zero. I had thought this was a way of achieving this: df[df.my_channel > 20000].my_channel = 0 If I copy the channel into a new data frame it's…
BMichell
  • 3,581
  • 5
  • 23
  • 31
193
votes
3 answers

Conditional import of modules in Python

In my program I want to import simplejson or json based on OS being Linux or Windows. I take the OS name as input from the user. Now, is it correct to do it with a condition like this? osys = raw_input("Press l for linux, w for Windows:") if (osys…
Tim
  • 1,971
  • 2
  • 12
  • 3
193
votes
9 answers

Javascript switch vs. if...else if...else

Guys I have a couple of questions: Is there a performance difference in JavaScript between a switch statement and an if...else? If so why? Is the behavior of switch and if...else different across browsers? (FireFox, IE, Chrome, Opera,…
John Hartsock
  • 85,422
  • 23
  • 131
  • 146
1
2 3
99 100