Conditional Statements
Syntax of if condition :
Syntax 1
If (condition)
Statement 1 ;
Else Statement 2;
Syntax 2
If (condition)
{
Statement 1 ;
Statement 2;
}
Else
{
Statement 3;
Statement 4;
}
Nested If:-
If (condition)
Statement 1 ;
Else If (condition)
Statement 1 ;
Else If (condition)
Statement 1 ;
Else
.
Else Statement;
These core tutorials will help you to learn the fundamentals of .NET. For an in-depth understanding and practical experience, explore Online ".NET Training"
Examples of if statement
A program to compare two numbers (I and j), if I is greater change from back color to green otherwise REO.
Open windows form application project
Place two text boxes and a button
Click on button 1
Code for button 1
Int I = int. parse (text Box 1. text);Int j =int. parse (text Box2.text);
If (i> j)
This. Back color = color. Green;
Else
This. Back color = color. Red;
Execute the project
Observations
What is this key?
this is a keyword
this works like an object for the current class
this will be created at run time.
this is under the control of CLR.
Working with Switch condition Switch syntax
Switch (variable){
Case 1: Statements;
Break;
Case 2: Statements;
Break;
. Default Statements;
Break;
}
Example of a Switch condition
Open windows form application project Place a text box and a button Code for button 1 click
{Int I = int. parse (text box 1. text);
Switch (i) { …. }
//switched close here
Switch (i)
{
Case 1: This. back color = color. Green ;
Break;
Case 2 : Color dialog c = new dialog ();
Creating an object
show dialog ();
this. Back color = c. color;
break;
case 3: Application . Exit ();
Break;
Default : Message box . show (“enter 1,2, or 3 only”);
Break;
}
// switch closed here Execute the project
Observations
Syntax to create an object Class name (space) obj _name = new class name ();
EX:-
Color dialog is a predefined class
Application is a class which contains Exit () method
Purpose of exit () is to terminate the project