code samples wiki

if else for foreach while

If Else Conditional Programming Examples

If ... else examples and documentation links in different programming languages. The examples cover a blackjack scenario and provide deep links to the reference documentation of the programming languages. See wiki article Conditional Programming for general information about conditional statements. The if ... else syntax is also known as "if statement", "conditional statements", "compound statements", "if else structure" or "conditional constructs".


Programming languages

XSL/XSLT Java JSP PHP Perl Python C, C++, Objective-C C# Bash Pascal JavaScript Excel Ruby Fortran Velocity ASM Basic Tcl Windows PowerShell Lotus Notes Formula Gentics Portal.Node Expressions Whitespace

Missing something or found an error? Please contribute by editing and fixing errors or leave a comment

Bookmark and Share

XSL/XSLT edit

<xsl:choose>
	<xsl:when test="card > 21">busted</xsl:when>
	<xsl:when test="card = 21">won</xsl:when>
	<xsl:otherwise>continue</xsl:otherwise>
</xsl:choose>

W3C XSL Transformations (XSLT) specification, conditional processing

Java edit

if (card > 21) {
	System.out.println("busted");
} else if (card == 21) {
	System.out.println("won");
} else {
	System.out.println("continue");
}

The Java Language Specification, Third Edition, Blocks and Statements

JSP edit

<c:choose>
	<c:when test='${card > 21}'>busted</c:when>
	<c:when test='${card == 21}'>won</c:when>
	<c:otherwise>continue</c:otherwise>
</c:choose>

J2EE 1.4 Tutorial, Core Tag Library

PHP edit

if ($card > 21) {
	echo "busted";
} else if ($card == 21) {
	echo "won";
} else {
	echo "continue";
}

PHP Manual, Control Structures

Perl edit

if ($card > 21) {
	print "busted";
} elsif ($card == 21) {
	print "won";
} else {
	print "continue";
}

Perl language reference, Compound Statements

Python edit

if card > 21:
	print "busted"
elif card == 21:
	print "won"
else:
	print "continue"

The Python Language Reference, Compound Statements

C, C++, Objective-C edit

if (card > 21) {
	printf("busted");
} else if (card == 21) {
	printf("won");
} else {
	printf("continue");
}

ISO/IEC 9899, Programming languages - C, 6.8.4 Selection statements

C# edit

if (card > 21) {
	Console.WriteLine("busted");
} else if (card == 21) {
	Concole.WriteLine("won");
} else {
	Console.WriteLine("continue");
}

C# Language Reference, if-else

Bash edit

if test "$card" -gt "21"; then
	echo "busted"
elif test "$card" -eq "21"; then
	echo "won"
else
	echo "continue"
fi

Bash Reference Manual, Conditional Constructs

Pascal edit

if card > 21 then
	writeln("busted");
else if card = 21 then
	writeln("won");
else
	writeln("continue");

Free Pascal Reference Guide, The If..then..else statement

JavaScript edit

if (card > 21) {
	document.write("busted");
} else if (card == 21) {
	document.write("won");
} else {
	document.write("continue");
}

ECMAScript Language Specification (ECMA-262), 12.5 The if Statement

Excel edit

=IF(
A1>21;
"busted";
IF(A1=21;"won";"continue")
)

Excel 2003 Help, Function Reference, Logical Functions, IF

Ruby edit

if card > 21
	print "busted"
elsif card == 21
	print "won"
else
	print "continue"
end

Programming Ruby, The Pragmatic Programmer's Guide, Conditional Execution

Fortran edit

if (card > 21) then
	print*, "busted"
else if (card == 21) then
	print *, "won"
else
	print *, "continue"
end if

Fortran 77 Standard, 11 CONTROL STATEMENTS

Velocity edit

#if( $card > 21 )
busted
#elseif( $card == 21 )
won
#else
continue
#end

VTL Reference, Directives

ASM edit

cmp dword [card], 21
je @f
jl @continue
;; busted
jmp @out
@@:
;; won
jmp @out
@continue:
;; continue
@out:

Basic edit

If Card > 21 Then
	Print "busted"
Else If Card = 21 Then
	Print "won"
Else
	Print "continue"
End If

Visual Basic Reference, Statements

Tcl edit

if {$card > 21} {
	puts stdout "busted"
} elseif {$card == 21} {
	puts stdout "won"
} else {
	puts stdout "continue"
}

Tcl Documentation, if

Windows PowerShell edit

if ($card -gt 21) 
    {"busted"} 
elseif ($card -eq 21) 
    {"won"} 
else 
    {"continue"}

Windows PowerShell Quick Reference

Lotus Notes Formula edit

@if (card>21; "busted"; card=21; "won"; "continue")

Formula Poster, Control Logic

Gentics Portal.Node Expressions edit

if (card > 21, 
	echo("busted"), 
	if (card == 21, 
		echo("won"), 
		echo("continue")
	)
)

Gentics Portal.Node Reference, Expression Parser Functions

Whitespace edit

please contribute