Вы находитесь на странице: 1из 6

Firefox https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-...

// These expressions all evaluate to a


boolean value.
// Therefore their values can be stored in
boolean variables.
bool a = (2 > 1);
bool b = a && true;
bool c = !false || (7 < 8);

bool true false


bool skyIsBlue = true;
bool penguinsCanFly = false;
Console.WriteLine($"True or false, is the
sky blue? {skyIsBlue}.");
// This simple program illustrates how
booleans are declared. However, the real
power of booleans requires additional
programming constructs such as
conditionals.

// These variables equal true.


&& bool a = true && true;
true true bool b = false || true;
|| bool c = !false;

true true
// These variables equal false.
!
bool d = true && false;
bool e = false || false;
bool f = !true;

1 sur 6 16/05/2020 à 15:21


Firefox https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-...

true false int x = 5;


Console.WriteLine(x < 6); // Prints "True"
int because 5 is less than 6.
Console.WriteLine(x > 8); // Prints
string "False" because 5 is not greater than 8.
string
string foo = "foo";
Console.WriteLine(foo == "bar"); // Prints
"False" because "foo" does not equal
"bar".

2 sur 6 16/05/2020 à 15:21


Firefox https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-...

if (true) {
true false // This code is executed.
true Console.WriteLine("Hello User!");
{} }

if (false) {
// This code is skipped.
Console.WriteLine("This won't be seen
:(");
}

3 sur 6 16/05/2020 à 15:21


Firefox https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-...

break
string color = "blue";
switch case
switch
switch (color) {
case switch
case "red":
break
Console.WriteLine("I don't like that
color.");
break;
case
case "blue":
Console.WriteLine("I like that
color.");
break;
default:
Console.WriteLine("I feel ambivalent
about that color.");
break;
}
// Regardless of where the break statement
is in the above switch statement,
// breaking will resume the program
execution here.
Console.WriteLine("- Steve");

condition ? expression1 : bool isRaining = true;


expression2 // This sets umbrellaOrNot to "Umbrella"
if isRaining is true,
if // and "No Umbrella" if isRaining is
false.
string umbrellaOrNot = isRaining ?
true false "Umbrella" : "No Umbrella";

// "Umbrella"
Console.WriteLine(umbrellaOrNot);

4 sur 6 16/05/2020 à 15:21


Firefox https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-...

else {}
if (true) {
else else
// This block will run.
if
Console.WriteLine("Seen!");
} else {
if false
// This will not run.
Console.WriteLine("Not seen!");
if
}

if (false) {
// Conversely, this will not run.
Console.WriteLine("Not seen!");
} else {
// Instead, this will run.
Console.WriteLine("Seen!");
}

if
int x = 100, y = 80;
else else
if
if (x > y)
else
{
else if
Console.WriteLine("x is greater than
true y");
true }
else else if (x < y)
{
Console.WriteLine("x is less than y");
}
else
{
Console.WriteLine("x is equal to y");
}

5 sur 6 16/05/2020 à 15:21


Firefox https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-...

switch
// The expression to match goes in
parentheses.
case switch (fruit) {
case case "Banana":
// If fruit == "Banana", this block
==
will run.
true switch
Console.WriteLine("Peel first.");
if else
break;
case "Durian":
Console.WriteLine("Strong smell.");
break;
default:
// The default block will catch
expressions that did not match any above.
Console.WriteLine("Nothing to say.");
break;
}

// The switch statement above is


equivalent to this:
if (fruit == "Banana") {
Console.WriteLine("Peel first.");
} else if (fruit == "Durian") {
Console.WriteLine("Strong smell.");
} else {
Console.WriteLine("Nothing to say.");
}

6 sur 6 16/05/2020 à 15:21

Вам также может понравиться