140 lines
2.7 KiB
Markdown
140 lines
2.7 KiB
Markdown
# Condition
|
|
|
|
## if / else
|
|
|
|
```C#
|
|
Console.WriteLint("Please enter your age: ");
|
|
int age = Convert.ToInt32(Console.ReadLine());
|
|
|
|
if (age > 100){
|
|
Console.WriteLine("You are to old to signed up! ");
|
|
}
|
|
else if(age >= 18){
|
|
Console.WriteLine("You are now signed up! ");
|
|
}
|
|
else if (age < 0){
|
|
Console.WriteLine("You are not born yet! ");
|
|
}
|
|
else{
|
|
Console.WriteLine("You must be 18+ to sing up! ");
|
|
}
|
|
```
|
|
|
|
```C#
|
|
Console.WriteLint("Please enter your name : ")
|
|
String name = Console.ReadLine()
|
|
|
|
if (name != ""){
|
|
Console.WriteLine("hello : " + name);
|
|
}else{
|
|
Console.WriteLine("You did not enter your name !" );
|
|
}
|
|
|
|
```
|
|
|
|
## switch
|
|
|
|
```C#
|
|
Console.WriteLint("What day is it today ?");
|
|
String day = Console.ReadLine();
|
|
|
|
switch(day)
|
|
{
|
|
case "Monday":
|
|
Console.WriteLine("It's Moday!")
|
|
break;
|
|
case "Tuesday":
|
|
Console.WriteLine("It's Tuesday!")
|
|
break;
|
|
case "Wednesday":
|
|
Console.WriteLine("It's Wednesday!")
|
|
break;
|
|
case "Thursday":
|
|
Console.WriteLine("It's Thursday!")
|
|
break;
|
|
case "Friday":
|
|
Console.WriteLine("It's Friday!")
|
|
break;
|
|
case "Saturday":
|
|
Console.WriteLine("It's Friday!")
|
|
break;
|
|
case "Sunday":
|
|
Console.WriteLine("It's Friday!")
|
|
break;
|
|
default:
|
|
Console.WriteLine(day + "is not a day")
|
|
break;
|
|
}
|
|
|
|
```
|
|
|
|
## Logical Operator
|
|
|
|
can be used to check if more than 1 condition is true / false
|
|
&& (AND)
|
|
|| (OR)
|
|
|
|
```C#
|
|
Console.WriteLine("What's the temperature outside");
|
|
double temp = Convert.ToDouble(Console.ReadLine());
|
|
|
|
if(temp >= 10 && temp <= 25){
|
|
Console.WriteLine("It's warm outside");
|
|
}else if(temp <= -50 || temp >= 50){
|
|
Console.WriteLine("DO NOT GO OUTSIDE !");
|
|
}
|
|
```
|
|
|
|
## try / catch / exception
|
|
|
|
errors that occur during execution
|
|
|
|
try = try some code that is considered "dangerous"
|
|
catch = catches and handles execptions when they occur
|
|
finally = always executes regardless if exception is caught or not
|
|
|
|
```C#
|
|
double x;
|
|
double y;
|
|
double result;
|
|
|
|
try
|
|
{
|
|
Console.Write("Enter number 1: ");
|
|
x = Convet.ToDouble(Console.ReadLine());
|
|
|
|
Console.Write("Enter number 2: ");
|
|
y = Convet.ToDouble(Console.ReadLine());
|
|
|
|
result = x / y;
|
|
|
|
Console.WriteLine("result: " + result);
|
|
|
|
}
|
|
catch(FormatExecption e)
|
|
{
|
|
Console.WriteLine("Enter ONLY numbers Please!");
|
|
}
|
|
catch(DivideByZeroExecption e)
|
|
{
|
|
Console.WriteLine("you can't divide by zero");
|
|
}
|
|
catch ( Exeption e)
|
|
{
|
|
Console.WriteLine("something went wrong !");
|
|
}
|
|
finally
|
|
{
|
|
Console.WriteLine("Tanks for visiting");
|
|
}
|
|
```
|
|
|
|
## Conditional Operator
|
|
|
|
// (condition) ? y : x
|
|
|
|
```C#
|
|
double temperature = 20;
|
|
Console.WriteLine((tempure >= 15) ? "It's warm outside!" : "It's cold outside!"; )
|
|
```
|