VisualBuilder
  Home > Csharp > Tutorials > Control Statement: Selection Statement in C sharp - C# Tutorial
Tell a friend
Link to us
Total Members
      Members: 84606
     
Sitemap Forum Chat
Home
C# Tutorial Home
1 . Introduction to C sharp
2 . Control Statement: Selection Statement in C sharp
3 . Control Statements – Loops Statement
4 . Methods
5 . Namespaces
6 . Introduction to Classes:
7 . Inheritance:
8 . Polymorphism
9 . Properties
10 . Indexers
11 . Structs
12 . Interfaces
13 . Delegates:
14 . Exception Handling:
15 . Attributes
16 . Enums
17 . Encapsulation
18 . Parameter Passing in C sharp
19 . Method Overloading
20 . Database Interaction Using C sharp
21 . Operator Overloading in C sharp -1
22 . Operator Overloading in C sharp -2
23 . Operator Overloading in C sharp -2
24 . Sockets
25 . DNS [Domain Name System]
26 . Working with Files
27 . Generating Help File in C sharp
28 . Code Access Security
29 . Multi-Threading
30 . Globalization and Localization -1
31 . Working with Registry in C sharp
32 . Globalization and Localization -2
33 . Windows Service
34 . Web Service
35 . Consuming Web Services
36 . Creating Proxy Object of Web Service
37 . Creating an XML document
38 . Reading XML document in C sharp
39 . Using XMLWriter class to Write XML document in C sharp
40 . Assembly Information : Getting Permission set of the assembly
41 . Creating your own Permission Set
42 . Using C sharp Socket
 
Csharp Group Home
Csharp Discussion (7)
Csharp Members (2250)
Csharp Resources
Csharp Source Code (35)
Csharp Articles (0)
Csharp Blogs
Csharp Jobs
Csharp Components (5)
Csharp Books
Csharp Websites (25)
Csharp News (17)
Csharp Q & A (4)
- Csharp Ask Question
- Csharp Questions
- Csharp Unanswered Questions
 
GROUPS
.NET
ASP.NET
.NET
C#
ASP
Visual Basic
Java
Java
JSP
EJB
Other
Delphi
C++
Ajax
UML
JavaScript
PHP
Web Design
Web Hosting
SQL Server
Oracle
Project Management
More Groups

 
LEARNING CENTER
TUTORIALS
.NET
.NET Tutorial
ASP Tutorial
ASP.NET Database Tutorial
ASP.NET Development Tips
ASP.Net Security,Internationalisation And Deployment
ASP.NET Server Controls Tips
ASP.NET Tutorial
C Sharp Tutorial
Web Development
Flex Tutorial
HTML Tutorial
Learn AJAX Tutorial
PHP Tutorial
Software Development
Database Tutorial
SQL Tutorial
UML Tutorial
Java
Ant Tutorial
EJB 3 Tutorial
Hibernate Tutorial
Java Tutorial
Java Web Component Tutorial
Java XML Tutorial
JDBC Tutorial
JDK1.5 Tutorial
JSF Tutorial
JSP And J2EE Design Tutorial
JSP Tutorial
Spring Tutorial
Struts Tutorial

RESOURCES
Q & A (432 )
Source Code (3217 )
Articles (11 )
Components (1589 )
News (880 )
Websites (1207 )

SUBMISSIONS
Submit Article
Submit Website
Submit News
Submit Source Code
Submit Component

COMMUNITY
Members Directory
Discussion Forum
Chat

SITE
About Us
Sitemap
Search
Contact Us
Link To Us
Feedback
Tell a Friend
Partners
Advertise


Csharp Tutorial
 Control Statement: Selection Statement in C sharp
  << Prev: Introduction to C sharp Next: Control Statements – Loops Statement >>

Control statements are used to process some conditional code which only runs if the condition is true otherwise some alternate code may be processed. The following are two control statements used in C#.



  1. if-else statement

  2. switch statement


IF-ELSE Statement:


If Statement allows you to take different path depending on the decision condition. The condition must return boolean. When the condition evaluates to a boolean true, a block for that true condition will be executed.


 


