If...Else in C# and VFP

C#

If, Else

VFP

If, (Then), Else, Endif

C# Syntax Notes

C# uses the same decision structure as all the other languages based on C. The keyword if is followed by a boolean expression in brackets () and then by the code to be executed if this expression evaluates as true. This can be a simple statement or a block of statements in braces {}. Optionally this can be followed by the keyword else and then by the code to be executed if the statement evaluates as false.

if(a==b)
  {
   // The test is true
   Console.Write("a equals b");
  }
else
  {
   // The test is false
   Console.Write("a does not equal b");
  }

The structure can be extended by adding an if to the else clause to switch between multiple options:

if(a>b)
  {
   Console.Write("a greater than b");
  }
else if(a<b)
  {
   Console.Write("a less than b");
  }
else
  {
   Console.Write("a equals b");
  }

VFP Syntax Notes

Visual FoxPro follows the same pattern as Visual Basic in that the block of code controlled by the if and the optional else are identified by being on separate lines and the endif statement closes the whole structure. In FoxPro the then is optional:

if(a=b) then
   * The test is true
   ? "a equals b"
else
   * The test is false
   ? "a does not equal b"
endif

This structure cannot be condensed into fewer lines. The if, else and endif keywords and the code all have to be on separate lines.

You can't chain if statements together in FoxPro but there is no need for this feature because each case in a select case can have its own independent test.

Warning to programmers from VB or FoxPro

Note the double equals sign in the C#comparison expression. Visual Basic and FoxPro both use the equals sign to mean two different things. In either language it can mean either "assign a value" or it can mean "compare these values". C# is different. It uses the same convention as in C and Java and the only meaning of a single equals sign is to assign a value. You have to use a double equals in a comparison in C sharp.

Procedures  |  Language index  |  Case

Related Items

Site Map - C Sharp

Information about C Sharp

Read More

Site Map

The Alvechurch Data site map.

Read More

VFP and C#.

C# is very different from Visual FoxPro so conversion is difficult.

Read More

C# and VFP comments

Adding comments to programs in C# and Visual FoxPro.

Read More

C# and VFP constants

Declaring constants in C# and Visual FoxPro.

Read More