多行注释的快捷键:Cmd + /
程序对齐:ctrl+I
ios 想5分钟,想不出来就不想了
1.逻辑运算
1.1 &&:全真为真,有假即假
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//与 &&
int a = 3;
int b = 4;
int result = a && b;
self.outputLabel.text = [NSString stringWithFormat:@"%d",result];
a = 0;
result = a && b;
self.outputLabel.text = [NSString stringWithFormat:@"%d",result];
//判断一个数X是否在数域(3,8]之间
//3 < x <=8
int x = 10;
result = x > 3 && x <= 8;
self.outputLabel.text = [NSString stringWithFormat:@"%d",result];
}
1.2 || : 有真即真,全假为假
1.3 ! : 颠倒黑白
上面三个“非”得运算符最高,其次是“与”最后是“或”
1.4 短路:是编译器对源代码进行的一种优化(删除)
//短路
a = 3;
b = 4;
int c = 5;
result = a > b && b < ++c;
//c = c + 1;
//result = a > b && b < c;
self.outputLabel.text = [NSString stringWithFormat:@"%d",c];
//运行结果c 为5而非6,出现短路情况
//result = a < b && b < c || c < a遇到类似的式子先确定优先级
练习一输入一个年份值,判断是否为闰年(优化程序)
- (IBAction)juage:(UIButton *)sender {
int year = [self.inputTextField.text intValue];
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
{
self.outputLabel.text = [NSString stringWithFormat:@"%d是闰年",year];
}
else
{
self.outputLabel.text = [NSString stringWithFormat:@"%d是平年",year];
}
}
练习二输入一个年月份值,判读该年该月天数
- (IBAction)calculation:(UIButton *)sender {
int year = [self.inputYearTextField.text intValue];
int month = [self.inputMonthTextField.text intValue];
if (month < 1 || month > 12)
{
self.inputMonthTextField.text = @"×";
}
int days = 31;
if (month == 2)
{
days = 28 + ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
}
else if (month == 4 || month == 6 || month == 9 || month == 11)
{
days = 30;
}
self.outputLabel.text = [NSString stringWithFormat:@"%d",days];
}
2.三目运算符(目表示涉及的表达式的个数)
a++;单目运算符
a+b;双目运算符
max = a > b ? a : b;三目运算符//如果a > b为真则运算第二个表达式a而把第三个表达式直接删除;如果a > b为假则运算第三个表达式b而把第二个表达式直接删除
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
int a = 10;
int b = 20;
int max;
// if (a > b)
// {
// max = a;
// }
// else
// {
// max = b;
// }
max = a > b ? a : b;
self.outputLabel.text = [NSString stringWithFormat:@"%d",max];
//%@表示字符串所占的占位符
BOOL c = YES;
self.outputLabel.text = [NSString stringWithFormat:@"%@", c ? @"YES" : @"NO"];
BOOL isMan = NO;
//赋值为真时,“三目运算”运算第二个表达式,当为“假”时,则运算第三个表达式;有了三目运算后,逻辑值就不能输出1或0了
self.outputLabel.text = [NSString stringWithFormat:@"%@",isMan ? @"男" : @"女"];
self.outputLabel.text = @"请输入一个年份:";
}
- (IBAction)clickButton:(UIButton *)sender {
int year = [self.inputTextField.text intValue];
self.outputLabel.text = [NSString stringWithFormat:@"%d年的2月有%d天",
year,((year % 4 == 0 && year % 100 != 0 )|| year % 400 == 0) ? 29 : 28];
3.赋值运算符
3.1左值:赋值号左边的值。常量与表达式不能做左值
3.2右结合性(赋值运算是右结合性)
3.3赋值号“=”与逻辑等“==”的区别
3.4复合赋值运算符,注意:其优先级极低
int x;
x = 3;//变量x就是一个左值
//3 = x;//左值不能是常量
//x + 5 = 10;//左值也不能是表达式
int y;
y = x + (y = 10);
//赋值号“=”与逻辑等“==”的区别
int a = 10;
int b = 20;
self.outputLabel.text = [NSString stringWithFormat:@"%d\n%d",a == b, a = b];
//a == b 为逻辑运算只有真跟假即输出的值为1或0;而a = b 得到值取决于赋值号右边的b值,原因是因为右结合性故a得到20
//复合赋值运算符
x = 10;
x += 5;//x = x + 5
self.outputLabel.text = [NSString stringWithFormat:@"%d",x];
x -= 5;//x = x - 5
self.outputLabel.text = [NSString stringWithFormat:@"%d",x];
x *= 5 + 3;//x = x * (5 + 3)
self.outputLabel.text = [NSString stringWithFormat:@"%d",x];
4.switch语句
4.1表达式的值必须是整数
4.2case后面必须是整数常量,不能重复。但无顺序要求
4.3break语句用于终止switch语句的运行
4.4default用于表达式的值与所有case后面的值均不相同的时,执行其后的语句。可有可无。
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *outputLabel;
@property (weak, nonatomic) IBOutlet UITextField *intputTextField;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.outputLabel.text = @"请输入一个1~7之间的整数";
}
- (IBAction)clickButton:(UIButton *)sender {
int number = [self.intputTextField.text intValue];
switch (number)//表达式的值必须是整数,不能是浮点数
{
case 1://case后面有且仅有一个整数常量
self.outputLabel.text = @"星期一";
break;
case 2://case后面不能是变量,表达式,浮点数,字符串
self.outputLabel.text = @"星期二";
break;
case 3://case后面不能重复,但无顺序要求
self.outputLabel.text = @"星期三";
break;
case 4:
self.outputLabel.text = @"星期四";
break;//终止switch语句的运行
case 5:
self.outputLabel.text = @"星期五";
break;
case 6:
self.outputLabel.text = @"星期六";
break;
case 7:
self.outputLabel.text = @"星期日";
break;
default://所有case后面的值均与number值不相同时,则执行其后的语句。可有可无的
self.outputLabel.text = @"输入错误";
break;
}
}
练习
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *outputLabel;
@property (weak, nonatomic) IBOutlet UITextField *intputTextField;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.outputLabel.text = @"请输入一个字符";
}
- (IBAction)clickButton:(UIButton *)sender { self.outputLabel.text = @"";
char ch = [self.intputTextField.text characterAtIndex:0];
switch (ch) {
case '0':
self.outputLabel.text = @"x == 0";
break;
case '1':
self.outputLabel.text = @"x == 1";
break;
case '2':
self.outputLabel.text = @"x == 2";
break;
case 'A':
self.outputLabel.text = @"x == A";
break;
case '#':
self.outputLabel.text = @"x == #";
break;
}
}
练习二输入一个年月份值,判读该年该月天数
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *inputYearTextField;
@property (weak, nonatomic) IBOutlet UITextField *inputMonthTextField;
@property (weak, nonatomic) IBOutlet UILabel *outputLabel;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)clickButton:(UIButton *)sender
{
int year = [self.inputYearTextField.text intValue];
int month = [self.inputMonthTextField.text intValue];
int days = 31;
switch (month) {
case 2:
days = 28 + ((year % 4 == 0 && year % 100 !=0) || year % 400 ==0);
break;
case 4:case 6:case 9:case 11:
days = 30;
break;
default:
break;
}
self.outputLabel.text = [NSString stringWithFormat:@"%d",days];
}
5循环
5.1是指依照指定的条件,反复执行某段儿代码
5.2 for
NSString *str = @"";
for (int i = 0; i < 10 ; I++)
{
str = [str stringByAppendingFormat:@"%d ",I];
}
self.outputLabel.text = str;
求0到100得和
self.outputLabel.text = str;
int sum = 0;
for (int i = 1; i <= 100; I++)
{
sum += I;
}
self.outputLabel.text = [NSString stringWithFormat:@"sum = %d",sum];
练习百分制成绩转换等级成绩
- (IBAction)transfer:(UIButton *)sender {
self.outputLabel.text = @"";
int score = [self.inputTextField.text intValue];
switch (score) {
case 90 ... 100:
self.outputLabel.text = @"优";
break;
case 60 ... 89:
self.outputLabel.text = @"一般";
break;
case 0 ... 59:
self.outputLabel.text = @"不及格";
break;
default:
self.inputTextField.text = @"×";
break;
}
}
练习二求任意两个数之间的和,如[13,28]
- (IBAction)calculation:(UIButton *)sender {
int firstNumber = [self.inputFirstNumberTextField.text intValue];
int secondNumber = [self.inputSecondNumberTextField.text intValue];
if(firstNumber > secondNumber)
{
int temp = firstNumber;
firstNumber = secondNumber;
secondNumber = temp;
}
int sum = 0;
for (int i = firstNumber; i<=secondNumber; I++)
{
sum += I;
}
self.outputLabel.text = [NSString stringWithFormat:@"%d",sum];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
double sum1 = 0;
double sum2 = 0;
for (int i = 1; i < 2000000000; i += 4)
{
sum1 += 1.0 / I;
sum2 += 1.0 / (i + 2);
}
self.outputLabel.text = [NSString stringWithFormat:@"π=%.15f",4*(sum1 - sum2)];
}
循环结构
5for(续)
5.2.1三个表达式的特殊用法
5.2.1.1三个表达式可以分别或同时为空
5.2.1.2三个表达式可以是逗号表达式
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self method1];
}
-(void)method1
{
NSString *str = @"";
int i = 0;
for (; i < 10; i++)//表达式一可以为空
{
str = [str stringByAppendingFormat:@"%d ",I];
}
str = [str stringByAppendingFormat:@"%d ",I];
self.outputLabel.text = str;
//死循环
// str = @"";
// for (int i = 0; YES; i++)//表达式二可以为空
// {
// str = [str stringByAppendingFormat:@"%d ",I];
// NSLog(@"%d ",i);
// }
// self.outputLabel.text = str;
for (int i = 0; i < 10; )//表达式三可以为空
{
NSLog(@"%d",i++);
}
// for(; ;)//三个表达式可以同时为空
// {
//
// }
}
str = @"";
for (int i = 0, j = 0, k = 0;i * j * k < 30; i++, j += 2, k++)
{
str = [str stringByAppendingFormat:@"i=%d,j=%d,k=%d\n",i,j,k];
}
self.outputLabel.text = str;
}
5.3while循环:用于不固定次数的循环
做练习:删除字符串中除数字、小数点外的其他字符
- (IBAction)clickButton:(UIButton *)sender {
NSString *str = @"";
int index = 0;
while (index < self.inputTextField.text.length)
{
char ch = [self.inputTextField.text characterAtIndex:index];
if((ch >='0' && ch <= '9')||ch == '.')
{
str = [str stringByAppendingFormat:@"%c",ch];
}
self.outputLabel.text = str;
index++;
}
}
练习二:将字符串中所有的字符,按大小字母数字或其他字符分类显示
- (IBAction)transfer:(UIButton *)sender {
NSString *upper = @"";
NSString *lower = @"";
NSString *digit = @"";
NSString *other = @"";
int index = 0;
while (index < self.inputTextField.text.length)
{
char ch = [self.inputTextField.text characterAtIndex:index];
switch (ch) {
case 'A' ... 'Z':
upper = [upper stringByAppendingFormat:@"%c",ch];
break;
case 'a' ... 'z':
lower = [lower stringByAppendingFormat:@"%c",ch];
break;
case '0' ... '9':
digit = [lower stringByAppendingFormat:@"%c",ch];
break;
default:
other = [other stringByAppendingFormat:@"%c",ch];
break;
}
index++;
}
self.outputUpperLabel.text = upper;
self.outputLowerLabel.text = lower;
self.outputDigitLabel.text = digit;
self.outputOtherLabel.text = other;
}
5.4do~while循环
-(void)method3
{
NSString *str = @"";
int x = 0;
do
{
str = [ str stringByAppendingFormat:@"%d ",x];
x++;
}
while (x < 10);
self.outputLabel.text = str;
}
做练习将字符串中的小写字母转换成大写字母
- (IBAction)transfer:(UIButton *)sender {
NSString *str = @"";
int index = 0;
do
{
char ch = [self.inputTextField.text characterAtIndex:index];
if (ch >= 'a' && ch <= 'z')
{
ch -= 32;
}
str = [str stringByAppendingFormat:@"%c",ch];
index++;
}while (index < self.inputTextField.text.length);
self.outputLabel.text = str;
}
练习二统计字符串中大、小写字母、数字、其他字符的个数
- (IBAction)statistics:(UIButton *)sender {
int upper = 0;
int lower = 0;
int digit = 0;
int other = 0;
int index = 0;
do
{
char ch = [self.inputTextField.text characterAtIndex:index];
switch (ch) {
case 'A' ... 'Z':
upper++;
break;
case 'a' ... 'z':
lower++;
break;
case '0' ... '9':
digit++;
break;
default:
other++;
break;
}
index++;
}while (index
练习、计算器
- (IBAction)clickKey:(UIButton *)sender {
NSString *str = self.outputLabel.text;
switch (sender.tag) {
case 0 ... 9:
str = [str stringByAppendingFormat:@"%ld",sender.tag];
break;
case 10:
str = [str stringByAppendingFormat:@"+"];
break;
case 11:
str = [str stringByAppendingFormat:@"-"];
break;
case 12:
str = [str stringByAppendingFormat:@"*"];
break;
case 13:
str = [str stringByAppendingFormat:@"/"];
break;
case 14:
{
int num1 = 0;
int i = 0;
for (;[str characterAtIndex:i] >= '0' && [str characterAtIndex:i] <= '9';I++)
{
num1 = num1 * 10 + [str characterAtIndex:i] - '0';
}
char operator = [str characterAtIndex:i];
int num2 = 0;
for (++i;i< str.length && [str characterAtIndex:i] >= '0' && [str characterAtIndex:i] <= '9';I++)
{
num2 = num2 * 10 + [str characterAtIndex:i] - '0';
}
switch (operator) {
case '+':
str = [NSString stringWithFormat:@"%d",num1 + num2];
break;
case '-':
str = [NSString stringWithFormat:@"%d",num1 - num2];
break;
case '*':
str = [NSString stringWithFormat:@"%d",num1 * num2];
break;
case '/':
str = [NSString stringWithFormat:@"%d",num1 / num2];
break;
}
break;
}
case 15:
str = @"0";
break;
}
self.outputLabel.text = str;
}
6循环(续)
6.1循环辅助语句
6.1.1 break
-(void)method1
{
NSString *str = @"";
for(int i = 0; i < 10; I++)
{
str = [str stringByAppendingFormat:@"%d ",I];
if (i == 5)
{
break;
}
}
self.outputLabel.text = str;
6.1.2continue
-(void)method2
{
NSString *str = @"";
for (int i = 0; i < 10; I++)
{
if (i == 5)
{
continue;
}
str = [str stringByAppendingFormat:@"%d ",I];
}
self.outputLabel.text = str;
}
-(void)method2
{
NSString *str = @"";
for (int i = 1;i <= 5;i++)
{
if (i % 2)
{
str = [str stringByAppendingFormat:@"*"];
}
else
{
continue;
}
str = [str stringByAppendingFormat:@"#"];
}
str = [str stringByAppendingFormat:@"$"];
self.outputLabel.text = str;
}
做练习:数七的游戏(不含含七和七的倍数),求第30个人数的数是多少?
NSString *str = @"";
int numberNumbered = 0;
int personNumber = 0;
while (1)
{
numberNumbered++;
if (numberNumbered % 7 == 0 || numberNumbered % 10 == 7 || numberNumbered / 10 == 7)
{
continue;
}
if (++personNumber == 30)
{
break;
}
}
self.outputLabel.text = [NSString stringWithFormat:@"第%d个人数的数是%d",personNumber,numberNumbered];