C# - Basic & Control Flow

Basic variables and operation

Read line

Console.WriteLine("Enter your name: ");
name = Console.ReadLine();

Variables and Types

string foo = "Hello";
int x = 5;
Console.WriteLine(foo);
double x = 81;
Console.Write(Math.Sqrt(x));
bool a = (2>1);
bool b = a && true;
bool c = !false || (7<8);

Math operator

double pow_ab = Math.Pow(6,2)
a++
b--

String operator

string upperstr2 = str2.ToUpper();
strong lower = mixedCase.ToLower();
string str = "Divyesh";
int index1 = str.IndexOf('s');
char first = str[0];
string test1 = str.Substring(2);
string name = firstName + " " + lastName;
str.Length;
Console.WwriteLine("Hello\nWorld");

Control Flow

If Statement & Else Clause & If and Else If

if (true) {
 Console.WriteLine("Hello User!");
}
if (true) {
	Console.WriteLine("Seen!");
} else {
	Console.WriteLine("Not seen!");
}
int x = 100, y = 80;
if (x > y)
{
	Console.WriteLine("x is greater than y");
}
else if (x < y)
{
	Console.WriteLine("x is less than y");
}
else
{
	Console.WriteLine("x is equal to y');
}

Switch Statements & Break Keyword: evaluates one expression and decides which code block to run by trying to match the result of the expression to each case. Switch statements are often used to replace if else structures when all conditions test for equality on one value.

string color = "blue";
switch (color) {
	case "red":
		Console.WriteLine("I don't like that color.");
		break;
	case "blue":
		Console.WriteLine("I like that color.");
		break;
	default:
		Console.WriteLine("I feel ambivalent about that color.");
		break;
}

Ternary Operator

bool isRaining = true;
string umbrellaOrNot = isRaining ? "Umbrella" : "No Umbrella";
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值