C# Basics
C# Basics
0 Variables
0 Basic types
0 Basic interaction with user
Outline
0 Operators
0 Conditional statements
0 Loops
0 Excercises
Operators
0 Assignment
int x = 23;
x = x + 1; Assignment returns a value!
int y = x = x + 5;
0 Rzutowanie
float f = 23;
int i = (int)f;
Operators
0 Arithmetic
z = x + y;
z = x - y;
z = x * y;
z = x / y;
z = x % y;
x++;
x--;
++x;
--x;
if (logical condition)
{
instructions...
}
Conditional statements - if
if (points > 5)
{
Console.WriteLine("Congratulations! You passed!");
}
if (points > 5)
{
Console.WriteLine("Congratulations! You passed!");
}
else
{
Console.WriteLine("Unfortunately, you didn’t pass.");
}
if (points == 10)
{
Console.WriteLine("Great score!!!");
}
else if (points > 5)
{
Console.WriteLine("Congratulations! You passed!");
}
else
{
Console.WriteLine("Unfortunately, you didn’t pass.");
}
switch (option)
{
case "n":
Console.WriteLine("Let’s start the game!");
break;
case "s":
Console.WriteLine("Settings...");
break;
case "q":
Console.WriteLine("Bye bye!");
break;
default:
Console.WriteLine("There’s no such option!");
break;
}
Loops – while
int l = 5;
while (l > 0)
{
Console.WriteLine(l);
l--;
}
Loops – while
int denumerator = 0;
while (denumerator == 0)
{
Console.Write("Denumerator: ");
denumerator = int.Parse(Console.ReadLine());
}
int denumerator = 0;
do
{
Console.Write("Denumerator: ");
denumerator = int.Parse(Console.ReadLine());
} while (denumerator == 0);
int variable = 0;
int denumerator = 0;
do
{
Console.Write("Denumerator: ");
} while (!int.TryParse(Console.ReadLine(), out denumerator) || denumerator == 0);