if
else
true
false
a == b
a != b
a < b
a <= b
a > b
a >= b
bool
int a = 4; int b = 3; bool areEqual = a == b; // outputs False bool biggerOrNot = a > b; // outputs True
else if
int a = 2 if (a > 4) { // do something } else if (a < 2) { // do something else } else if (a < 3) { // do something else } else { // a is 4?? }
double temperatureInOulu = 2.3; double temperatureInIvalo = -10.9; if (temperatureInOulu > temperatureInIvalo) { Console.WriteLine("Oulu is warmer than Ivalo"); } else if (temperatureInOulu < temperatureInIvalo) { Console.WriteLine("Ivalo is warmer than Oulu"); } else { Console.WriteLine ("Oulu and Ivalo have the same temperature"); }
This outputs Oulu is warmer than Ivalo.
Oulu is warmer than Ivalo
!
Console.WriteLine(!true); // Outputs false bool itsColdOutside = true; if (!itsColdOutside) // same as checking if (itsColdOutside == false) { Console.WriteLine("It's warm outside."); }
switch
path
case
Here's the catalogue!
break
string path = "/browse"; switch (path) { case "/browse": Console.WriteLine("Here's the catalogue!"); break; case "/contact": Console.WriteLine("Here's our contact info."); break; default: Console.WriteLine("Given path doesn't exist!"); break; }
{}
if (true) { int b = 1; // b is only accessible in this block b = 2; // This works } b = 3; // This throws an error
namespace
global
&&
||
int a = 1; int b = 3; int c = 5; if (a < 10) { if (b < 10) { if (c < 10) { Console.WriteLine ("All are smaller than 10!") } } }
int a = 1; int b = 3; int c = 5; if (a < 10 && b < 10 && c < 10) { Console.WriteLine ("All are smaller than 10!") }
a && b
b
a == false
a || b
a == true
!a
^
a ^ b
a
&
a & b
|
a | b
int uppercaseLetters = 2; uppercaseLetters += 4; // is now 6 int specialCharacters = 2; specialCharacters *= 2; // is now 4 if (uppercaseLetters >= 6 && specialCharacters >= 2) { Console.WriteLine("Strong password!"); } else { Console.WriteLine("Weak password..."); }
This outputs Strong password!
Strong password!
int baa = 49; if (baa > 20) Console.WriteLine("Baa"); else Console.WriteLine("Not baa!"); if (baa > 20) Console.WriteLine("Baa"); else Console.WriteLine("Not baa!");
Have a nice week!
Have a nice weekend!
Tip: Use DateTime.Now.ToString() for current time. Use .Length after your message variable to get the length of the message.
DateTime.Now.ToString()
.Length
Basics covering the syntax in C# are covered here:
Assignments about this topic can be found here
| Comparison | Syntax | |:-------------------------|:---------| | Less than | `a < b` | | Less than or equal to | `a <= b` | | Greater than | `a > b` | | Greater than or equal to | `a >= b` | | Equal to | `a == b` | | Not equal to | `a != b` |