If / ElseIf Statements:

A web programmer has different options for checking a condition and depending on the desired test will use either an "if statement" or an "if/else statement" or an "if/elseif" statement. This page gives the syntax for the "if/elseif statement" for both VB.NET and C#. Review the code below and view the interactive example.

Check 2 or more conditions:

If a web programmer needs to check several conditions and give a different response based on the user's interaction an "if / else if statement" can be used.

VB.NET code for the If / Else Statement:


       If intInput = 1 Then
            lblMsg.text ="Thank you."
        ElseIf intInput = 2 Then
            lblMsg.text ="That's fine."
        ElseIf intInput = 3 Then
            lblMsg.text ="Too big."
         Else
           lblMsg.text ="Not a number I know."
       End If
 

Below is the exact code as above only in C# instead of VB.NET.

C# code for the If / Else Statement:


          
{ 
    if (intInput == 1) 
    { 
        lblMsg.text = "Thank you."; 
    } 
    else if (intInput == 2) 
    { 
        lblMsg.text = "That is fine."; 
    } 
    else if (intInput == 3) 
    { 
       lblMsg.text = "Wow!"; 
    } 
    else { 
        lblMsg.text = "Sorry. Not understood"; 
    } 
} 
      





VB.NET vs. C#

Variables

Decisions

Built In Functions