Csharp/CSharp Tutorial/Statement/If — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 12:20, 26 мая 2010
Содержание
- 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
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