Csharp/CSharp Tutorial/Statement/If
Содержание
- 1 A nested if statement
- 2 bool values in if statement
- 3 Demonstrate the if statement
- 4 Determine if a value is positive, negative, or zero
- 5 If a value is positive or negative
- 6 if statement with || and &&
- 7 Relational operators that can be used in a conditional expression.
- 8 The if Statement
- 9 Use an if statement that executes a block
A nested if statement
<source lang="csharp">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"); } } }
}</source>
intValue >= 1000
bool values in if statement
<source lang="csharp">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."); }
}</source>
b is False b is True This is executed.
Demonstrate the if statement
<source lang="csharp">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"); }
}</source>
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
<source lang="csharp">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"); } }
}</source>
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.
<source lang="csharp">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"); } }
}</source>
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 &&
<source lang="csharp">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); }
}</source>
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:
<source lang="csharp">if(condition)
statement;</source>
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.
<source lang="csharp">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); } }
}</source>
5 is less than 100