C Sharp/if Statement

From Meshplex

Jump to: navigation, search
Image:Csharp_programming.gif

Main Home

Basics
C# Tutorial Home
C# - Introduction to Visual Studio IDE
Introduction to C#
C# - Overview
C# - Statements
C# - Data Types
C# - Variables
C# - Operators
C# - Flow Control

C# - Variables II
C# - Functions and Methods
C# - Classes and Objects I
C# - Enumerations
C# - Dates and Times
C# - Random Numbers

Advanced
C# - Inheritance
C# - Polymorphism
C# - Garbage Collection
C# - Operator Overloading
C# - Encapsulation
C# - Properties
C# - Indexers
C# - Exceptions
C# - GUI
C# - Delegates
C# - Events
C# - Components
C# - Multithreading
C# - Regular Expressions
C# - Graphics and Multimedia
C# - Files and Streams
C# - XML
C# - Database, SQL and ADO.NET
C# - ASP.NET Web Forms and Web Controls
C# - Web Services
C# - Network Programming
C# - Datastructures and Collections
C# - Enumerations and Iterators
C# - .NET Assemblies
C# - CLR
C# - Visual Studio Debugger
C# - Namespaces
C# - Generics
C# - MS Intermediate Language
C# - Deploying Windows Application

Contents

[edit] C# IF Statements

The C# if/else statement makes decisions. An if statement can only evaluate to two results: true or false. Depending upon the result of the of the condition the if block will execute or be ignored. Lets take a look at an example:


C# Pseudocode

If Persons Age is greater than 75
Print "Nice retirement"

The statement above determines if the condition Age is greater than 75. If the condition is true then it will print Nice retirement. If the condition is false then the statement is ignored then the program continues on the next piece of code.

The example above can be written in pure C# code like this:

if(age > 75)
     Console.WriteLine("Have a nice retirement");

The C# code directly relates to the pseudocode. Again, if age is greater than 75 the code will print "Have a nice retirement". If the condition is false then the statement will be ignored and it continues to the next part of the code.


Example

  1. using System;
  2.  
  3. class Program
  4. {
  5. static void Main(string[] args)
  6. {
  7. int age = 31;
  8.  
  9. if(age > 75)
  10. Console.WriteLine("Have a nice retirement");
  11.  
  12. Console.Read();
  13. }
  14. }

The variable age is 31. When the program reaches line 9 it will determine if age > 75 is true or false. In this case the condition evaluates to false. So, line 10 will not execute. "Have a nice retirement" will not print because line 9 evaluates to false.

Here is a table of all the allowable comparison operators for c#


C# Comparison Operators

Operator Name Description
< Less than Determines if left side is less than right side
> Greater than Determines if right side is greater than left side
<= Less than or equal to Determines if left side is less than or equal to right side
>= Greater than or equal to Determines if left side is greater than or equal to right side
== Equal to Determines if left side is equal to right side
 != Not equal to Determines if left side is not equal to right side

[edit] C# if/else Statements

The if statement is executed when the condition is true. But what if you want the program to perform a certain task if the condition is false. The else statement comes to the rescue. Lets take the following pseudocode as a guide:

If age is greater than 75
Print "Have a nice retirement"
else
Print "You are still young"

The pseudocode prints "Have a nice retirement" if the condition evaluates to true. If the condition evaluates to false then the program prints "You are still young". So if the if statement evaluates to true then the else statement is not executed.


Here is a code snippet that relates to the pseudocode:

if (age > 75)
     Console.WriteLine("Have a nice retirement");
else
     Console.WriteLine("You are still young");


C# code example:

using System;
 
class Program
{
    static void Main(string[] args)
    {
        int age = 31;
 
        if (age > 75)
            Console.WriteLine("Have a nice retirement");
        else
            Console.WriteLine("You are still young");
 
        Console.Read();
    }
}

Output:

You are still young


As you can see if age is greater than 75 it will print "Have a nice retirement", otherwise it will print "You are still young".

In our next section we will discuss nested if/else statements.

[edit] C# Nested if/else Statements

Example code:

using System;
 
class Program
{
    static void Main(string[] args)
    {
        char grade = 'b';
 
        if (grade == 'a')
            Console.WriteLine("Outstanding student");
        else if (grade == 'b')
            Console.WriteLine("Good student");
        else if (grade == 'c')
            Console.WriteLine("Average student");
        else if (grade == 'd')
            Console.WriteLine("Need a little more practice");
        else
            Console.WriteLine("Need alot of practice");
 
        Console.Read();
    }
}


  • Use two = "==" signs when comparing variable or literals.
  • Only one if/else if block is executed. The others are skipped.

[edit] C# Ternary Operator

  • The ternary operator looks like this: ?:

Ternary operator has one condition expression and can return one of possible two values. Lets take a look at an example:


  1. using System;
  2.  
  3. namespace ConsoleApplication3
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. int a = 1;
  10. string b = (a < 2) ? "true" : "false";
  11. Console.WriteLine(b);
  12. Console.Read();
  13. }
  14. }
  15. }

true


Line 10: Since (a < 2) evaluates to true, true is printed. If (a < 2) evaluated to false then false would be the result.