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.

Filter by
Sorted by
Tagged with
2223votes
27answers
1.0mviews

How to write a switch statement in Ruby

How do I write a switch statement in Ruby?
user avatar
  • 323k
410votes
3answers
1.6mviews

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 ...
user avatar
  • 12.3k
368votes
30answers
674kviews

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?
user avatar
  • 5,185
243votes
3answers
179kviews

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"...
user avatar
  • 19.9k
241votes
8answers
499kviews

Replace all elements of Python NumPy Array that are greater than some value

I have a 2D NumPy array and would like to replace all values in it greater than or equal to a threshold T with 255.0. To my knowledge, the most fundamental way would be: shape = arr.shape result = np....
user avatar
  • 2,822
241votes
5answers
175kviews

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, ...
user avatar
  • 3,590
240votes
27answers
361kviews

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 ? ...
user avatar
  • 3,507
232votes
4answers
92kviews

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 ...
user avatar
193votes
3answers
66kviews

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 ...
user avatar
191votes
3answers
634kviews

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. ...
user avatar
  • 7,252
174votes
6answers
322kviews

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 ...
user avatar
  • 3,181
172votes
4answers
10kviews

An expensive jump with GCC 5.4.0

I had a function which looked like this (showing only the important part): double CompareShifted(const std::vector<uint16_t>& l, const std::vector<uint16_t> &curr, int shift, int ...
user avatar
  • 1,113
170votes
9answers
140kviews

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 ...
user avatar
159votes
18answers
717kviews

Can you use if/else conditions in CSS?

I would like to use conditions in my CSS. The idea is that I have a variable that I replace when the site is run to generate the right style-sheet. I want it so that according to this variable the ...
user avatar
  • 119k
157votes
12answers
141kviews

Is it good practice to use the xor operator for boolean checks? [closed]

I personally like the exclusive or, ^, operator when it makes sense in the context of boolean checks because of its conciseness. I much prefer to write if (boolean1 ^ boolean2) { //do it } than ...
user avatar
  • 28.8k

15 30 50 per page
1
2 3 4 5
1160