Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,688 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
210,850 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.