5.3 KiB
Conditionals
Overview
Conditionals
if and else
Logical Operators
Conditionals
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
Conditionals return true or false, meaning the result can even be allocated into a variable if needed
bool areEqual = (a == b);
if , else and else if statements
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");
}
__if __ statements are executed in order
The first statement with a __true __ condition is executed
If no statement holds a true value, __else __ is executed
This outputs "Oulu is warmer than Ivalo"
! -Operator
Console.WriteLine(!true); // Outputs false
bool itsColdOutside = true;
if(!itsColdOutside)
{
Console.WriteLine("It's warm outside.");
}
The ! -operator flips the boolean value __ __
The switch statement
The __switch __ statement compares the parameter value (here: the __path __ variable) with the value of each case
Only the matching code is executed
If no match is found, the default code block is executed
This outputs "Here's the catalogue!"
break ends the case and exits the switch: the code will not compile if omitted
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("No such path!");
break;
}
Scope of Variables
Variables declared inside of blocks are called local variables ; they are only accessible inside of that block.
int a = 0;
if(a < 10)
{
// Variable 'b' is only accessible inside of this if block
int b = 1;
b = 2; // This works
}
b = 3; // This throws an error
Assignment Operators
We have used the assignment operator ' =' for assigning values for variables:
int x;
x = 25;
__Notice __ the difference between '=' and the conditional '=='!
'=' is used for assigning values for variables, '== is used for comparing values
Logical Operators
'&&', '||' and '!' mean the logical AND, OR and NOT operators
For example,
int a = 1;
int b = 3;
int c = 5;
Console.WriteLine(a < b && a < c);
outputs "True"
Operator | Name | Example | Description |
---|---|---|---|
&& | AND | a && b | Returns true if both variables are true.b is not evaluated if a == false. |
OR | |||
! | NOT | !a | Negates boolean value (true becomes false and vice versa) |
^ | XOR | a ^ b | Exclusive OR: returns true if __only __ a == true OR only b == true. |
& | Logical AND | a & b | Returns true if both variables are true. Both variables are always evaluated. |
Logical OR | a |
Tässä on kaikki aritmeettiset operaattorit
Operators - Example
This outputs "Strong password!"
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-ass password...");
}
Exercise 1:
Create a console application which asks the user which weekday it is and assigns the answer to a string variable.
Using a switch-case, calculate the days remaining until next Monday.
If the result is more than 3, print "Have a nice week!". Otherwise, print "Have a nice weekend!".
Exercise 2:
Create a console application which lets the user input a note.
If the length of the note is less than 30, the program prints the current time and the note, separated with a tab. Otherwise, the date and the note are printed to a separate line.
Tip: Use DateTime.Now.ToString() for current time. Use .Length after your message variable to get the length of the message.
One More Thing...
If a code block following a statement only has one line of code, you can write the code without using curly brackets:
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!");
You may see code where this is done. However, we highly recommend you not to use this syntax as it is highly prone to errors .
Get Help
All the basics covering the syntax in C# are covered here:
https://www.tutorialspoint.com/csharp/index. htm l
https://www.w3schools.com/cs/default.asp