Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
More Operators
Control Statements
http://www.physics.gla.ac.uk/~kaiser/
++ x++ ; x = x + 1;
-- x-- ; x = x - 1;
+= x += 2; x = x + 2;
-= x -= 2; x = x - 2;
*= x *+= 2; x = x * 2;
/= x /+= 2; x = x / 2;
%= x %+= 2; x = x % 2;
result is 5 result is 6
5 + 6 = 11
++
1 * 3
2 * 5 value
value
10 + 3 = 13
simple if statement:
if (condition)
statement;
If the condition is true
(non-zero), the statement will be
executed.
if - else if - else
statement: If the condition is false (0), it will
not be executed.
if (condition1){
statement1; Multiple statements may be in
statement2;
curly braces.
} else if (condition2){
statement3; else allows a statement to be
statement4;
} else
executed if the condition is not
statement5; fulfilled.
int main()
{
printf("Enter number of dollars owed:");
fgets(line, sizeof(line), stdin);
sscanf(line, "%d", &balance_owed);
if (balance_owed = 0)
printf("You owe nothing.\n");
else
printf("You owe %d dollars.\n", balance_owed);
return (0);
}
Output:
Enter number of dollars owed:42
You owe 0 dollars.
int main()
{
/* start things out */
old_number = 1;
current_number = 1;
printf("%d\n", current_number);
next_number = current_number + old_number;
old_number = current_number;
current_number = next_number;
}
return (0);
}
so the first Fibonacci numbers are 1, 1, 2, 3, 5, 8,.... They
appear in nature e.g. in the number of left- and right-turning
spirals on the bottom of some pine cones.
In our C code, the above equation is implemented as
old_number = 1;
current_number = 1;
if (item == 0) Output:
break; Enter # to add
or 0 to stop:-2
if (item < 0) { Enter # to add
++minus_items; or 0 to stop:22
continue; Total: 22
} Enter # to add
total += item; or 0 to stop:1459890
printf("Total: %d\n", total); Total: 1459912
} Enter # to add
printf("Final total %d\n", total); or 0 to stop:0
printf("with %d negative items omitted\n", Final total 1459912
minus_items); with 1 negative items omitted