Questions tagged [if-statement]

An "if" statement is a flow control structure in most programming languages that branches execution flow depending on a binary condition, generally evaluated at runtime. If statements are also commonly also called conditionals. When using this tag please also include an appropriate language tag, such as e.g. "java" if your question is language-specific.

An if statement is a flow control structure in most programming languages that branches execution flow depending on a binary condition, generally evaluated at runtime. If-statements are also commonly known as conditionals.

When using this tag please also include an appropriate language tag, such as e.g. if your question is language-specific.


Basic Syntax

The if statement has the following syntax:

if <condition>
then
     <statement-1>
else
     <statement-2>

<condition> may be parenthesized (as it is in JavaScript), the keyword then may be omitted (Python, C-like languages, JavaScript and others).

The else section is optional in most languages.

An example if statement in JavaScript:

var myVariable = 100;

if (myVariable >= 20) {
    console.log('My variable is greater than or equal to 20!');
} else {
    console.log('My variable is less than 20!');
}

if-else statements may also be nested, where another if may appear in if statement, and/or in else statement. For example:

if ( number1 > 20 )
   if ( number2 > 50 )
      print('Both numbers satisfy condition')
   else
      print('Second number doesn't satisfy condition')
else
   if( number2 > 50 )
      print('Only Second number satisfies condition')
   else
      print('None of the two numbers satisfy condition')

else+if is used to chain if statements:

if ( number > 20 )
     print('Greater than 20')
else+if ( number > 10 )
     print('Greater than 10')
else
     print('Less than 11')

else+if statements may simply be an else statement followed by an if (e.g else if; done in JavaScript and many C-like languages), or a special keyword such as elif (Python), or elsif (Perl).


As a ternary operator

In C and C-like languages, conditional expressions can take the form of a ternary operator called the conditional expression operator, ?:, which follows this template:

(condition)?(evaluate if condition was true):(evaluate if condition was false)

In Python, if is used explicitly, and the ordering is slightly different:

(evaluate if condition was true) if (condition) else (evaluate if condition was false)

An example of the ternary operator in JavaScript:

var myVariable = 100;

myVariable>20 ? console.log('Greater than 20!') : console.log('Less than or equal to 20!');

See also:

61703 questions
7
votes
4 answers

C# if else, switch, bool

Im trying to code a simple text adventure. when I started to code the directions a user can head in I realized the user could put in "north east", "south west" etc so I thought I should make cases for them. my problem is case5 "north east" does not…
Charlie
  • 83
  • 1
  • 2
  • 6
7
votes
2 answers

How to select based on different column data

I want to perform a different SELECT based on the column data. For example I have a table http://sqlfiddle.com/#!2/093a2 where I want compare start_date and end_date only if use_schedule = 1. Otherwise select all data. (A different select) Basically…
NBhatti
  • 297
  • 1
  • 3
  • 12
7
votes
2 answers

What is the point of these if(0) conditionals?

I was looking through some of the CFArray code after finding out it was open source and I found some, to me, strange code. What is the point of these "empty" if (0) conditionals? Is there some crazy benefit or is this just left over from something?…
Halnry
  • 418
  • 2
  • 10
7
votes
5 answers

Evaluating multiple variable together in if condition

I was wondering whether its possible in java to evaluate multiple variables together in if-else condition like in python. actual code if(abc!=null && xyz!=null) {//...} dummy code if(abc && xyz !=null) {// will it be possible}
Aman Gupta
  • 5,548
  • 10
  • 52
  • 88
7
votes
5 answers

If value is greater/lesser than xyz

I have a value as a number. For instance, 502. I want to write a php if statement that will display some text if the value is lesser or greater than certain numbers, or between a range. E.g. number is 502, text will say: "Between 500-600" number is…
Seth-77
  • 254
  • 2
  • 4
  • 15
7
votes
11 answers

alternative to if statement in java

I am curious to see any alternative(s) to the regular if statements such as if(x) do a; if(y) do b; if(z) do c; so as you see all if statements are seperate and no else condition. Please notice that X Y Z are totally seperate…
Hellnar
  • 62,315
  • 79
  • 204
  • 279
7
votes
2 answers

SQL SELECT If (a="") Then change b value

This seems like a simple question, but I haven't been able to find an answer. I am basically trying to do this: SELECT * FROM table1 IF(columnA > 0) BEGIN columnB = 'Greater than 0' END I don't want the value to change in the table, I just want it…
Tyler Mortensen
  • 451
  • 3
  • 8
  • 19
7
votes
3 answers

How to run multiple expressions when an if condition is true?

I'm new to Scheme and am trying to have an if-statement perform more than one action if its condition is true. I tried something like: (if (char=? (string-ref str loc) #\a) ((+ count 1) (+ reference 1)) ~else action, etc.~ And it…
V1rtua1An0ma1y
  • 597
  • 2
  • 10
  • 16
7
votes
1 answer

What is the correct way to use the bash if else && || shortcut?

I have come across some strange behavious (well probably not strange but I don't understand it !) I want to write some if / e,se statements using the bash shorthand syntax: [[ 1 -eq 1 ]] && echo "true" || echo "false" The output of the above code…
ilium007
  • 539
  • 3
  • 8
  • 17
7
votes
5 answers

python - Simulating 'else' in dictionary switch statements

I'm working on a project which used a load of If, Elif, Elif, ...Else structures, which I later changed for switch-like statements, as shown here and here. How would I go about adding a general "Hey, that option doesn't exist" case similar to an…
Joshua Merriman
  • 925
  • 1
  • 8
  • 13
7
votes
4 answers

Nested if else statements over a number of columns

I have a large data.frame where the first three columns contain information about a marker. The remaining columns are of numeric type for that marker in each individual. Each individual has three columns. The dataset looks as follows: …
user1967407
  • 175
  • 1
  • 7
7
votes
5 answers

Javascript time passed since timestamp

I am trying to "cache" some information by storing it in a variable. If 2 minutes have passed I want to get the "live" values (call the url). If 2 minutes have not passed I want to get the data from the variable. What I basicly want is: if(time…
Parrotmaster
  • 647
  • 1
  • 7
  • 27
7
votes
1 answer

MATLAB: How do I check if any element in my matrix is nan and do something if that is the case

I know I can use isnan to check for individual elements, such as for i=1:m for j=1:n if isnan(A(i,j)) do something end end end However, instead what I want to do is if any(isnan(A)) do something end When…
Niseonna
  • 179
  • 2
  • 3
  • 13
7
votes
2 answers

Nullable type with inline if cannot work together?

Given the following code: Dim widthStr As String = Nothing This works - width is assigned Nothing: Dim width As Nullable(Of Double) If widthStr Is Nothing Then width = Nothing Else width = CDbl(widthStr) End If But this does not - width…
Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151
7
votes
3 answers

If-statement with && operator checks for 2nd value?

Does an if-statement with an && operator check for the second parameter if the first one is false / NO? Would the following be able to crash? NSDictionary *someRemoteData = [rawJson valueForKey:@"data"]; if( [someRemoteData…
Roland Keesom
  • 8,180
  • 5
  • 45
  • 52