ProgBasics_lec02_operators_arrays
.pdfusing System;
namespace CSConsoleApplication
{
static class Program
{
static void Main()
{
int a, b;
string buf = Console.ReadLine(); a = int.Parse(buf);
b = Convert.ToInt32(Console.ReadLine()); double c = (a + b) / 2;
bool isEven = c % 2 == 0; Console.WriteLine("Number is even? {0}",
isEven ? "yes" : "no");
Console.WriteLine("Press any key to exit."); Console.ReadKey();
}
}
} |
1 |
|
Блок
{
//это блок - последовательность //операторов, заключенная в //операторные скобки (begin-end, { - })
}
•Блок = один оператор
•Блок может содержать один оператор или быть пустым
• Блоки могут быть вложенными |
2 |
|
Условный оператор
if (<условие>)
{
//условие истинно
}
else if (<условие>)
{
//другое условие истинно
}
else
//все условия - ложны
3
Оператор switch
switch (<выражение>)
{
case <значение>: break;
case <значение>: { break; } default: break;
}
4
Оператор switch
int val = 3; switch (val)
{
case 1: break; case 2:
case 3: Console.WriteLine("3!"); break; default: break;
}
5
Оператор switch
string str = "hello"; switch (str)
{
case "Hello": Console.WriteLine("Hello"); break; case "helLO": Console.WriteLine("helLO"); break; default: Console.WriteLine(str); break;
}
6
Оператор switch
string str = "hello"; switch (str)
{
case "Hello": Console.WriteLine("Hello"); break; case "helLO": Console.WriteLine("helLO"); break;
}
Console.WriteLine("Switch completed.");
7
Оператор switch
int a = 2; switch (a)
{
case 1: Console.WriteLine("1"); break; case 2: Console.WriteLine("2");
case 3: Console.WriteLine("3"); break;
}
8
Циклы
•for
•while
•do – while
•foreach
9
Цикл for
for (initializer; condition; iterator) body
initializer –инициализатор condition – условие работы цикла iterator – итератор
body – тело цикла
10