Conditional Statements

Posted on Wednesday, October 1, 2008

Conditional statements are used where your program must evaluate whether a logical statement is true or false. These include, but are not limited to:

IF-THEN-ELSE

IF-THEN-ELSE statements are used when your algorithm requires branching logic. Please note: an IF-THEN-ELSE statement is not a loop - the code does not repeat.

General form:

IF(CONDITION evaluates to TRUE) THEN
    DO SOMETHING
ELSE
    DO SOMETHING ELSE

Language-specific examples:

  • C/C++ / C# / Java
if(x == 1)
{
    // do something
}
else
{
    // do something else
}
  • VisualBasic.NET
If foo = "bar" Then
    ' do something
Else
    ' do something else
End If
  • Ruby / Python
if foo == 'bar'
    # do something
else
    # do something else
end

Loops are used when your algorithm requires that a portion of your process is repeated.

There are two general classes of loops: determinate, and indeterminate. An indeterminate loop has no set number of repetitions - it will repeat until an exit condition is met. A determinate loop repeats (at most) a fixed number of times. Both types of loops can be exited early, using the keyword(s) defined by the language being used to implement the loop.

WHILE, DO-WHILE, and DO-UNTIL loops

General form:

WHILE(CONDITION evaluates to TRUE)
    DO SOMETHING
LOOP

DO
    SOMETHING
WHILE(CONDITION evaluates to TRUE)

DO
    SOMETHING
UNTIL(CONDITION evaluates to TRUE)

Language-specific examples:

  • C/C++ / C# / Java
while(choice != 'x')
{
    // do something
}

do
{
    // do something
} while(choice != 'x')

do
{
    // do something
} while(choice == 'x')
  • VisualBasic.NET
While repeat = True
    ' do something
End While

Do While repeat = True
    ' do something
Loop

Do Until repeat = False
    ' do something 
Loop

FOR and FOREACH loops

General form:

FOR(ITERATOR; CONDITION; INCREMENTOR)
    DO SOMETHING
	
FOREACH(ELEMENT in COLLECTION)
    DO SOMETHING
  • C/C++
for(int i = 0; i < 10; i++)
{
    // do something
}
  • C#
int x = 10;
for(int i = 0; i <= x; i++)
{
    // do something
}

foreach (Control ctrl in this.Controls)
{
    // do something
}
  • Java
int x = 10;
for(int i = 0; i <= x; i++) {
    // do something
}

for (Control ctrl : this.Controls) { 
    // do something
}
  • VisualBasic.NET
For Each ctrl As Control In Me.MainForm.Controls
    ' do something
Next ctrl

SWITCH or SELECT CASE statements

A SWITCH or SELECT CASE statement is primarily a shorthand way of writing a series of IF / ELSE-IF / ELSE-IF / ELSE statements. As such, it, like an IF-THEN-ELSE statement, is a branching statement - it is not a loop.

General form:

SWITCH (COMPARITOR)
    CASE (VALUE1):
	    DO SOMETHING
		BREAK
    CASE (VALUE2):
	    DO SOMETHING
		BREAK
	CASE (DEFAULT VALUE):
	    DO SOMETHING
		BREAK
END SWITCH
  • C/C++ / C# / Java
switch(rdb.Name)
{
    case "button1":
	    // do something
		break;
		
	case "button2":
	    // do something
		break;
		
	default:
	    // do something
		break;
}
  • VisualBasic.NET
Select Case option
    Case 0
	    ' do something
	Case 1
	    ' do something
	Case Else
	    ' do something
End Select