View Single Post
Old 05-20-2009, 03:09 PM   #15 (permalink)
kmote
 

Join Date: Jul 2005

Location: England

Posts: 2,198

kmote has a spectacular aura aboutkmote has a spectacular aura about

Default Re: the tricky business of programming

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;
    }
}

__________________
MSI P43 Neo|Enermax Pro82+ 425W|E5200|silent 8500GT|250GB Samsung spinpoint F1|Samsung SATA DVD RW|4GB Corsair|Antec SOLO|openSUSE11


There are in order of increasing severity: lies, darn lies, statistics, and computer benchmarks. - diskinfo man page
kmote is online now   Reply With Quote