2. Операторы языка C#

  • Published on
    15-Apr-2017

  • View
    307

  • Download
    0

Embed Size (px)

Transcript

  • C#

    .NET.

    , , 2012

    www.hse.ru

  • 11.2012, .. 11.2012, ..

  • true false,

    if (x > 0) {

    x = x;

    }

    else {

    x = -x;

    }

    true false

    ,

    if (true) {

    Console.Write(11);

    }

    11.2012, ..

  • using System;

    class Program {

    static void Main() {

    double x; //

    double.TryParse(Console.ReadLine(),out x);

    /*

    X X, X >= 0

    -X, X < 0

    */

    if (x >= 0)

    Console.WriteLine("|x| = {0:f3}", x);

    else

    Console.WriteLine("|x| = {0:f3}", -x);

    }

    }

    11.2012, ..

  • using System;

    class Program {

    static void Main( ) {

    double a, b, x;

    Console.Write("a = ");

    double.TryParse(Console.ReadLine(), out a);

    Console.Write("b = ");

    double.TryParse(Console.ReadLine(), out b);

    if (a > b) { //

    a = a + b;

    b = a - b;

    a = a - b;

    }

    Console.Write("x = ");

    double.TryParse(Console.ReadLine(), out x);

    if (a

  • .. 1000 , --, 2004

    11.2012, ..

  • false

    true

    ,

    int i = 5;

    do {

    i--;

    } while (i > 1);

    i

    4 > 1 == true 4

    3 > 1 == true 3

    2 > 1 == true 2

    1 > 1 == false 1

    11.2012, ..

  • ?

    ,

    string line;

    double x;

    do {

    Console.Write(" : ");

    line = Console.ReadLine();

    } while (!double.TryParse(line, out x));

    , q:

    char ch;

    string line;

    do {

    Console.Write(" : ");

    line = Console.ReadLine();

    } while (!char.TryParse(line, out ch) ||

    ch != 'q);

    11.2012, ..

  • using System;

    class Program {

    static void Main() {

    double a, b, x;

    do {

    Console.Write("a = ");

    } while (!double.TryParse(Console.ReadLine(), out a));

    do {

    Console.Write("b = ");

    } while (!double.TryParse(Console.ReadLine(), out b));

    if (a > b) { //

    a = a + b;

    b = a - b;

    a = a - b;

    }

    do {

    Console.Write("x = ");

    } while (!double.TryParse(Console.ReadLine(), out x));

    if (a

  • /* .

    .*/

    using System;

    class Program {

    static void Main() {

    int currNum; //

    string str; //

    do {

    Console.Write(" : ");

    str = Console.ReadLine();

    currNum = int.Parse(str);

    if (currNum != 0)

    Console.WriteLine(" : " +

    currNum * currNum);

    }

    while (currNum != 0);

    Console.Write("\n Enter");

    Console.ReadLine();

    }

    }

  • false

    true

    ,

    int i = 5;

    while (i > 1) {

    i--;

    }

    i

    5 > 1 == true 5

    4 > 1 == true 4

    3 > 1 == true 3

    2 > 1 == true 2

    1 > 1 == false 1

    11.2012, ..

  • /* N .

    N . */

    using System;

    class Program {

    static void Main() {

    // C# !

    int n = 1, //

    N; //

    string str; //

    Console.Write(" : ");

    str = Console.ReadLine();

    N = int.Parse(str);

    while (n

  • .. 1000 , --, 2004

  • 11.2012, ..

    true

    false

    ,

    int i = 3;

    for (int j = 0; j < 7; j++) {

    i = j * 2;

    }

  • using System;

    class Program {

    static void Main() {

    uint n;

    do {

    Console.Write("n = ");

    } while(!uint.TryParse(Console.ReadLine(), out n)||n

  • 11.2012, ..

  • //

    ConsoleKeyInfo ;

    do { //

    /* . */

    /* */

    /* . */

    Console.WriteLine(" ESC!");

    = Console.ReadKey(true);

    } while (.Key != ConsoleKey.Escape);

    ?

    ConsoleKeyInfo Console.ReadKey(bool intercept)

    ( ). ConsoleKeyInfo. -Console.ReadKey(true).Key. ConsoleKey . intercept - .

    11.2012, ..

  • break, ,

    .

    break

    .

    .

    break ,

    .

    continue ( )

    .

    11.2012, ..

  • 21

    : int a = 7;

    int i = 0;

    while (a == 7) {

    if (i < 4)

    Console.Write(i++);

    else

    Console.Write(++i);

    break;

    }

    :

    0

  • // b = true

    .

    b ,

    if. else.

    10.

    // b = false

    11.2012, ..

  • i .

    ,

    i

    11.2012, ..

  • :

    789340

    Console.Write(),, , .

    11.2012, ..

  • -

  • -

    :

    - (-10)

    - (-2)

    - (-3)

    . .

    11.2012, ..

    .

    -10, -9, , 9, 10 -2.

  • 101000, , , ., . 20

    .: (495) 621-7983, : (495) 628-7931

    www.hse.ru

Recommended

View more >