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
189
votes
8 answers

Why do empty JavaScript arrays evaluate to true in conditional structures?

I was encountering a lot of bugs in my code because I expected this expression: Boolean([]); to evaluate to false. But this wasn't the case as it evaluated to true. Therefore, functions that possibly returned [] like this: // Where myCollection…
racl101
  • 3,760
  • 4
  • 34
  • 33
183
votes
19 answers

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 style-sheet changes! It looks like: [if {var} eq 2…
Haim Evgi
  • 123,187
  • 45
  • 217
  • 223
172
votes
4 answers

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& l, const std::vector &curr, int shift, int shiftY) { ... for(std::size_t i=std::max(0,-shift);i
Jakub Jůza
  • 1,113
  • 2
  • 8
  • 13
164
votes
7 answers

Creating a new column based on if-elif-else condition

I have a DataFrame df: A B a 2 2 b 3 1 c 1 3 I want to create a new column based on the following criteria: if row A == B: 0 if rowA > B: 1 if row A < B: -1 so given the above table, it should be: A B C a 2 2 …
nutship
  • 4,624
  • 13
  • 47
  • 64
161
votes
12 answers

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

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 if((boolean1 && !boolean2) || (boolean2 &&…
Peter
  • 29,498
  • 21
  • 89
  • 122
150
votes
12 answers

If condition A is matched, condition B needs to be matched in order to do action C

My question is: if (/* condition A */) { if(/* condition B */) { /* do action C */ } else /* ... */ } else { /* do action C */ } Is it possible to just write the code of action C one time instead of twice? How…
145
votes
10 answers

Determining if a variable is within range?

I need to write a loop that does something like: if i (1..10) do thing 1 elsif i (11..20) do thing 2 elsif i (21..30) do thing 3 etc... But so far have gone down the wrong paths in terms of syntax.
btw
  • 7,006
  • 9
  • 40
  • 40
142
votes
8 answers

Can I use conditional statements with EJS templates (in JMVC)?

and if yes, what is the syntax? My goal is to prepend an 's' to the word 'comment' when there is more than one. in an jQuery.ejs template in a JMVC app. The following breaks. I can't find any docs for conditionals... <%=commentsNumber%> comment<% if…
Regis Zaleman
  • 3,182
  • 6
  • 29
  • 30
141
votes
6 answers

Inline conditions in Lua (a == b ? "yes" : "no")?

Is there anyway to use inline conditions in Lua? Such as: print("blah: " .. (a == true ? "blah" : "nahblah"))
Softnux
  • 2,440
  • 4
  • 20
  • 21
138
votes
10 answers

Set value of one Pandas column based on value in another column

I need to set the value of one column based on the value of another in a Pandas dataframe. This is the logic: if df['c1'] == 'Value': df['c2'] = 10 else: df['c2'] = df['c3'] I am unable to get this to do what I want, which is to simply…
NLR
  • 1,714
  • 2
  • 11
  • 21
135
votes
10 answers

Conditional with statement in Python

Is there a way to begin a block of code with a with statement, but conditionally? Something like: if needs_with(): with get_stuff() as gs: # do nearly the same large block of stuff, # involving gs or not, depending on needs_with() To clarify,…
norman
  • 5,128
  • 13
  • 44
  • 75
134
votes
14 answers

JavaScript: using a condition in switch case

How can I use a condition inside a switch statement for JavaScript? In the example below, a case should match when the variable liCount is <= 5 and > 0; however, my code does not work: switch (liCount) { case 0: setLayoutState("start"); …
haemse
  • 3,971
  • 5
  • 28
  • 40
134
votes
6 answers

Replacing Numpy elements if condition is met

I have a large numpy array that I need to manipulate so that each element is changed to either a 1 or 0 if a condition is met (will be used as a pixel mask later). There are about 8 million elements in the array and my current method takes too long…
ChrisFro
  • 2,723
  • 4
  • 15
  • 8
126
votes
19 answers

IF... OR IF... in a windows batch file

Is there a way to write an IF OR IF conditional statement in a windows batch-file? For example: IF [%var%] == [1] OR IF [%var%] == [2] ECHO TRUE
Anthony Miller
  • 15,101
  • 28
  • 69
  • 98
123
votes
8 answers

IN Clause with NULL or IS NULL

Postgres is the database Can I use a NULL value for a IN clause? example: SELECT * FROM tbl_name WHERE id_field IN ('value1', 'value2', 'value3', NULL) I want to limit to these four values. I have tried the above statement and it doesn't work, well…
Phill Pafford
  • 83,471
  • 91
  • 263
  • 383