c# if
string emotions;
emotions = Console.ReadLine();
//for emotions say the user input was either
//happy,sad and mad
if (emotions == "happy")
{
Console.Write("You are happy");
}
else if (emotions == "sad")
{
Console.Write("You are sad");
}
else if (emotions == "mad")
{
Console.Write("You are mad");
}
else
{
Console.Write("Unknown input");
}
4.5
4
//Default If condition
if (comEnvList.Items.Count > 0)
{
comEnvList.SelectedIndex = 1;
}
else
{
comEnvList.SelectedIndex = 0;
}
//IIf statement
comEnvList.SelectedIndex = comEnvList.Items.Count > 0 ? 1 : 0;
Thank you!
4
0
3
1
int x;
int y;
if ( x == y)
{
Console.WriteLine("x and y is equal");
}
else
{
Console.WriteLine("x and y is not equal");
}
// Same, but shorter ( and harder XD )
Console.WriteLine($"x and y {(x == y? "is equal" : "is not equal")}");
Thank you!
1
0
4
6
if (20 > 18)
{
Console.WriteLine("20 is greater than 18");
}
/*
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
*/
Thank you!
6
0
0
0
if (condition1)
{
// block of code to be executed if condition1 is True
}
else if (condition2)
{
// block of code to be executed if the condition1 is false and condition2 is True
}
else
{
// block of code to be executed if the condition1 is false and condition2 is False
}
Thank you!
0
0
3.79
7
if (condition)
{
print("Yes");
}
else
{
print("No");
}
Thank you!
7
0
Are there any code examples left?
New code examples in category C#