Questions tagged [conditional]

Conditional has various meanings for various languages and probably should be avoided as a tag.

Conditional has various meanings for various languages and probably should be avoided as a tag.

In a general programming sense, conditional can refer to a situation where the output or the portion of the code to be executed depends on some particular condition.

6604 questions
362
votes
2 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
  • 11,342
  • 19
  • 69
  • 105
324
votes
28 answers

Laravel Checking If a Record Exists

I am new to Laravel. Please excuse the newbie question but 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
  • 4,675
  • 8
  • 27
  • 45
224
votes
3 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
  • 18,301
  • 39
  • 106
  • 153
212
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
  • 3,210
  • 3
  • 24
  • 27
184
votes
3 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
  • 6,812
  • 15
  • 40
  • 59
157
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
  • 78,484
  • 22
  • 123
  • 143
154
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
  • 28,255
  • 17
  • 83
  • 120
138
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
  • 6,456
  • 9
  • 37
  • 40
116
votes
5 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
  • 3,804
  • 12
  • 43
  • 58
110
votes
5 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,115
  • 3
  • 12
  • 8
109
votes
3 answers

Syntax for if/else condition in SCSS mixin

Hi I'm trying to learn SASS/SCSS and am trying to refactor my own mixin for clearfix what I'd like is for the mixin to be based on whether I pass the mixin a width. thoughts so far (pseudo code only as I will be including other mixins) @mixin…
clairesuzy
  • 26,108
  • 7
  • 51
  • 51
108
votes
9 answers

#ifdef #ifndef in Java

I doubt if there is a way to make compile-time conditions in Java like #ifdef #ifndef in C++. My problem is that have an algorithm written in Java, and I have different running time improves to that algorithm. So I want to measure how much time I…
jutky
  • 3,746
  • 6
  • 29
  • 43
106
votes
6 answers

VBA - how to conditionally skip a for loop iteration

I have a for loop over an array. What I want to do is test for a certain condition in the loop and skip to the next iteration if true: For i = LBound(Schedule, 1) To UBound(Schedule, 1) If (Schedule(i, 1) < ReferenceDate) Then …
Richard H
  • 34,219
  • 33
  • 105
  • 133
105
votes
14 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
  • 12,722
  • 27
  • 65
  • 96
101
votes
13 answers

MySQL Conditional Insert

I am having a difficult time forming a conditional INSERT I have x_table with columns (instance, user, item) where instance ID is unique. I want to insert a new row only if the user already does not have a given item. For example trying to insert…
The Unknown
  • 17,346
  • 29
  • 69
  • 91
1
2 3
99 100