static void Main (string [] args)
            int size = int.Parse (Console.ReadLine ());
            for (int i = 0;i<size;i ++)
                for (int j = 0;j<size;j ++)
                    if (i == j || (size --i --1) == j)
                        Console.Write ("X");
                    else else
                        Console.Write ("");
                Console.WriteLine ();

This code is diverted from the site, it is a program that inputs a numerical value indicating the size and displays the x mark of that size with some characters, but I can not understand why the if block part is like this. Please professor.

Obtained from: http://kitako.tokyo/lib/CsExercise.aspx?id=5

  • Answer # 1

    I searched. (Lol)

    The content of the question is

    C # exercises 6-4

    However, the written answer code is

    Java Exercises 4 --21

    Isn't it?
    The written code is the answer that outputs a cross

  • Answer # 2

    I don't understand why the if block looks like this

    AppropriatelysizeAssuming the value of

    i = 0soj = 0When, X is displayed because the condition of if is satisfied.

    i = 0soj = 1At that time ...


    i = size-1soj = size-1At that time ...

    If you check it, you can understand the meaning of the conditional expression.

    For examplesize = 5Assuming, there are 25 patterns in total, so you can check it in 3 to 5 minutes.

    If you still don't understand
    Think of it as "math (mathematics? I don't know which category it is)".

    if (i == j || (size --i --1) == j)

    It is a variable name that is familiar in arithmetic (mathematics) individually for the two conditions written in.xWhenyIf you write using

    y = x

    (size --y --1) = x(Here size is a constant)

    The formula.
    Both are just linear functions (straight lines) (it is natural because the "x" can be drawn with two lines), so if you draw this graph, you will understand the meaning.

Related articles