美文网首页
return、break 和continue 的使用区别

return、break 和continue 的使用区别

作者: 稀释1 | 来源:发表于2020-05-22 16:11 被阅读0次
-(void)viewDidLoad{
 [super viewDidLoad];
   UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
   btn.center = self.view.center;
   btn.backgroundColor = UIColor.redColor;
   [btn addTarget:self action:@selector(buttonSelectAction) forControlEvents:UIControlEventTouchUpInside];
   [self.view addSubview:btn];
}

-(void)buttonSelectAction{
    
    NSLog(@"开始");
    [self testMethod];
    NSLog(@"结束");
    
}
-(void)testMethod{
    
    NSLog(@"方法内部开始");
    for (int i = 0; i < 5; i ++) {
        
        for (int j = 0; j<4; j++) {
              if (j == 0) {
                        NSLog(@"进入! j==%d",j);
//                        return;// 跳出当前作用域,返回到函数调用处,继续执行
                        break;// 结束当前循环 跳出当前代码块,继续执行
//                        continue;// continue之后的代码不会执行 进行下一轮循环
                   
                    }
             NSLog(@"执行中...j==%d",j);
        }
        
      
        NSLog(@"执行中...i==%d",i);
        
    }
    NSLog(@"方法内部结束");
}

相关文章

网友评论

      本文标题:return、break 和continue 的使用区别

      本文链接:https://www.haomeiwen.com/subject/bonqahtx.html