Home>

### c # - i can't understand the conditional expression of the if block

``````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
http://kitako.tokyo/lib/CsExercise.aspx?id=106

However, the written answer code is

Java Exercises 4 --21
http://kitako.tokyo/lib/JavaExercise.aspx?id=104

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

Appropriately`size`Assuming the value of

`i = 0`so`j = 0`When, X is displayed because the condition of if is satisfied.

`i = 0`so`j = 1`At that time ...

...

`i = size-1`so`j = size-1`At that time ...

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

For example`size = 5`Assuming, 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.`x`When`y`If 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.