C#
  Home arrow C# arrow Page 4 - Branching and Looping in C#, Part 1
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

Branching and Looping in C#, Part 1
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 47
    2005-04-12

    Table of Contents:
  • Branching and Looping in C#, Part 1
  • No Implicit Conversion
  • Nesting
  • Combining else With if
  • The switch Statement
  • Looping Statements

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Branching and Looping in C#, Part 1 - Combining else With if


    (Page 4 of 6 )

     

    You can combine else with if in one statement to execute more than one test on the expression. As you already know, if tests the expression, and if it evaluates to false, the else block will be executed. Sometimes this is not what we need, however. We may need to be very specific with the else part. For example, take a look at the following code:

    int x = 5;
    if(x != 0)
    {
      Console.WriteLine("x != 0");
    }
    else
    {
      Console.WriteLine("x = 0");
    }

    There is nothing special about this code; but notice that with the if statement we test to see if the x is not equal to zero, and if so, it will execute the if block, and else it will execute the else block. Now let's look at the following code:

    using System;
    namespace MyCompany
    {
      public class IfStatement
      {
        static void Main(string[] args)
        {
          int x = 5;
          if(x == 0)
          { 
            Console.WriteLine("x != 0"); 
          }
          else if(x == 4)
          {
            Console.WriteLine("x == 4"); 
          } 
          else if(x == 5)
          {
            Console.WriteLine("x == 5");
          }
          else
          {
            Console.WriteLine("x = 0"); 
          }
          Console.ReadLine(); 
        } 
      }
    }
     

    if you compile and run the application, you will get the following in the console window:

    C# features the combination of else with if to further test the Boolean expression (only when the if statement evaluates to false), so if the if Boolean expression evaluated to true, the else if statements will never be tested. I will rewrite the above code as follows:

    using System;
    namespace MyCompany 
    {
      public class IfStatement
      {
        static void Main(string[] args)
        { 
          int x = 5;
          if(x != 0)
          {
            Console.WriteLine("x != 0");
          }
          else if(x == 4)
          {
            Console.WriteLine("x == 4");
          }
          else if(x == 5)
          {
            Console.WriteLine("x == 5");
          }
          else
          {
            Console.WriteLine("x = 0");
          } 
          Console.ReadLine(); 
        }
      }
    }

    You will get the following result:

    This is an unexpected result, because x is assigned 5. It is true that x is assigned 5, but the code was not perfect; the if statement tested to check if x is not equal to 0, and it's not equal to zero, so it escaped  the next else if statement, and that's why we got this value. With this in mind, never use an expression that is misleading while using else if statements.

    More C# Articles
    More By Michael Youssef


     

    C# ARTICLES

    - C# and XML
    - Pointers and Arrays in C#
    - C# 3.0 Extension Methods
    - Overloading Operators in C#
    - Iterators and Nullable Types
    - Patterns and Iterators in C#
    - C# Exceptions
    - Methods in C#
    - Delegates and Events in C#
    - Advanced C#
    - Working with Regular Expressions in C#
    - Sending Simple E-Mail in C#
    - Building C# Comparable Objects: IComparable ...
    - Color Transformation Applications in C# GDI+...
    - Performing Color Transformation Operations i...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT