上一帖看的十分痛苦,又臭又长象流水账。
至于第一次写这种东西是酱紫了,只得慢慢改进。
额,数据类型,算数表达式,if…else,for。
下面几帖就整理了数据类型,算数表达式,
还有初中数学课的神马if…else,for.汗颜!!
提供下相关代码,请戳右边_>testProduct_3
发现对文字真没好感,表达能力有问题,口吃,智障。
【注意】本次代码都是写在 main 下
[objc]
int i = 1; //整型
float f = 1.01;//浮点型
double d = 1.01e10;//双精度浮点型
char myChar = ‘A’;//单个字符
//输出格式符
NSLog (@"int = %d",i);
NSLog (@"float = %f",f);
NSLog (@"double = %g",d);
NSLog (@"double = %f",d);
NSLog (@"char = %c",myChar);
[/objc]
1、变量、常量
变量和常量关係:
变量,来源于数学,是计算机语言中能储存计算结果或能表示值抽象概念。
说白就是存储数据的值的空间。
而顾名思义,任何数字或某个字符,字符串通常称之为常量。
如果文字说不通,还是看代码好了。
以int i = 1; 为例
_>int 则是数据类型
_>i 则是整型变量,变量名为i
_>1 是一个常量
2、数据类型
上面的例子中,提到 int 则是数据类型,那么数据类型又是什么鬼东西.
[下面内容纯属收刮来的_>]
数据类型,可以说是一个值的集合和定义在这个值集上一组操作的总称。
数据类型的出现是为了把数据分成所需内存大小不同的数据,
编程的时候需要用大数据的时候才需要申请大内存,就可以充分利用内存。
例如熊熊必须睡双人床,就给他双人床,而猴子单人床就够了。
[<_上面内容纯属收刮来的]
以上方手写代码为例。
2.1 int类型
_>int 类型,也叫整型,也就是有符号 32 位整数。
【注意】一个整数在计算机上占用32位,或者64位。这取决于运行的计算机。
如:你在32位下编译,则是占用32位。
这些都是整型:100 -100 0144 0x64
特别留意后面两个:0144 0x64
这里0开头,就是表示八进制,0x开头,则表示16进制
为此可以实验下,复制以下代码:
[objc]
NSLog(@"0144 = %d",0144);
NSLog(@"0x64 = %d",0x64);
[/objc]
输出如下:
[objc]
2014-03-11 10:41:53.152 testProduct[1246:303] 0144 = 100
2014-03-11 10:41:53.153 testProduct[1246:303] 0x64 = 100
[/objc]
2.2 float类型
_>float ,浮点型,他跟整型的区别是,他可以存储包含小数数值。
也就是说要区分浮点常量,看下有没小数点就行了。
这些都是浮点型:10.1f -10.1
2.3 double类型
_>double ,双精度浮点型,与单精度数据类型(float)相似,
但精确度比float高,编译时所占的内存空间依不同的编译器也有所不同。
单精度浮点数占4字节(32位)内存空间,其数值范围为3.4E-38~3.4E+38,;
双精度型占8 个字节(64位)内存空间,其数值范围为1.7E-308~1.7E+308。
上面1.01e10,其实就是小学数学课上的科学计数法就是1.01*10^10
2.4 char类型
_>char ,存储单个字符。
如上方例子,则表示字母A,这里有点要注意如:
char myChar = ‘1’; 这里表示的是数字1,不是数值1。
2.5 long 、 long long
附带一些限定词:
当int溢出的时候,就可以用到long、long long
这时候将就有扩展值域的效果,使用如下:
long int i;
long long int i;
long double i;
2.6 short
当然有long,就有个short。
作用则是相反是用来存储相当小的值。还是为了节约内存。
使用如下:
short int i;
2.7 signed 、 unsigned
附带signed和unsigned,则是表示带不带符号。
平时写的int,都是signed,也就是有符号的整型。
比如16位系统中一个int能存储的数据的范围为-32768~32767,
而unsigned能存储的数据范围则是0~65535。
3、算术表达式
复制如下代码:
[objc]
printf("\n");// \n 为换行符
//算数表达式
//二元运算符
int x = 5 ,y = 2;
NSLog(@"5 + 2 = %i",x + y);//加
NSLog(@"5 – 2 = %i",x – y);//减
NSLog(@"5 * 2 = %i",x * y);//乘
NSLog(@"5 / 2 = %i",x / y);//除
NSLog(@"5 %% 2 = %i",x % y);//模,取余
[/objc]
这里说下\n,我忘记我有没说过,额就是 换行符 的意思。
3.1 二元运算符
关于二元运算符,应有两个操作数,
由两个操作数加一个二元算术运算符可构成一个算术表达式。
例如平时的:加减乘除,这里还有一个取余数的模。
模用 % 表示,例如5%2,就是5/2=2…..1 (余1),
输出的时候,需要输出 百分号,则再紧跟一个 百分号 即可。
具体看输出结果
[objc]
2014-03-11 13:59:42.992 testProduct[2760:303] 5 + 2 = 7
2014-03-11 13:59:42.992 testProduct[2760:303] 5 – 2 = 3
2014-03-11 13:59:42.993 testProduct[2760:303] 5 * 2 = 10
2014-03-11 13:59:42.993 testProduct[2760:303] 5 / 2 = 2
2014-03-11 13:59:42.993 testProduct[2760:303] 5 % 2 = 1
[/objc]
3.2 类型转换
恩。看了下,貌似输出出来没啥不对。
但是5/2不是等于2.5么?!
这里就要说道下面一点,由于5和2都是整型,
所以运算就将在整型运算的规则下进行。
这里用代码来进行验证下,代码如下:
[objc]
//整型 && 浮点 类型转换
int z = 3.141592654;
float t = 1;
NSLog(@"z = %d , t = %f",z,t);
NSLog(@"5 / 2.0f = %0.1f",x / 2.0f );
//类型转换
NSLog(@"5 / 2 = %f",(float)x / y );
[/objc]
_>第一行,定义了个 整型,将浮点值赋给整型变量,得到的是小数部分都被删节了。
_>第二行,定义了个 浮点型,将整型值赋给浮点变量,没有任何变化。
_>第四行,将整型变量,与浮点型进行运算,前面讲过,
2个整型运算的规则下进行,运算就将在整型运算的规则下进行。
现在换了一个浮点型,也就能正确输出。
可以看到这里编译器主动提示输出格式用%f

