Csharp/C Sharp/Language Basics/If

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Another if else

<source lang="csharp"> /* Learning C# by Jesse Liberty Publisher: O"Reilly ISBN: 0596003765

  • /
using System;
public class NestIfValues
{
   static void Main()
   {
      int temp = 32;
      if (temp <= 32)
      {
         Console.WriteLine("Warning! Ice on road!");
         if (temp == 32)
         {
           Console.WriteLine(
            "Temp exactly freezing, beware of water.");
         }
         else
         {
            Console.WriteLine("Watch for black ice! Temp: {0}", temp);
         }
      }
   }
}
          
      </source>


Demonstrate a block of code

<source lang="csharp"> /* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852

  • /

// Demonstrate a block of code.

using System;

public class BlockDemo {

 public static void Main() { 
   int i, j, d; 

   i = 5; 
   j = 10; 

   // the target of this if is a block 
   if(i != 0) { 
     Console.WriteLine("i does not equal zero"); 
     d = j / i; 
     Console.WriteLine("j / i is " + d); 
   } 
 } 

}

      </source>


Demonstrate the if

<source lang="csharp"> /* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852

  • /

// Demonstrate the if.

using System;

public class IfDemo {

 public static void Main() {  
   int a, b, c;  
 
   a = 2;  
   b = 3;  
 
   if(a < b) Console.WriteLine("a is less than b"); 

   // this won"t display anything  
   if(a == b) Console.WriteLine("you won"t see this");  

   Console.WriteLine(); 

   c = a - b; // c contains -1 

   Console.WriteLine("c contains -1"); 
   if(c >= 0) Console.WriteLine("c is non-negative"); 
   if(c < 0) Console.WriteLine("c is negative"); 

   Console.WriteLine(); 

   c = b - a; // c now contains 1 
   Console.WriteLine("c contains 1"); 
   if(c >= 0) Console.WriteLine("c is non-negative"); 
   if(c < 0) Console.WriteLine("c is negative"); 

 }  

}

      </source>


Determine if a value is positive, negative, or zero

<source lang="csharp"> /* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852

  • /

// Determine if a value is positive, negative, or zero. using System;

public class PosNegZero {

 public static void Main() { 
   int i; 

   for(i=-5; i <= 5; i++) { 

     Console.Write("Testing " + i + ": "); 

     if(i < 0) Console.WriteLine("negative"); 
     else if(i == 0) Console.WriteLine("no sign"); 
       else Console.WriteLine("positive"); 
   } 

 } 

}


      </source>


Determine if a value is positive or negative

<source lang="csharp"> /* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852

  • /

// Determine if a value is positive or negative. using System;

public class PosNeg {

 public static void Main() { 
   int i; 

   for(i=-5; i <= 5; i++) { 
     Console.Write("Testing " + i + ": "); 

     if(i < 0) Console.WriteLine("negative"); 
     else Console.WriteLine("positive"); 
   } 

 } 

}

      </source>


If Branching

<source lang="csharp"> /* Learning C# by Jesse Liberty Publisher: O"Reilly ISBN: 0596003765

  • /
using System;
namespace Branching
{
   public class TestBranching
   {
      static void Main()
      {
            int valueOne = 10;
            int valueTwo = 20;
            int valueThree = 30;
            Console.WriteLine("Testing valueOne against valueTwo...");
            if ( valueOne > valueTwo )
            {
                Console.WriteLine(
                "ValueOne: {0} larger than ValueTwo: {1}",
                valueOne, valueTwo);
            }
            Console.WriteLine("Testing valueThree against valueTwo...");
            if ( valueThree > valueTwo )
            {
                Console.WriteLine(
                    "ValueThree: {0} larger than ValueTwo: {1}",
                    valueThree, valueTwo);
            }   // end if
      }         // end Main
   }            // end class
}               // end namespace
          
      </source>


If Else

<source lang="csharp"> /* Learning C# by Jesse Liberty Publisher: O"Reilly ISBN: 0596003765

  • /
using System;
namespace Branching
{
    public class TestIfElse
    {
        static void Main()
        {
            int valueOne = 10;
            int valueTwo = 20;
            Console.WriteLine("Testing valueOne against valueTwo...");
            if ( valueOne > valueTwo )
            {
                Console.WriteLine(
                    "ValueOne: {0} larger than ValueTwo: {1}",
                    valueOne, valueTwo);
            }       // end if
            else
            {
                Console.WriteLine(
                    "Nope, ValueOne: {0} is NOT larger than ValueTwo: {1}",
                    valueOne, valueTwo);
            }       // end else
        }           // end Main
    }               // end class
}                   // end namespace
          
      </source>


If else for int

<source lang="csharp"> /*

* C# Programmers Pocket Consultant
* Author: Gregory S. MacBeth
* Email: gmacbeth@comporium.net
* Create Date: June 27, 2003
* Last Modified Date:
*/

