Scrigroup - Documente si articole

     

HomeDocumenteUploadResurseAlte limbi doc
AccessAdobe photoshopAlgoritmiAutocadBaze de dateCC sharp
CalculatoareCorel drawDot netExcelFox proFrontpageHardware
HtmlInternetJavaLinuxMatlabMs dosPascal
PhpPower pointRetele calculatoareSqlTutorialsWebdesignWindows
WordXml

AspAutocadCDot netExcelFox proHtmlJava
LinuxMathcadPhotoshopPhpSqlVisual studioWindowsXml

A program to find prime numbers

c



+ Font mai mare | - Font mai mic



A program to find prime numbers

/*
*
* Dumb program that generates prime numbers.
*/
#include <stdio.h>
#include <stdlib.h>

main()
else
divisor = divisor-1;
}

if(not_prime == 0)
printf('%d is a prime numbern', this_number);
this_number = this_number + 1;
}
exit(EXIT_SUCCESS);
}

Example 1.2



What was interesting in there? A few new points, perhaps. The program works in a really stupid way: to see if a number is prime, it divides that number by all the numbers between half its value and two-if any divide without remainder, then the number isn't prime. The two operators that you haven't seen before are the remainder operator , and the equality operator, which is a double equal sign . That last one is without doubt the cause of more bugs in C programs than any other single factor.

The problem with the equality test is that wherever it can appear it is also legal to put the single sign. The first, , compares two things to see if they are equal, and is generally what you need in fragments like these:

if(a == b)
while (c == d)

The assignment operator is, perhaps surprisingly, also legal in places like those, but of course it assigns the value of the right-hand expression to whatever is on the left. The problem is particularly bad if you are used to the languages where comparison for equality is done with what C uses for assignment. There's nothing that you can do to help, so start getting used to it now. (Modern compilers do tend to produce warnings when they think they have detected 'questionable' uses of assignment operators, but that is a mixed blessing when your choice was deliberate.)

There is also the introduction for the first time of the if statement. Like the while statement, it tests an expression to see if the expression is true. You might have noticed that also like the while statement, the expression that controls the if statement is in parentheses. That is always the case: all of the conditional control of flow statements require a parenthesized expression after the keyword that introduces them. The formal description of the if statement goes like this:

if(expression)
statement

if(expression)
statement
else
statement

showing that it comes in two forms. Of course, the effect is that if the expression part is evaluated to be true, then the following statement is executed. If the evaluation is false, then the following statement is not executed. When there is an else part, the statement associated with it is executed only if the evaluation gives a false result.

If statements have a famous problem. In the following piece of code, is the statement-2 executed or not?

if(1 > 0)
if(1 < 0)
statement-1
else
statement-2

The answer is that it is. Ignore the indentation (which is misleading). The else could belong to either the first or second if, according to the description of the if statement that has just been given, so an extra rule is needed to make it unambiguous. The rule is simply that an else is associated with the nearest else-less if above it. To make the example work the way that the indentation implied, we have to invoke a compound statement:

if(1 > 0)
else
statement-2

Here, at least, C adheres to the practice used by most other languages. In fact a lot of programmers who are used to languages where the problem exists have never even realized that it is there-they just thought that the disambiguating rule was 'obvious'. Let's hope that everyone feels that way.



Politica de confidentialitate | Termeni si conditii de utilizare



DISTRIBUIE DOCUMENTUL

Comentarii


Vizualizari: 846
Importanta: rank

Comenteaza documentul:

Te rugam sa te autentifici sau sa iti faci cont pentru a putea comenta

Creaza cont nou

Termeni si conditii de utilizare | Contact
© SCRIGROUP 2024 . All rights reserved