Csharp/CSharp Tutorial/Statement/If — различия между версиями

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

Версия 15:31, 26 мая 2010

A nested if statement

class MainClass
{
  public static void Main()
  {
    int intValue = 1500;
    string stringValue = " ";
    if (intValue < 1000)
    {
      System.Console.WriteLine("intValue < 1000");
    }
    else
    {
      System.Console.WriteLine("intValue >= 1000");
      if (stringValue == "closed")
      {
        System.Console.WriteLine("closed");
      }
    }
  }
}
intValue >= 1000

bool values in if statement

using System; 
 
class Example { 
  public static void Main() { 
    bool b; 
 
    b = false; 
    Console.WriteLine("b is " + b); 
    b = true; 
    Console.WriteLine("b is " + b); 
 
    // a bool value can control the if statement 
    if(b) 
       Console.WriteLine("This is executed."); 
 
    b = false; 
    if(b) 
       Console.WriteLine("This is not executed."); 
 
  } 
}
b is False
b is True
This is executed.

Demonstrate the if statement

using System; 
 
class MainClass {  
  public static void Main() {  
    int a, b, c;  
  
    a = 2;  
    b = 3;  
  
    if(a < b) 
        Console.WriteLine("a is less than b"); 
 
    if(a == b) 
        Console.WriteLine("you won"t see this");  
 
    Console.WriteLine(); 
 
    c = a - b;
 
    Console.WriteLine("c is",c); 
    if(c >= 0) 
         Console.WriteLine("c is non-negative"); 
    if(c < 0) 
         Console.WriteLine("c is negative"); 
 
    Console.WriteLine(); 
 
    c = b - a;
    Console.WriteLine("c is",c); 
    if(c >= 0) 
        Console.WriteLine("c is non-negative"); 
    if(c < 0) 
        Console.WriteLine("c is negative"); 
  }  
}
a is less than b
c is
c is negative
c is
c is non-negative

Determine if a value is positive, negative, or zero

using System; 
 
class MainClass {  
  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"); 
    } 
  } 
}
Testing -5: negative
Testing -4: negative
Testing -3: negative
Testing -2: negative
Testing -1: negative
Testing 0: no sign
Testing 1: positive
Testing 2: positive
Testing 3: positive
Testing 4: positive
Testing 5: positive

If a value is positive or negative

The semicolon signals the end of a statement.


using System; 
 
class MainClass {  
  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"); 
    } 
  } 
}
Testing -5: negative
Testing -4: negative
Testing -3: negative
Testing -2: negative
Testing -1: negative
Testing 0: positive
Testing 1: positive
Testing 2: positive
Testing 3: positive
Testing 4: positive
Testing 5: positive

if statement with || and &&

using System;
class MainClass
{
  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);
  }
}
5
5

Relational operators that can be used in a conditional expression.

Operator Meaning < Less than <= Less than or equal > Greater than >= Greater than or equal == Equal to != Not equal

The if Statement

You can selectively execute part of a program through the if statement.

Its simplest form is shown here:


if(condition) 
   statement;

Use an if statement that executes a block

A code block is a grouping of two or more statements. This is done by enclosing the statements between opening and closing curly braces.


class MainClass
{
  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);
    }
  }
}
5 is less than 100