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
99
votes
10 answers

Dart null / false / empty checking: How to write this shorter?

This is my code for true on everything but empty string, null and false: if (routeinfo["no_route"] == "" || routeinfo["no_route"] == null || routeinfo["no_route"] == false) { // do sth ... } This is my code for true on everything but empty…
Blackbam
  • 17,496
  • 26
  • 97
  • 150
98
votes
7 answers

Check if year is leap year in javascript

function leapYear(year){ var result; year = parseInt(document.getElementById("isYear").value); if (years/400){ result = true } else if(years/100){ result = false } else if(years/4){ result= true } …
BigBob
  • 1,015
  • 1
  • 8
  • 9
94
votes
10 answers

Swift inline conditional?

How do I do this in Swift ? (someboolexpression ? "Return value 1" : "Return value 2") (no I have not read the whole manual yet... I probably missed it on page 2!) OK so its on page 91 and the above appears to be correct. However I am trying to…
Duncan Groenewald
  • 8,496
  • 6
  • 41
  • 76
94
votes
3 answers

Ruby - Access multidimensional hash and avoid access nil object

Possible Duplicate: Ruby: Nils in an IF statement Is there a clean way to avoid calling a method on nil in a nested params hash? Let's say I try to access a hash like this: my_hash['key1']['key2']['key3'] This is nice if key1, key2 and key3…
Nobita
  • 23,519
  • 11
  • 58
  • 87
93
votes
4 answers

How do use a Switch Case Statement in Dart

I am trying to understand how the switch is working in the dart. I have very simple code: methodname(num radians) { switch (radians) { case 0: // do something break; case PI: // do something else break; } } This…
Peter StJ
  • 2,297
  • 3
  • 21
  • 29
93
votes
12 answers

Why would you use an assignment in a condition?

In many languages, assignments are legal in conditions. I never understood the reason behind this. Why would you write: if (var1 = var2) { ... } instead of: var1 = var2; if (var1) { ... } ?
lajos
  • 25,525
  • 19
  • 65
  • 75
89
votes
6 answers

C# conditional AND (&&) OR (||) precedence

We get into unnecessary coding arguments at my work all-the-time. Today I asked if conditional AND (&&) or OR (||) had higher precedence. One of my coworkers insisted that they had the same precedence, I had doubts, so I looked it up. According to…
87
votes
1 answer

Project reference conditional include with multiple conditions

Here's a snippet from my csproj file: {4F9034E0-B8E3-448E-8794-CF9B9A5E7D46}
Charlie Salts
  • 13,109
  • 7
  • 49
  • 78
86
votes
10 answers

Python conditional assignment operator

Does a Python equivalent to the Ruby ||= operator ("set the variable if the variable is not set") exist? Example in Ruby : variable_not_set ||= 'bla bla' variable_not_set == 'bla bla' variable_set = 'pi pi' variable_set ||= 'bla bla' …
Hartator
  • 5,029
  • 4
  • 43
  • 73
85
votes
1 answer

How to check if matching text is found in a string in Lua?

I need to make a conditional that is true if a particular matching text is found at least once in a string of text, e.g.: str = "This is some text containing the word tiger." if string.match(str, "tiger") then print ("The word tiger was…
Village
  • 22,513
  • 46
  • 122
  • 163
84
votes
6 answers

How do I use properly CASE..WHEN in MySQL

Here is a demo query, notice it is very simple, Fetches only where base_price is 0, And still, it chooses the condition 3: SELECT CASE course_enrollment_settings.base_price WHEN course_enrollment_settings.base_price = 0 THEN 1 WHEN…
Itay Moav -Malimovka
  • 52,579
  • 61
  • 190
  • 278
84
votes
2 answers

How do I use regular expressions in bash scripts?

I want to check if a variable has a valid year using a regular expression. Reading the bash manual I understand I could use the operator =~ Looking at the example below, I would expect to see "not OK" but I see "OK". What am I doing…
idrosid
  • 7,983
  • 5
  • 44
  • 41
84
votes
5 answers

Ruby ternary operator without else

Is there a ruby idiom for "If do-this," and "do-this" just as a simple command? for example, I'm currently doing object.method ? a.action : nil to leave the else clause empty, but I feel like there's probably a more idiomatic way of doing this…
84
votes
8 answers

Conditional Count on a field

If I had a table like this: jobId, jobName, Priority Whereby Priority can be an integer between 1 to 5. Since I would need this query for generating a chart on report, I would need to display the jobid, jobname and 5 fields called Priority1,…
Houman
  • 64,245
  • 87
  • 278
  • 460
83
votes
2 answers

Makefile : contains string

A variable returns MINGW32_NT-5.1 or CYGWIN_NT-5.1. (yea, dot at the end) Need to compare that given var contains NT-5.1 positioned anywhere. Using cygwin and would like to be compatible with pretty much any *nix.
Pablo
  • 28,133
  • 34
  • 125
  • 215