y

Yazılım

1 member
Trying to predict the ways that a program can fail or produce unwanted results and then taking preventive measures to account for such situations is a necessary part of producing good, reliable programs. Running a sufficient number of test cases against a program often points the finger to portions of the program that do not account for certain cases. But it goes further than that. It must become a matter of self-discipline while coding a program to always say “What would happen if ...” and to insert the necessary program statements to handle the situation properly.
== ve =
Remember that the double equal sign == is the equality test and the single equal sign is the assignment operator. It can lead to lots of headaches if you forget this and inadvertently use the assignment operator inside the if statement.
Reklam
printf içinde şartlı operatör
It is not necessary that the conditional operator be used on the right-hand side of an assignment—it can be used in any situation in which an expression could be used. This means that you could display the sign of the variable number, without first assigning it to a variable, using a printf statement as shown: printf ("Sign = %i\n", ( number < 0 ) ? –1 : ( number == 0 ) ? 0 : 1);
şartlı operatörle else if yaratmak
sign = ( number < 0 ) ? -1 : (( number == 0 ) ? 0 : 1); If number is less than zero, sign is assigned the value –1; else if number is equal to zero, sign is assigned the value 0; else it is assigned the value 1.The parentheses around the “else” part of the preceding expression are actually unnecessary.This is because the conditional operator associates from right to left, meaning that multiple uses of this operator in a single expression, such as in e1 ? e2 : e3 ? e4 : e5 group from right to left and, therefore, are evaluated as e1 ? e2 : ( e3 ? e4 : e5 )
şartlı operatör, conditional operator
The general format of the conditional operator is condition ? expression1 : expression2 where condition is an expression, usually a relational expression, that is evaluated first whenever the conditional operator is encountered. If the result of the evaluation of condition is TRUE (that is, nonzero), then expression1 is evaluated and the result of the evaluation becomes the result of the operation. If condition evaluates FALSE (that is, zero), then expression2 is evaluated and its result becomes the result of the operation. The conditional operator is most often used to assign one of two values to a variable depending upon some condition. For example, suppose you have an integer variable x and another integer variable s. If you want to assign –1 to s if x were less than zero, and the value of x^2 to s otherwise, the following statement could be written: s = ( x < 0 ) ? -1 : x * x;
değilleme operatörü, negation operator. ( ! )
To easily test if the value of a flag is FALSE, you can use the logical negation operator, !. In the expression if ( ! isPrime ) the logical negation operator is used to test if the value of isPrime is FALSE (read this statement as “if not isPrime”). In general, an expression such as ! expression negates the logical value of expression. So if expression is zero, the logical negation operator produces a 1. And if the result of the evaluation of expression is nonzero, the negation operator yields a 0. The logical negation operator can be used to easily “flip” the value of a flag, such as in the expression myMove = ! myMove; As you might expect, this operator has the same precedence as the unary minus operator, which means that it has higher precedence than all binary arithmetic operators and all relational operators. So to test if the value of a variable x is not less than the value of a variable y, such as in ! ( x < y ) the parentheses are required to ensure proper evaluation of the expression. Of course, you could have equivalently expressed the previous expression as x >= y
Reklam
48 öğeden 11 ile 20 arasındakiler gösteriliyor.