C-Sharp: различия между версиями
Перейти к навигации
Перейти к поиску
Строка 13: | Строка 13: | ||
int[] age = new int[5] (); | int[] age = new int[5] (); | ||
int[] age2 = {3, 7, 15, 21, 7}; | int[] age2 = {3, 7, 15, 21, 7}; | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | == Ввод и вывод == | ||
+ | <syntaxhighlight lang="c#" line> | ||
+ | Console.WriteLine("Please, enter your age:"); | ||
+ | string ageString = Console.ReadLine(); | ||
+ | int age = Convert.ToInt32(ageString); | ||
+ | Console.WriteLine("Your age = " + age); | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | == Условный оператор == | ||
+ | <syntaxhighlight lang="c#" line> | ||
+ | Console.WriteLine("Please, enter your age:"); | ||
+ | string ageString = Console.ReadLine(); | ||
+ | int age = Convert.ToInt32(ageString); | ||
+ | if (age > 65) | ||
+ | { | ||
+ | Console.WriteLine("Here is your pension."); | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | Console.WriteLine("Good morning people!"); | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | == Циклы == | ||
+ | <syntaxhighlight lang="c#" line> | ||
+ | Console.WriteLine("while loop"); | ||
+ | i = 1; | ||
+ | while (i < 11) | ||
+ | { | ||
+ | Console.WriteLine(i); | ||
+ | i++; | ||
+ | } | ||
+ | |||
+ | Console.WriteLine("do while loop"); | ||
+ | i = 1; | ||
+ | do | ||
+ | { | ||
+ | Console.WriteLine(i); | ||
+ | i++; | ||
+ | } while (i < 11); | ||
+ | |||
+ | Console.WriteLine("for loop"); | ||
+ | for (int j = 1; j < 11; j++) | ||
+ | { | ||
+ | Console.WriteLine(j); | ||
+ | } | ||
</syntaxhighlight> | </syntaxhighlight> |
Версия 12:04, 23 ноября 2020
Hello World
Объявление переменных
1 int a = 5;
2 double b = a / 2;
3 bool test = true;
4 char firstLetter = 'C';
5 string name = "Hi, there!";
6 int[] age = new int[5] ();
7 int[] age2 = {3, 7, 15, 21, 7};
Ввод и вывод
1 Console.WriteLine("Please, enter your age:");
2 string ageString = Console.ReadLine();
3 int age = Convert.ToInt32(ageString);
4 Console.WriteLine("Your age = " + age);
Условный оператор
1 Console.WriteLine("Please, enter your age:");
2 string ageString = Console.ReadLine();
3 int age = Convert.ToInt32(ageString);
4 if (age > 65)
5 {
6 Console.WriteLine("Here is your pension.");
7 }
8 else
9 {
10 Console.WriteLine("Good morning people!");
11 }
Циклы
1 Console.WriteLine("while loop");
2 i = 1;
3 while (i < 11)
4 {
5 Console.WriteLine(i);
6 i++;
7 }
8
9 Console.WriteLine("do while loop");
10 i = 1;
11 do
12 {
13 Console.WriteLine(i);
14 i++;
15 } while (i < 11);
16
17 Console.WriteLine("for loop");
18 for (int j = 1; j < 11; j++)
19 {
20 Console.WriteLine(j);
21 }