C#
  Home arrow C# arrow Page 3 - 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 - Nesting


    (Page 3 of 6 )

    Of course you can nest if statements as much as you want. Let's take a look:

    using System;
    namespace MyCompany 
    {
      public class IfStatement
      {
        static void Main(string[] args)
        {
          Console.WriteLine("Please type a number");
          int x = Convert.ToInt32(Console.ReadLine());
          if(x % 2 == 0)
          {
            if(x < 1000)
            {
              Console.WriteLine("this is an EVEN number");
            }
            else 
            {
              Console.WriteLine("this is a big EVEN number");
            } 
          }
          else
          {
            if(x < 1000)
            {
              Console.WriteLine("this is an ODD number");
            }
            else
            {
              Console.WriteLine("this is a big ODD number");
            } 
          }
          Console.ReadLine(); 
        }
      }
    }

    As you can see, I have added nested if statements to test if the number is greater than or less than 1000, and I print to the console a line in each case. One thing to note about VS.NET is its indentation scheme:

    You can tell that there's only one top level if statement associated with an else statement, and each statement has a nested if statement, too. VS.NET will indent if statements automatically.

    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 1 hosted by Hostway
    Stay green...Green IT