 Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Objective-C.Programming.pdf
Скачиваний:
11
Добавлен:
21.02.2016
Размер:
8.64 Mб
Скачать Chapter 6 Numbers

int x = 5;

x += 5; // x is 10

You can think of the second line as “assign x the value of x + 5.” In addition to +=, there is also -=, *=, /=, and %=.

To get the absolute value of an int, you use a function instead of an operator. The function is abs(). If you want the absolute value of a long, use labs(). Both functions are declared in stdlib.h:

#include <stdio.h>

#include <stdlib.h>

int main (int argc, const char * argv[])

{

printf("3 * 3 + 5 * 2 = %d\n", 3 * 3 + 5 * 2);

printf("11 / 3 = %d remainder of %d \n", 11 / 3, 11 % 3); printf("11 / 3.0 = %f\n", 11 / (float)3);

printf("The absolute value of -5 is %d\n", abs(-5));

return 0;

}

Floating-point numbers

If you need a number with a decimal point, like 3.2, you use a floating-point number. Most programmers think of a floating-point number as a mantissa multiplied by 10 to an integer exponent. For example, 345.32 is thought of as 3.4532 x 102. And this is essentially how they are stored: a 32-bit floating number has 8 bits dedicated to holding the exponent (a signed integer) and 23 bits dedicated to holding the mantissa with the remaining 1 bit used to hold the sign.

Like integers, floating-point numbers come in several sizes. Unlike integers, floating-point numbers are always signed:

 float g; // 32-bits double h; // 64-bits long double i; // 128-bits

Tokens for displaying floating-point numbers

printf() can also display floating point numbers, most commonly using the tokens %f and %e. In main.c, replace the integer-related code:

int main (int argc, const char * argv[])

{

double y = 12345.6789; printf("y is %f\n", y); printf("y is %e\n", y);

return 0;

}

When you build and run it, you should see:

y is 12345.678900 y is 1.234568e+04

So %f uses normal decimal notation, and %e uses scientific notation.

46 ( & &%

Q) + < & ' ! ( & ') ! C ' + ' & * 7

! " #\$%

&

? I 975=6 4:8T,

'?0 ,+ '! ?%,

'?0 ,+ '! ?%,

-,

.

B ) ) ) ) 7

? 975=6 48 ? 9 75 J-=

-# ' ( (& + # '

( J " ; + ) + ) ; ! & ) + & ' ) + ' ! ( + G ' ) ? ! K ) + )'' & " ' 7 ' ) ; =) ) !

& ) ) & ' &) ) ) ) ' & 7

> + 7 & &) V

&&

\$' V & ! % + )' )' ! )-8=9!

!.

7

Loops

In Xcode, create yet another new project: a C Command Line Tool named Coolness.

The first program I ever wrote printed the words, “Aaron is Cool”. (I was 10 at the time.) Write that program now:

#include <stdio.h>

int main(int argc, const char * argv[])

{

printf("Aaron is Cool\n"); return 0;

}

Build and run the program.

Let’s suppose for a moment that you could make my 10-year-old self feel more confident if the program printed the affirmation a dozen times. How would you do that?

Here’s the dumb way:

#include <stdio.h>

int main(int argc, const char * argv[])

{

printf("Aaron is Cool\n"); printf("Aaron is Cool\n"); printf("Aaron is Cool\n"); printf("Aaron is Cool\n"); printf("Aaron is Cool\n"); printf("Aaron is Cool\n"); printf("Aaron is Cool\n"); printf("Aaron is Cool\n"); printf("Aaron is Cool\n"); printf("Aaron is Cool\n"); printf("Aaron is Cool\n"); printf("Aaron is Cool\n"); return 0;

}

The smart way is to create a loop.

The while loop

The first loop we’ll use is a while loop. The while construct works something like the if construct we discussed in Chapter 4. You give it an expression and a block of code contained by curly braces. In the

49

Тут вы можете оставить комментарий к выбранному абзацу или сообщить об ошибке.

Оставленные комментарии видны всем.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]