the tricky business of programming

^ If you are using a compiler. For Javascript you can use Notepad, and some people do. Javascript doesn't get compiled, like the other languages mentioned. The browser "renders" it, but Javascript doesn't get compiled. It is not a stand-alone application, just code used within HTML in the form of a script.

^^ Yes the moral of the thread is be careful, but don't let it end like that. I was just trying to think of some obvious mistakes.

Things that aren't technical errors, but aren't what you want to do (like the = and == functions)

^ Yes that would be an error, if you try to say:

If (someVariable = someOtherVariable) {
// the code here wouldn't work if, if "someVariable were equal
//to 5 and someOtherVariable = 5 as well.
}

That is because the code is NOT COMPARING the two values. It is trying to make
someVariable receive the value of someOtherVariable, not compare the two.
What would be needed in this case would be the == .

So you are wrong, Soulphire. Sorry. Unless you can show me what you mean, with an example.
 
^ If you are using a compiler. For Javascript you can use Notepad, and some people do. Javascript doesn't get compiled, like the other languages mentioned. The browser "renders" it, but Javascript doesn't get compiled. It is not a stand-alone application, just code used within HTML in the form of a script.

^^ Yes the moral of the thread is be careful, but don't let it end like that. I was just trying to think of some obvious mistakes.



^ Yes that would be an error, if you try to say:

If (someVariable = someOtherVariable) {
// the code here wouldn't work if, if "someVariable were equal
//to 5 and someOtherVariable = 5 as well.
}

That is because the code is NOT COMPARING the two values. It is trying to make
someVariable receive the value of someOtherVariable, not compare the two.
What would be needed in this case would be the == .

So you are wrong, Soulphire. Sorry. Unless you can show me what you mean, with an example.

What soulphire is saying (and he is right) is that
Code:
if (something = another)
is not a syntax error, put that into a compiler and it will be fine (which is what makes it is so tricky). If the compiler produced an error as in the case of a syntax mistake eg. a missing '}' or ';' then it wouldn't be such a problem.
Interestingly enough, according to standards (note that I have not read the standard myself to verify this) javascript does not require semicolons though they are considered good practice. And while you are right to note that javascript is an interpreted language and as such is not compiled, you can usually set the browser to pop up with an error if there is a problem.

EDIT: if anyone wants an example of why this isn't a syntax error (although arguably it should be) then I would be more than happy
 
Kmote pal - I think any example you can post would be helpful to newbies **** i am learning stuff from it I am a self taught coder so I have a few bad habits an didn't realise i did until i saw this thread.
 
Ask and ye shall receive, here is a fairly lame example of why the single = is legal in an if. In c:
Code:
#include <stdio.h>
#include <stdlib.h>

#define LIMIT 14

int doSomething()
{
	return 11;
}

int main(void)
{
	int aNumber;
	
	if((aNumber = doSomething()) > LIMIT)
	{
		printf("%d is greater than %d", aNumber, LIMIT);
	}
	else
	{
		printf("%d is less than %d", aNumber, LIMIT);
	}
	return EXIT_SUCCESS;
}

and because the inequality operators "return" a boolean the same applies to java:
Code:
public class Main {

    private static int LIMIT = 14;

    public static void main(String[] args)
    {
        int aNumber;
        
        if((aNumber = doSomething()) > LIMIT)
        {
            System.out.format("%d is greater than %d\n", aNumber, LIMIT);
        }
        else
        {
            System.out.format("%d is less than %d\n", aNumber, LIMIT);
        }
    }

    private static int doSomething()
    {
        return 20;
    }
}
 
Interestingly enough, according to standards (note that I have not read the standard myself to verify this) javascript does not require semicolons though they are considered good practice. And while you are right to note that javascript is an interpreted language and as such is not compiled, you can usually set the browser to pop up with an error if there is a problem.

You are correct, Javascript does not require semicolons. I believe it treats a new line as an end of the previous statement. However I always use semicolons, just out of habit from other languages.

And you are also right again, Javascript problems can be debugged with the browser. If you are using Firefox I highly recommend the Firebug extension. You can step-by-step crawl Javascript code, insert breaks, and so on to debug and find out where things went wrong.
 
Yes, however....

I gave the example of Javascript where that would not work, I can prove it when I get time.

You are correct, Javascript does not require semicolons. I believe it treats a new line as an end of the previous statement. However I always use semicolons, just out of habit from other languages.

Wel I have had code go all haywire without them, and I know that was it because when I put the semicolons in it all worked ok.
 
Well apparently semicolons are not necessary as I went back and tried a few things after posting here. Thing is, I know in the past I have had problems not putting them in ... I suppose it must have been a different reason other than a missing semicolon.
 
Back
Top Bottom