0% found this document useful (0 votes)
47 views6 pages

Codecademy C# Logics

This document discusses various programming concepts in C# including booleans, conditionals, loops, and switches. It demonstrates how to declare and evaluate boolean expressions, use conditional statements like if/else and ternary operators, iterate through loops, and use switch statements to compare a value to multiple cases. The examples show how to output text to the console based on the results of logical comparisons and condition checks.

Uploaded by

Mu Shoshin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views6 pages

Codecademy C# Logics

This document discusses various programming concepts in C# including booleans, conditionals, loops, and switches. It demonstrates how to declare and evaluate boolean expressions, use conditional statements like if/else and ternary operators, iterate through loops, and use switch statements to compare a value to multiple cases. The examples show how to output text to the console based on the results of logical comparisons and condition checks.

Uploaded by

Mu Shoshin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy