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:

52698 questions
6
votes
3 answers

Replacing If Statements?

I am trying to make my code more efficient and replace a bunch of if statements that I wrote. My program so far basically checks what operator (such as +, -, etc) is entered and then computes it. For example 1 + 5 gives 6. When the program evaluates…
Thomas Paine
  • 303
  • 3
  • 12
6
votes
3 answers

If Else-if in Robot Framework

I want get value from a Keyword by using else if. Example: String text = "" If variable > 5 text = "one"; else if variable <5 text = "two"; else text = "three"; In Robot Framework I use the code ${txt} Set Variable ${txt}= Run…
6
votes
6 answers

Simple if else onclick then do?

How do I make it so if yes button clicked change colour? Is using .onclick the best option for this? Am I doing it the optimal way? Thanks. html:
Markus Hallcyon
  • 341
  • 2
  • 5
  • 15
6
votes
7 answers

In an If-Else Statement for a method return, should an Else be explicitly stated if it can instead be implicitly followed?

I have a method that checks certain things and returns a Boolean based on those checks. It involves a single branching If section that checks about 5 conditions in sequence. If any of those conditions return true, then the method will return true;.…
Grace Note
  • 3,125
  • 4
  • 33
  • 54
6
votes
2 answers

Easy way to check if item is in list?

I'm writing a search algorithm in C++, and one of the things I need to do is have a few if statements that check cells above, below, left of, and right of. Each time a cell is found to be open and added to the stack, I want it added to a list of…
Befall
  • 5,824
  • 8
  • 22
  • 28
6
votes
5 answers

IF implementations in C#

The majority of developers write IF statements in the following way if (condition) { //Do something here } of course this considered normal but often can create nested code which is not so elegant and a bit ugly. So the question is: Is possible…
CodeArtist
  • 4,844
  • 8
  • 36
  • 59
6
votes
7 answers

How to check for a device language in android?

I want my application to check which language the phone is using. This if statement is supposed to do that: if (Locale.getDefault().getLanguage().equals("en")) { yourYesResponse = "That is great " + usersName + "!"; } else if…
Mishil D.
  • 107
  • 2
  • 8
6
votes
4 answers

C programming - Loop until user inputs number scanf

I need help with error checking for my program. I'm asking the user to input a integer and I would like to check if the users input is a integer. If not, repeat the scanf. My code: int main(void){ int number1, number2; int sum; //asks user…
Asia x3
  • 586
  • 1
  • 14
  • 33
6
votes
3 answers

python if statement too long and ugly, is there a way to shorten it

i have this long and ugly else if statement in python, is there a way to condense it into a shorter block of code maybe on one line or two if possible because it seems like there would be a way of shortening a piece of code like this if p == "A": y…
6
votes
7 answers

Continue script if only one instance is running?

now this is embarrassing. I'm writing quick script and I can't figure out why this statement don't work. if [ $(pidof -x test.sh | wc -w) -eq 1 ]; then echo Passed; fi I also tried using back-ticks instead of $() but it still wouldn't work. Can…
Gargauth
  • 2,115
  • 6
  • 24
  • 28
6
votes
3 answers

Using ifelse() to replace NAs in one data frame by referencing another data frame of different length

I already reviewed the following two posts and think they might answer my question, although I'm struggling to see how: 1) Conditional replacement of values in a data.frame 2) Creating a function to replace NAs from one data.frame with values from…
Daniel Fletcher
  • 1,045
  • 2
  • 12
  • 22
6
votes
5 answers

C: Convert A ? B : C into if (A) B else C

I was looking for a tool that can convert C code expressions for the form: a = (A) ? B : C; into the 'default' syntax with if/else statements: if (A) a = B else a = C Does someone know a tool that's capable to do such a transformation? I work…
tur1ng
  • 2,696
  • 4
  • 21
  • 31
6
votes
3 answers

Python - simplify repeated if statements

I am very new to python and looking for a way to simplify the following: if atotal == ainitial: print: "The population of A has not changed" if btotal == binitial: print: "The population of B has not changed" if ctotal == cinitial: …
6
votes
1 answer

Batch - Compare variable with regular expression

I'm doing a batch script that has to check if there are some programs installed on the computer. For that, I execute programName --version and I store the output in a variable. The problem is when I try to compare with a regular expression (only to…
jgarciabt
  • 521
  • 5
  • 19
6
votes
5 answers

Quick alternative to lots of if statements

I'm beginner at java, and I'm making a simple program where I type in something, and if what I type in matches one of the things on the "database" then it'll print some text. Is there a simpler way to check this rather than doing this: int 1; int…
user3532547
  • 87
  • 1
  • 9