using System; namespace Client.Chapter_4___Program_Control {

 public class ifelse
 {
   static void Main(string[] args)
   {
     int a = 5, b = 5, c = 10;
     if (a == b)
       Console.WriteLine(a);
     if ((a > c) || (a == b))
       Console.WriteLine(b);
     if ((a >= c) && (b <= c))
       Console.WriteLine(c);
   }
 }

}

      </source>


illustrates the use of a nested if statement

<source lang="csharp"> /* Mastering Visual C# .NET by Jason Price, Mike Gunderloy Publisher: Sybex; ISBN: 0782129110

  • /

/*

 Example4_3.cs illustrates the use of
 a nested if statement
  • /

public class Example4_3 {

 public static void Main()
 {
   int reactorTemp = 1500;
   string emergencyValve = " ";
   if (reactorTemp < 1000)
   {
     System.Console.WriteLine("Reactor temperature normal");
   }
   else
   {
     System.Console.WriteLine("Reactor temperature too high!");
     if (emergencyValve == "closed")
     {
       System.Console.WriteLine("Reactor meltdown in progress!");
     }
   }
 }

}

      </source>


Illustrates the use of an if statement that executes a block

<source lang="csharp"> /* Mastering Visual C# .NET by Jason Price, Mike Gunderloy Publisher: Sybex; ISBN: 0782129110

  • /

/*

 Example4_2.cs illustrates the use of an if statement
 that executes a block
  • /

public class Example4_2 {

 public static void Main()
 {
   int smallNumber = 5;
   int bigNumber = 100;
   if (bigNumber < smallNumber)
   {
     System.Console.Write(bigNumber);
     System.Console.Write(" is less than ");
     System.Console.Write(smallNumber);
   }
   else
   {
     System.Console.Write(smallNumber);
     System.Console.Write(" is less than ");
     System.Console.Write(bigNumber);
   }
 }

}

      </source>


Illustrates the use of the if statement

<source lang="csharp"> /* Mastering Visual C# .NET by Jason Price, Mike Gunderloy Publisher: Sybex; ISBN: 0782129110

  • /

/*

 Example4_1.cs illustrates the use of the if statement
  • /

public class Example4_1 {

 public static void Main()
 {
   int smallNumber = 5;
   int bigNumber = 100;
   if (bigNumber > smallNumber)
     System.Console.WriteLine(bigNumber + " is greater than " +
       smallNumber);
   else
     System.Console.WriteLine(bigNumber + " is less than " +
       smallNumber);
 }

}


      </source>


Logical operators with an if statement

<source lang="csharp"> /* Mastering Visual C# .NET by Jason Price, Mike Gunderloy Publisher: Sybex; ISBN: 0782129110

  • /

/*

 Example4_4.cs illustrates the use of
 logical operators with an if statement
  • /

public class Example4_4 {

 public static void Main()
 {
   int reactorTemp = 1500;
   string emergencyValve = "closed";
   if ((reactorTemp > 1000) && (emergencyValve == "closed"))
   {
     System.Console.WriteLine("Reactor meltdown in progress!");
   }
 }

}


      </source>