C#
  Home arrow C# arrow Branching and Looping in C#, Part 1
Iron Speed
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 
Dedicated Servers 
Actuate Whitepapers 
VeriSign Whitepapers 
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 / 46
    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

    At the virtual BlackBerry Technical Seminar 2008, you can ask your development questions directly of Research In Motion® (RIM) experts, and take advantage of learning opportunities designed uniquely for BlackBerry solution developers. Register Today!

    Branching and Looping in C#, Part 1


    (Page 1 of 6 )

    Certainly you are familiar with if, switch, while, return and goto statements. These are famous Branching and Looping statements in many programming languages for selecting code paths conditionally, looping and jumping to another part of your application unconditionally. In this article we will discuss what C# offers to us from these statements. As you will see, the common programming errors that existed in C++ were eliminated with C# statements.

    The if Statement

    The if statement gives you the ability to test an expression, then select a statement or a group of statements to be executed if the expression evaluates to true. If you have the else part of the statement, you can execute another statement (or statements) if the expression evaluates to false. The syntax of if statement is as follows:

    if(bool expression)
    statement 1;

    As we will see shortly, the expression must evaluate to bool value (true or false). If you have more than one statement, you must put the statements in a block using curly braces:

    if(bool expression)
    {
      statement 1;
      statement 2;
      statement 3;
    }

    The same applies to the else statement, so if you have only one statement to execute with the else statement, you can write it like this:

    if(bool expression)
      statement 1;
    else
      statement 1;

    But if you have multiple statements in your else part, you can write it as:

    if(bool expression)
    {
      statement 1;
      statement 2; 
      statement 3;
    }
    else
    {
      statement 1;
      statement 2;
      statement 3;
    }

    For consistency, use the curly braces even if you have only one statement with the if statement or with the else statement. Most programmers prefer this usage.

    More C# Articles
    More By Michael Youssef


     

    C# ARTICLES

    - Exceptions in C#
    - Overriding versus Overloading
    - Value Types and Reference Types
    - Defining Member and Type Visibility
    - Managing Files in C#
    - Working with Windows Registry in C#
    - Lossless Image Resizing in C#
    - Lossless Image Converting in C#
    - Creating an RSS Feed with ASP.Net Written in...
    - Polymorphism in C#
    - Inheritance in C#
    - C# Events Explained
    - C# Delegates Explained
    - C# StreamReader and StreamWriter Explained
    - C# FileStream Explained

    Click Here




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway