Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Conditional (computer programming)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Else if=== By using <code>else if</code>, it is possible to combine several conditions. Only the statements following the first condition that is found to be true will be executed. All other statements will be skipped. '''if''' condition '''then''' ''-- statements'' '''elseif''' condition '''then''' ''-- more statements'' '''elseif''' condition '''then''' ''-- more statements;'' ... '''else''' ''-- other statements;'' '''end if'''; For example, for a shop offering as much as a 30% discount for an item: '''if''' discount < 11% '''then''' print (you have to pay $30) '''elseif''' discount<21% '''then''' print (you have to pay $20) '''elseif''' discount<31% '''then''' print (you have to pay $10) '''end if'''; In the example above, if the discount is 10%, then the first if statement will be evaluated as true and "you have to pay $30" will be printed out. All other statements below that first if statement will be skipped. The <code>elseif</code> statement, in the [[Ada (programming language)|Ada]] language for example, is simply [[syntactic sugar]] for <code>else</code> followed by <code>if</code>. In Ada, the difference is that only one <code>end if</code> is needed, if one uses <code>elseif</code> instead of <code>else</code> followed by <code>if</code>. [[PHP]] uses the <code>elseif</code> keyword<ref name="php_elseif">''[http://php.net/manual/control-structures.elseif.php PHP elseif syntax]''</ref> both for its curly brackets or colon syntaxes. [[Perl]] and [[Ruby (programming language)|Ruby]] provide the keyword <code>elsif</code> to avoid the large number of braces that would be required by multiple <code>if</code> and <code>else</code> statements. [[Python (programming language)|Python]] uses the special keyword <code>elif</code> because structure is denoted by indentation rather than braces, so a repeated use of <code>else</code> and <code>if</code> would require increased indentation after every condition. Some implementations of [[BASIC]], such as [[Visual Basic]],<ref name="vb_elseif">''[https://docs.microsoft.com/dotnet/visual-basic/language-reference/statements/if-then-else-statement Visual Basic ElseIf syntax]''</ref> use <code>ElseIf</code> too. Similarly, the earlier UNIX shells (later gathered up to the POSIX shell syntax<ref name="posixshell">''[http://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html POSIX standard shell syntax]''</ref>) use elif too, but giving the choice of delimiting with spaces, line breaks, or both. However, in many languages more directly descended from Algol, such as [[Simula (programming language)|Simula]], [[Pascal (programming language)|Pascal]], [[BCPL (programming language)|BCPL]] and [[C (programming language)|C]], this special syntax for the <code>else if</code> construct is not present, nor is it present in the many syntactical derivatives of C, such as [[Java (programming language)|Java]], [[ECMAScript]], and so on. This works because in these languages, any ''single'' statement (in this case <code>if ''cond''</code>...) can follow a conditional without being enclosed in a block. This design choice has a slight "cost". Each <code>else if</code> branch effectively adds an extra nesting level. This complicates the job for the compiler (or the people who write the compiler), because the compiler must analyse and implement arbitrarily long <code>else if</code> chains recursively. If all terms in the sequence of conditionals are testing the value of a single expression (e.g., <code>if x=0</code> ... <code>else if x=1</code> ... <code>else if x=2</code>...), an alternative is the [[switch statement]], also called case-statement or select-statement. Conversely, in languages that do not have a switch statement, these can be produced by a sequence of <code>else if</code> statements.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)