Conditional Statements in C# .Net

Ratings:
(4)
Views:265
Banner-Img
  • Share this blog:

 

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

arrrowPlace two text boxes and a button

arrrow Click on button 1

arrrowCode 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

  Screenshot_19

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:-

Screenshot_20

Color dialog is a predefined class

Application is a class which contains Exit () method

Purpose of exit () is to terminate the project

You liked the article?

Like : 0

Vote for difficulty

Current difficulty (Avg): Medium

Recommended Courses

1/15

About Author
Authorlogo
Name
TekSlate
Author Bio

TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.


Stay Updated


Get stories of change makers and innovators from the startup ecosystem in your inbox