_>第四行的 %0.1f ,意思则是,保留小数点1位。
_>第五行,将2个整型进行运算,最后却可以输出?主要在于(float)x
他是类型转换运算符,将变量X的值转成float型,所以最后可以正确输出。
3.3 运算符优先级
这部分就不用过多文字解释,直接看代码即可。
跟上基础数学是差不多的
[objc]
//运算符优先级
NSLog(@"5 + 2 * 2 = %i",x + y * y);//乘法优先加法
NSLog(@"5 / 5 – 2 / 2 = %i",x / x – y / y );//除法优先减法
NSLog(@"5 %% 2 * 2 = %i",x % y * y );//模与乘除一样
//括号是可以改变运算顺序的:
NSLog(@"5 * (5 – 2) / 2 = %i",x * (x – y) / y );
NSLog(@"5 %% (2 * 2) = %i",x % (y * y) );
[/objc]
3.4 一元运算符
只需要一个操作数的运算符称为一元运算符,也称单目运算符。
如: 逻辑非 ! 自增 ++ 自减 — 一元减 – 按位求反 ~
上代码:
[objc]
//一元运算符
//如: 逻辑非 ! 自增 ++ 自减 — 一元减 – 按位求反 ~
NSLog(@"!0 = %i , !1 = %i",!0,!1);
//" !0" 这个逻辑表达式的值为1.(判断的这个数为0,成立,则其表达式的值为1)
//" !1" 这个逻辑表达式的值为0.(判断的这个数非0,不成立,则其表达式的值为0)
//一元减
NSLog(@"-i : %i",-i);
//自增 && 自减
NSLog(@"i = %i",i);
NSLog(@"++i : %i",++i);
NSLog(@"–i : %i",–i);
//i++ && ++i 区别
NSLog(@"i = %i",i);
NSLog(@"i++ : %i",i++);
NSLog(@"i– : %i",i–);
//按位求反 ~
NSLog(@"~0 : %d",~0);
//即将0变1,将1变0
//至于为啥是-1,这关系到符号位
[/objc]
_>首行,逻辑非,代码已经有详解了
_>接下来是 一元减,额这个貌似也没有我说的必要
_>关于自增和自减,也既++和–,意思就是每次自己加1或者减1.
例如i++,意思就是i=i+1,i–则是i=i-1
输出结果如下:
[objc]
2014-03-11 15:09:05.676 testProduct[3217:303] i = 1
2014-03-11 15:09:05.676 testProduct[3217:303] ++i : 2
2014-03-11 15:09:05.677 testProduct[3217:303] –i : 1
2014-03-11 15:09:05.677 testProduct[3217:303] i = 1
2014-03-11 15:09:05.677 testProduct[3217:303] i++ : 1
2014-03-11 15:09:05.678 testProduct[3217:303] i– : 2
[/objc]
当代码运行时,你会发现i++和++i输出不同。
首先不管i++,还是++i都是i=i+1的意思,
区别在于i++是i先不自加,在语句完后自加,
而++i先自加,再执行语句。
以上面代码为例,
默认 i 是为 1
++i,的时候 i 为 1,i 先自加,再NSLog则 i 为 2
–i,的时候 i 为 2,i 先自减,再NSLog则 i 为 1
此时再输出i 为 1
i++,的时候 i 为 1,i 先执行NSLog,输出 i 为 1,再自加 此时 i 为 2
–i,的时候 i 为 2,i 先执行NSLog,输出 i 为 2,再自减 此时 i 为 1
_>按位求反,这个稍微知道下就行了,例如0的时候,转为二进制,
然后把0变成1,1变成0,之后再转回十进制。
而这里面为啥输出-1.主要原因在于符号位
3.5 赋值运算
[objc]
//赋值运算
NSLog(@"i = %i",i);
NSLog(@"i += : %i",i += 2);//等价于 i = i + 2;
NSLog(@"i -= : %i",i -= 2);//等价于 i = i – 2;
NSLog(@"i = %i , x = %d , y = %d",i,x,y);
NSLog(@"1 – x + y : %i",1 – x + y);
NSLog(@"1 – (x + y) : %i",1 – (x + y));
NSLog(@"i -= x + y : %i",i -= x + y);//等价于 i = i – (x + y);
[/objc]
使用赋值运算符可以较容易书写,也让其他人易读。
并且可以始程序运行的更有效率,因为编译器在计算表达式时能够产生更少代码。
详细用法请参见代码注解。
order atorvastatin 40mg online cheap lipitor 20mg atorvastatin 40mg brand
cipro 1000mg cost – buy cephalexin 250mg for sale cheap augmentin
ciprofloxacin medication – ciprofloxacin 500mg tablet order generic augmentin 1000mg
buy metronidazole 400mg without prescription – cost cleocin 300mg buy generic zithromax for sale
order ciprofloxacin 500mg pill – order doryx pills buy erythromycin
generic valacyclovir – purchase zovirax online acyclovir 800mg drug
stromectol for lice – order aczone pills tetracycline 500mg over the counter
buy generic metronidazole – buy generic oxytetracycline online azithromycin pill
oral ampicillin buy penicillin pills amoxil for sale online
how to get lasix without a prescription – furosemide over the counter order capoten pill
metformin 500mg over the counter – buy epivir online lincomycin generic
zidovudine 300 mg price – buy glycomet online cheap allopurinol for sale online
clozaril over the counter – glimepiride 1mg us pepcid over the counter
order quetiapine pill – buy ziprasidone pills for sale buy generic eskalith over the counter
anafranil 25mg for sale – imipramine 75mg without prescription buy sinequan 75mg
order hydroxyzine generic – prozac 20mg brand endep where to buy
amoxicillin where to buy – oral amoxicillin buy cipro without a prescription
buy augmentin generic – trimethoprim cheap order ciprofloxacin 500mg online cheap
order cleocin 300mg without prescription – cefpodoxime online order buy chloromycetin pill
azithromycin cheap – buy metronidazole 400mg pill where can i buy ciprofloxacin
stromectol 2mg – stromectol buy cefaclor 250mg for sale
albuterol uk – buy cheap fluticasone order theo-24 Cr pills
purchase clarinex without prescription – order aristocort 10mg online cheap ventolin order
depo-medrol tablet – generic medrol online azelastine sprayer
buy micronase 5mg generic – micronase order order forxiga generic
buy prandin pills – buy empagliflozin 25mg online jardiance over the counter
glucophage order – purchase glycomet online cheap acarbose 50mg over the counter
lamisil 250mg cost – griseofulvin price buy griseofulvin pill
semaglutide 14mg cheap – buy cheap generic desmopressin desmopressin generic
order ketoconazole 200mg online – mentax price sporanox over the counter
order famvir pills – acyclovir 800mg ca oral valcivir 1000mg
order lanoxin 250mg online – dipyridamole 100mg without prescription lasix us
buy hydrochlorothiazide 25 mg generic – plendil for sale order zebeta 5mg online
buy lopressor 50mg online – order inderal 20mg generic order nifedipine 30mg sale
buy generic nitroglycerin – buy diovan online cheap valsartan 160mg cheap
simvastatin worst – tricor bathroom atorvastatin full
rosuvastatin pills orange – rosuvastatin lamp caduet online bat
buy viagra professional charge – viagra gold axe levitra oral jelly shield
priligy doubtful – levitra with dapoxetine whoever cialis with dapoxetine annoy
cenforce stumble – tadacip utter brand viagra pills crazy
brand cialis damn – viagra soft tabs own penisole dawn
cialis soft tabs north – cialis oral jelly pills specter viagra oral jelly office
brand cialis lord – tadora guest penisole fourth
uti antibiotics thick – uti treatment disappear uti antibiotics low
prostatitis medications eye – prostatitis treatment northward prostatitis pills soup
valacyclovir online frog – valacyclovir explanation valacyclovir odd
claritin pills knee – claritin weather claritin original
loratadine medication daemon – loratadine medication floor loratadine treasure
dapoxetine hire – dapoxetine mount priligy stone
promethazine response – promethazine century promethazine blind
ascorbic acid waiter – ascorbic acid suspect ascorbic acid cure
clarithromycin rapid – albendazole form cytotec pills smooth
florinef pills endure – nexium pills peculiar lansoprazole weary
order generic bisacodyl 5mg – liv52 20mg canada oral liv52
buy aciphex 20mg without prescription – motilium online buy purchase motilium pill
cotrimoxazole 480mg without prescription – levetiracetam 1000mg price tobramycin canada
zovirax uk – dydrogesterone sale buy dydrogesterone 10mg generic
dapagliflozin 10 mg brand – buy precose 50mg without prescription order acarbose generic
buy fulvicin 250mg generic – buy lopid order gemfibrozil 300mg without prescription
order vasotec 10mg – doxazosin 2mg tablet xalatan us
generic dramamine 50mg – dramamine 50mg generic risedronate 35mg usa
oral nootropil – levaquin for sale online sinemet 10mg cost
purchase hydrea sale – buy trental without a prescription brand robaxin 500mg
brand spironolactone 25mg – buy naltrexone pills for sale naltrexone cost
how to buy flexeril – order prasugrel pills buy vasotec online
generic zofran 4mg – buy generic detrol buy generic ropinirole
purchase ascorbic acid pills – purchase compro pills cheap prochlorperazine pill
buy atenolol medication – buy sotalol 40mg sale coreg cost
verapamil cheap – order diltiazem pills tenoretic for sale online
order generic atorvastatin – purchase vasotec online cheap order bystolic without prescription
buy generic gasex – ashwagandha brand buy diabecon tablets
lasuna online order – purchase himcolin online buy generic himcolin for sale
cheap speman generic – cheap himplasia sale buy fincar online cheap
buy finasteride paypal – purchase cardura online uroxatral usa
buy terazosin 5mg online – hytrin 5mg us order dapoxetine 60mg pills
oxcarbazepine brand – where to buy trileptal without a prescription purchase levothyroxine