The first large-scale examination of how computers affect the learning of mathematics in American classrooms found that when used selectively by trained teachers in middle schools they can significantly enhance academic performance.

But the study also indicated that their value in elementary school is far more limited and that when used primarily for drills and practice at either level, which is common, or when students spend long periods of time using them, computers in schools can be counterproductive.

The results imply that school districts should spend more on computers in middle schools than elementary schools and should focus attention on professional development for teachers to make sure they know how to use the computers effectively.

The study, called ''Does It Compute?'' released yesterday, was carried out by a researcher at Educational Testing Service in Princeton, N.J., and sponsored by the journal Education Week and the Milken Exchange on Education Technology, which promotes the effective use of computers in the classroom. It took the scores of thousands of fourth and eighth graders on the 1996 math portion of the National Assessment of Educational Progress, the most widely cited test given to samples of students across the country, and examined them based on surveys of students and teachers regarding classroom computer use.

Among eighth graders, it found that when students used computers for more complex mathematics like simulations, which measure changing variables, or applications, where data are manipulated and analyzed, gains of more than a third of an academic year were registered. For fourth graders using computers for mathematical games, the gain was a tenth of an academic year, or a few weeks' worth of instruction. The study divided computer use into four categories: drill and practice (common but least effective), demonstration of new topics (seldom used), as well as mathematical games, and simulations and applications.

By using a large sample size and controlling for such factors as socio-economic background, teacher preparedness, class size and school environment, the new study offers insight in the heated debate over the cost-benefit of the $5 billion spent annually on school computers.

''When computers are used to perform certain tasks, namely applying higher order concepts, and when teachers are proficient enough in computer use to direct students toward productive uses more generally, computers do seem to be associated with significant gains in mathematics achievement,'' concluded the report, written by Harold Wenglinsky.

It added: ''The effects of technology appear to be much smaller in the fourth than the eighth grade, and so may not be cost-effective. To the extent that the primary benefit of computers lies in applying higher order skills, there may not be much opportunity to benefit from using computers before middle school.''

Given the circumscribed and mixed nature of the findings, they are likely to be used by both sides in the debate over whether the investment has proven itself. In 1983, there was a computer for every 125 students whereas in 1995, the ratio was 1 to 9.

''What this tells me is that under certain conditions, uses of technology with certain kids in certain subjects at certain times will be effective,'' said Larry Cuban, an education professor at Stanford University and a longstanding skeptic toward the value of computers in schools. ''That kind of conditional statement can be made about virtually any approach to teaching. The question I continually ask is how else we could achieve the same results in a less costly way.''

But those who have advocated increased investment in education technology say they feel vindicated.

''What is unprecedented about this study is the way it takes a broad cross section of schools,'' said Christopher J. Dede, a professor of education and information technology at George Mason University in Virginia. ''It turns out that if you are at all in the ballpark about using technology correctly, there are some things that are happening that are good. It is not idiosyncratic to one teaching style or software vendor but something fundamental. Technology permits different kinds of pedagogy.''

The report describes technology uses among the 6,627 fourth graders and 7,146 eighth graders who took the core assessment in mathematics in 1996. It sought to avoid pitfalls of earlier studies on computer use by, for example, asking not about ratio of computer to student but frequency of use.

The study found that eighth graders and fourth graders use computers in school at about the same frequency. Less well off students have as much access to computers as better off students and, in fact, poor students had higher levels of computer use than their better off peers.

''Compared to white, non-poor, and suburban students, those who are minority, poor and urban find at least as much opportunity to use computers in school,'' the report said.

It added, ''It seems that policies to promote computer access in school have succeeded in eliminating inequities of this sort; yet inequities in teacher preparedness and what is taught using computers remains.''

The report measured teacher preparedness by whether teachers had received training in computer use but it did not distinguish between a weekend seminar and a semester-long course.

Photo: A study examined how computers affect learning. In Montclair, N.J., a young student sat at a terminal at the Bradford School. (Thomas Dallal for The New York Times)