Syntax:



  • If (condition) {
    // Single if statement
    //execute block if condition, returns true.}

  • If (condition) {
    // execute block if condition, return false
    } else {
    // execute block if condition, return false
    }

  • If (condition1) {
    // execute block if condition1 is true
    } else if (condition2) {
    // execute block if condition2 is true
    } else if (condition 3) {
    // execute block if condition3 is true
    } else {
    // otherwise if none condition satisfied then, this block will execute.
    }


 


Switch Statement:


Another selection statement is the switch statement, which executes a set of logic depending on the value of a given parameter that is evaluated by switch statement. The type of values a switch statement operates on can be Boolean, Enums, Integral Type and Strings.


 


Syntax:


Switch (Evaluated Value can be Boolean or Enums or integral type or strings) {


//switch with integer type


Case 1: // Block of statement


Break;


Case 2: // Block of statement


Break;


Case 3: // Block of statement


Break;


Case 4: // Block of statement


Break;


Default: // Block of statement


Break;


}


 


Note:- The switch block follows the switch expression, where one or more choices are evaluated for a possible match with the switch expression. Each choice is labeled with the case keyword, followed by an example that is of the same type as the switch expression and followed by a colon (:). When the result evaluated in the switch expression matches one of these choices, the statement immediately following the matching choice is executed. The block ends with either break, continue, goto, return or throw statement.


 


Example: Demonstrate Selection Statement




 


using System;


using System.Collections.Generic;


using System.ComponentModel;


using System.Data;


using System.Drawing;


using System.Text;


using System.Windows.Forms;


namespace cSHARPEXAMPLES


{


public partial class Form2 : Form


{


  public Form2()


    {


      InitializeComponent();


    }


  private void Form2_Load( object sender, EventArgs e)


    {


      Console.WriteLine( "FORM 2" );


    }


  private void button1_Click( object sender, EventArgs e)


    { // Here we can go through the example on IF


      int _value = Int32 .Parse(txtEnter.Text);


  if (_value > 100)


    {


    MessageBox .Show( "Value is greater than 100" );


        txtEnter.Text = "" ;


    } else if (_value < 100 && _value > 200){


    MessageBox .Show( "Value is greater than 100 but less than 200" );


        txtEnter.Text = "" ;


    } else {


    MessageBox .Show( "Value is less than 100" );


        txtEnter.Text = "" ;


        }


  }


  private void button2_Click( object sender, EventArgs e)


    { // Here we can go through the example on Switch


      int _value = Int32 .Parse(txtEnter.Text);


   switch (_value) // switch example with integer type


    {


    case 1:


    case 4:


    case 5: //Here the case 1 and 4 has no break, this will falls to this statement


      MessageBox .Show( "Value is 1 or 4 or 5" );


            txtEnter.Text = "" ;


      break ;


    case 2:


      MessageBox .Show( "Value is 2" ); txtEnter.Text = "" ;


      break ;


    case 3:


      MessageBox .Show( "Value is 3" ); txtEnter.Text = "" ;


      break ;


    default :


      MessageBox .Show( "Value is greater than 3" );


      break ;


            }


        }


    }


}


Output:



Click on: IF Example Button


This print value is less than 100


Click on: switch Example Button


This print value is greater than 3


  << Prev: Introduction to C sharp Next: Control Statements – Loops Statement >>
Csharp Tutorial Home
Give feedback and win a prize.

 
   Printer Friendly
   Email to a friend
   Add to my Favourites    
  Download PDF version
   Report Bad Submissions
   Submit Feedback
 
  Delicious   Digg   Technorati   Blink   Furl   Reddit   Newsvine   Google Click each image to add
this page to each site.
 
 
Welcome Guest Signup
MEMBER'S PANEL
EMAIL
PASSWORD
Forgot your password?
New User? Click Here!
 
Resend Activation Email!
 
SEARCH
 
 
LINKS
MSN
Video Surveillance
Skype vs. sipcall
Gift to Pakistan
 
ADVERTISEMENT
 
PARTNER LIST

More
 
 
 

Home | Login | About Us | Contact Us | Privacy Policy | Advertising