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
1 answer

Equivalence of IF and WHERE

We all know that the DO loop is more powerful than the FORALL statement in Fortran. That is, you can always substitute a FORALL by a DO, but not vice versa. What about the WHERE statement and block? Can I always substitute the IF by a WHERE? Is it…
6
votes
4 answers

How to Select all codes between Curly Braces in Notepad++?

In Notepad++ How to select all (Highlight) codes between Curly braces (Start & End)? For eg. If-else, Method definition, While, For loop etc. I remember similar option is available with Adobe Dreamweaver where we can perform this CTRL ' (Holding the…
Jenson M John
  • 4,972
  • 4
  • 25
  • 45
6
votes
3 answers

Basic JavaScript if vs. else if

My question is this: I'm new to JavaScript, I am trying to understand the difference between the " if { " and " else if { " statements. Thus far the only answers I have found are related to someone inheriting my code later, obviously no one is ever…
MJ Hdesigner
  • 77
  • 1
  • 4
6
votes
6 answers

Declaring a variable in an if-else block in C++

I'm trying to declare a variable in an if-else block as follows: int main(int argc, char *argv[]) { if (argv[3] == string("simple")) { Player & player = *get_Simple(); } else if (argv[3] == string("counting")) { Player &…
Ian Burris
  • 5,779
  • 17
  • 55
  • 80
6
votes
3 answers

How to break out of an if statement from a boolean inside the if statement

I have something like this bool a = true; bool b = true; bool plot = true; if(plot) { if(a) { if(b) b = false; else b = true; //do some meaningful stuff here } //some more stuff here that needs…
Chris
  • 651
  • 1
  • 6
  • 15
6
votes
4 answers

How to use conditions within an anonymous function

A function can be defined as @(x) x^.2 (for e.g) But in case, we have a function that takes different representation on different intervals for e.g : if abs(x)<3 fun = x^.2 else 0 How can we use the same way (i mean use @(x) ) to define such a…
Jessica
  • 71
  • 1
  • 1
  • 2
6
votes
6 answers

How do I simplify an IF statement that returns true or false?

public bool CheckStuck(Paddle PaddleA) { if (PaddleA.Bounds.IntersectsWith(this.Bounds)) return true; else return false; } I feel like the above code, within the procedure, is a bit redundant and…
Shivam Malhotra
  • 289
  • 1
  • 3
  • 10
6
votes
3 answers

How to check if a service is running via batch file and stop it if it is not running?

I want a batch file to check if the service "MyServiceName" is running. If the service is running, I want the batch file to disable it and then display a message. If it isn't running, and is disabled, I want the batch file to display a message and…
Gary Allen
  • 557
  • 1
  • 4
  • 11
6
votes
2 answers

Add if statement to do-end block

I've rewritten my html code with rails content_tags and now I want to add if-statements to them. Before I had: <% if ... %>
...
<% end %> Now I have 2 types of this block: <%= content_tag(:div, ..., class:…
Peter Tretyakov
  • 3,094
  • 6
  • 33
  • 46
6
votes
6 answers

C# Execution speed: equal (==) vs not equal (!=)

I'm trying to figure out some best-practices for if-statements. When I need to switch on some equality, i.e. need an if-else construct, I usually write condition on "not-equal". The reason behind this is that usually, when a non-successful result…
Andrei V
  • 6,788
  • 6
  • 38
  • 58
6
votes
4 answers

Which is faster - if..else or Select..case?

I have three condition to compare. Which one is more faster between the following two? Please point me out. Thanks all! If var = 1 then Command for updating database ElseIf var = 2 then Command for updating database ElseIf var = 3 then …
RedsDevils
  • 1,335
  • 9
  • 24
  • 46
6
votes
2 answers

IF ELSE in excel -- check if a cell is null/empty

I am not programming in VBA. This is a simple excel spreadsheet. Essentially, I have a formula the finds the lowest price in a range of cells and returns its respective column title. It works fine. However, some ranges do not have any values at all.…
sherrellbc
  • 4,222
  • 7
  • 37
  • 71
6
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}
agpt
  • 4,887
  • 9
  • 45
  • 85
6
votes
5 answers

T-SQL if statement error

i'm trying to execute the following SQL 2008 code it says there is a problem near "=" and "else"... i cant understand what is wrong with the code ALTER PROCEDURE dbo.LoginEmp @username NVARCHAR(10), @password…
Monir Tarabishi
  • 688
  • 1
  • 17
  • 28
6
votes
3 answers

Shell Scripting: RegEx in if Statement

I can't seem to figure out how to write a regex correctly in an if statement. I wanted it to print out all the lines with "End Date" in it. NUMBERS contains a text file with the following content: End Date ABC ABC ABC ABC ABC ABC 05/15/13 2 …
dalawh
  • 759
  • 8
  • 12
  • 31
1 2 3
99
100