N
N
Nikita Salnikov2020-04-21 10:56:09
C++ / C#
Nikita Salnikov, 2020-04-21 10:56:09

What is the logic between different operators?

int x = 0;
string poem = "";

while (x<4) 
{
poem = poem + "a";

if (x<1){
poem = poem + "";
}
poem = poem +"n";
if (x>1) {
poem = poem + " oyster";
x= x+2;
}
if (x==1){
poem = poem + "noys";
}
if (x<1){
poem = poem + "oise";
}
x = x+1;
}
output.Text = poem;

The code prints the message a noise annoys an oyster.
How to understand how these Xs relate to these "poem"? What is the connection between them? How to solve such problems?

PS the question itself is based on the book the
5e9ebbe181e53877445404.jpeg

answer is based on the book
5e9ebc1349240631454587.jpeg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
eRKa, 2020-04-21
@kttotto

Nikita Salnikov ,

there are numbers then the answer must be numbers

Wrong. The condition can be a number, a string, or both (an object), and depending on the fulfillment of the condition, the operation can also be performed on anything (a number, a string, etc.). Roughly speaking, "if there is one, then I will add 'a' to the end of the line, if there are two, then I will add 'b' to the end of the line". Maybe vice versa: "if 'one' I will add 2 to the result, if 'two', I will add 4", etc.
The whole problem could be rewritten like this
var x = 0;
var result = "";
while (x < 30) {
  if(x == 0 || x == 8 || x == 15) {
    result += "a";
  }
  if(x == 1 || x == 7) {
    result += " ";
  }
  if(x == 2 || x == 9 || x == 10 || x == 16) {
    result += "n";
  }
  
  ... // и т.д.
  x++;
}

where x is just the ordinal number of the letter in the string. The meaning is the same, to find the dependence of the location of the parts of the string in the whole string.
Such dependences, in this case, can be found arbitrarily. The author of the book offered his own version, he simply confused the conditions so that it would be more interesting to look for an answer.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question