C#
  Home arrow C# arrow Page 2 - 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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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 / 49
    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 - No Implicit Conversion


    (Page 2 of 6 )

     

    C# eliminates a common source of programming errors that existed in the world of C and C++, which is the implicit conversion of numeric values to Boolean values (0 to false and all the other numeric values to true). In C#, the if statement's expression must evaluate to a Boolean value, so the next block of code will not compile in C# but it will compile in C++:

    int x = 10;
    if(x)
    {
      Console.WriteLine(x);
    }

    In C# you will get a compile time error that states "Can't implicitly convert type 'int' to 'bool'". You need to write this block as follows:

    int x = 10;
    if(x > 5)
    {
      Console.WriteLine(x);
    }

    This will compile in C# because now it's a Boolean expression. Let's look at an example. The following example takes an input number from the user, tests to see if it's an even or odd number (using the module operator), and prints a line in each case.

    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)
          {
            Console.WriteLine("this is an EVEN number");
          }
          else
          {
            Console.WriteLine("this is an ODD number");
          } 
          Console.ReadLine(); 
        } 
      } 
    }
     

    More C# Articles
    More By Michael Youssef


     

    C# ARTICLES

    - C# Meets Design Patterns
    - Coding a CRC-Generating Algorithm in C
    - Cyclic Redundancy Check
    - Handling Methods and Functions
    - Destroying Objects in C#
    - Creating Objects in C-Sharp
    - Classes and Objects
    - Programming Languages: Managed versus Native
    - LINQ-to-MySQL with DbLinq in C#
    - Working with Dates and Times in C#
    - Generics, Dictionaries, and More
    - More About Generics
    - Working with C# Collections
    - Generics
    - C# and XML





    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek