none
C# Can every statement be an expression? RRS feed

  • Question

  • Can every statement be an expression in C#? For example I know that a method invocation statement can be used as an expression and because of that I can do stuff like this:

    for (int i = 0; i < 10; Console.WriteLine(i++));

    But is it the case with every statement?

    But this thing for some reason doesn't work with the while loop

    while (Console.WriteLine(1) > 0) { }

    This code gives an error. Maybe you can explain what's happening? I got a bit confused.

    Monday, February 12, 2018 8:35 PM

All replies

  • Console.WriteLine method has result type void. So you cannot use it in while condition because of you would compare void with int value. Console.WriteLine(i++) in for cycle works because of i<10 is condition. For cycle has last parameter iterator. I could be "everything" and it is not mandatory (could be empty). Iterator parameter could be function calling, lambda expression, etc... There could be many expressions separated by comma (,). It is about definition what is behavior after one iteration is finished.

    You must write while cycle another:

    int i = 0;

    while(i++ <= 0)

    {

        Console.WriteLine(i);

    }

    How for cycle works:

    1) i is initialize to 0.

    2) There is condition i < 10 - it is true => go to body

    3) Body is empty, go to iterator

    4) Write to Console value of i (0)

    5) Set i as i+1

    6) Go to step 2. 

    While cycle doesn't have iterator section so it works:

    1) Initialize must be outside of while cycle. 

    2) Check if condition is true (same as step 2 of for cycle)

    3) If true, go to body

    4) Go to step 2. 

    In while cycle is not section where function could be called instead of for cycle. 

    You could use function calling in example if you want to read from file:

    string line=string.Empty;

    while((line = textReader.ReadLine())!=null)

    {

    }

    But result of calling textReader.ReadLine is saved into line variable and it is compare with null. But Console.WriteLine doesn't result value. What could you compare?

    • Edited by Petr B Monday, February 12, 2018 9:10 PM
    Monday, February 12, 2018 8:56 PM
  • But this also works: 

    static void Main(string[] args)
            {
                int i = 0;
                for (Console.WriteLine(i); i < 10; i++)
                {

                }
        }

    Why?



    Monday, February 12, 2018 9:08 PM
  • Try read this:

    https://docs.microsoft.com/en-US/dotnet/csharp/language-reference/keywords/for

    A then try:

    int i = 0;

    for(; Console.WriteLine(i); i++);

    There is error. Why?

    Initializer section has same possibility as iterator section. But "the condition section contains a boolean expression that’s evaluated to determine whether the loop should exit or should run again."

    Monday, February 12, 2018 9:29 PM
  • The following is the format of the for statement:

    for (initializer; condition; iterator)
    body

    The initializer and the iterator are statements; their value is ignored by the for statement. The condition is a Boolean expression; its value determines whether the loop continues.



    Sam Hobbs
    SimpleSamples.Info

    Monday, February 12, 2018 10:26 PM
  • Greetings NentoR.

    The key to understanding here is that when you have a test for "is something greater than zero" (or less that ten, or whatever), the "something" must be a valid number. If "something" is void, you will get a compiler error.

    Consider the following methods.

    // A method that returns a number.
    int DoSomething1()
    {
       return 1;
    }
    
    
    // A method that returns void.
    void DoSomething2()
    {
    }

    We can use the first method wherever an integer is required, but not the second.

    // These all work
    while(DoSomething1() > 0)
    {
    }
    
    for(int i = 0; DoSomething1() > 0; i++)
    {
    }
    
    if(DoSomething1() > 0)
    {
    }
    
    
    // But these will all produce an error.
    while(DoSomething2() > 0)
    {
    }
    
    for(int i = 0; DoSomething2() > 0; i++)
    {
    }
    
    if(DoSomething2() > 0)
    {
    }

    Console.WriteLine returns void.

    Does that help?

    Monday, February 12, 2018 11:29 PM