C# C# Basics (Retired) Perform Converting Strings to Integers

Why is code sometimes read from left to right and then others from right to left?

using System;
namespace Treehouse.FitnessFrog
{
    class Program
    {
        static void Main()
        {
            int runningTotal = 0; /*read left to right: we're making an int variable called runningTotal and assigning it the value 0 */
            Console.Write("Enter how many minutes you exercised: "); //read left to right
            string entry = Console.ReadLine();        //read left to right
            int minutes = int.Parse(entry);  //read RIGHT side of the equals sign to LEFT?  
            runningTotal = runningTotal + minutes; //read the RIGHT side of the equals sign to LEFT for reasons unclear to me
            Console.WriteLine("You've entered " + runningTotal + " minutes."); //read left to right
        }
    }
}

Jeremy discusses in the video this is referencing that runningTotal = runningTotal + minutes; is read from right side of the equals sign to the left, but I'm not clear why we're suddenly interpreting the code from right to left vs. left to right like all the other code we've dealt with so far. This is confusing, and I'm unclear what circumstances are read right to left vs. left to right. Can someone clear this up for me? Thanks!

2 Answers

Simon Coates
Simon Coates
28,677 Points

ok, keep in mind i really don't know what i'm talking about, but I think it might be a function of operators. Operator precedence determines which bits get evaluated first, and in some languages at least, the operators seems to come with an idea of direction to read. I had a quick google and saw this article on c#, which might be on topic: https://msdn.microsoft.com/en-us/library/aa691323(v=vs.71).aspx

Steven Parker
Steven Parker
200,416 Points

:+1: Good job, Simon, even if you "don't know what you're talking about". Your page reference is precisely on topic, and the "idea of direction to read" is known as associativity. That MSDN page covers both.

Avan Sharma
Avan Sharma
7,652 Points

Precedence of Operators. In case of assignment operation, always right side of the code is calculated first and is assigned to left side.

int a = 4; Here 'a' is declared first and value of 4 is assigned from right side to left.