:
. .
: . , . .
: _? 1: 2
(true) (folse). 1, 2.
:
static void Main(string[] args)
{
int n;
n = Convert.ToInt32(Console.ReadLine());
n = n == 0? 1: n;
Console.WriteLine("1/n="+1/n);
Console.ReadKey();
}
:
. , A B, A.
: for (; ; ) ;
, .
:
static void Main(string[] args)
{
int i;
for(i=0; i<10; i++)
Console. WriteLine(" i: {0}", i);
Console.ReadKey();
}
(if, switch). , , .
If .
: if 1;
: int a = 1
string tstr = Consol.ReadLine();
int b = Convert. ToInt 32 (tstr);
if (b = = 1) a = 3;
else a = 4;
Console.WriteLine (a);
Switch .
: switch ()
{
case 1; ;
break;
:
switch (op)
{
case +: res=a+b; break;
case -: res=a-b; brear;
case *: res=a*b; break;
case /: res=a/b; break;
default: res=double.NaN: ok=false; break;
}
If (ok) Console.WrileLine(: +res);
else Console.WriteLine( );
(for, while, do, foreach). , , .
. 4 :
while;
do while;
for;
foreach.
, , , . . , . .
|
|
while
. , . (! , .)
: while () ;
:
float a, b, c;
int n=1;
c=0; a=2; b=3;
while (n<6)
{
c += a+b*n;
n++;
}
do while
. do while , , . , do while .
: do while ()
int i = 11;
do
{
Console.WriteLine("i: {0}", i);
i++;
}
while (i < 10);
Console.ReadKey();
for
, , . ().
: for (; ; ) ;
(), . , . , , , 1 .
: , true. .
: , , . .
:
for (int n=1; n<6; n++)
c+=a+b*n;
( + if):
Console.WriteLine(" a b");
String astr, bstr;
astr=Console.ReadLine();
bstr=Console.ReadLine();
double a, b;
a=Convert.ToDouble(astr);
b=Convert.ToDouble(bstr);
Console.WriteLine(" p");
string pstr;
pstr=Console.ReadLine();
double p, x, y;
p=Convert.ToDouble(pstr);
for (x=a; x<b; x+=p)
{
if (Math.Abs(x)<3)
{
y=Math.Pow(Math.Cos(2*x), 2)+7*Math.Pow(Math.Tan(x),4);
}
if (Math.Abs(x) >= 3)
{
y = Math.Sqrt(Math.Abs(1 / Math.Tan(3 * x) - 5));
Console.WriteLine("{0}\t{1}\n", x, y);
Console.ReadLine();
}
}
foreach
, . foreach . , . , . , .
foreach (, ).
: foreach ( __ in ) ;
:
int[] fibarray = new int[] { 0, 1, 1, 2, 3, 5, 8, 13 };
foreach (int element in fibarray)
{
System.Console.WriteLine(element);
|
|
}
System.Console.WriteLine();
for (int i = 0; i < fibarray.Length; i++)
{
System.Console.WriteLine(fibarray[i]);
}
System.Console.WriteLine();
int count = 0;
foreach (int element in fibarray)
{
count += 1;
System.Console.WriteLine("Element #{0}: {1}", count, element);
}
System.Console.WriteLine("Number of elements in the array: {0}", count);