Loop_for

这篇博客探讨了编程中的循环结构,包括for循环的使用方式,并通过实例展示了如何计算阶乘、斐波那契数列以及判断素数。此外,还提及了两种不同的循环实现,即while循环和for循环的简单示例。
 for(计数器的初始化; 判定条件; 变化规律){
循环体
}

* */
//练习: 求x!
// int x= 10;
// int fac = 1;
// for(int i=1;i<=x;i++){
// fac *=i;
// }
// System.out.println("fac="+fac);

//练习:  求斐波那契数列的第n项值
//                         
//      1   1   2   3   5   8  13  21 
//     a1  a2  a3
//         a1  a2  a3
//             a1  a2  a3
//                 a1  a2  a3 

/*
a1 = 1
a2 = 1
a3 = 2
 
  n-3 次    { 
向后平移
a1=a2
a2=a3
a3 = a1+a2

}*/
// int n = 1;
//用户输入n (-100  0  1  2  4 5  6 )

// if(n>=3){
// int a1 = 1;
// int a2 = 1;
// int a3 = 2;
// for(int i=1; i<=n-3;i++){
// a1=a2; //a1为上次的a2
// a2=a3; //a2 为上次的a3    相当于整体向后平移
// a3 = a1+a2;
// }
// System.out.println("第"+n+"项的值为:"+a3);
//
// }else if(n==1||n==2){
// System.out.println("第"+n+"项的值为:"+1);
//
// }else{
// System.out.println("n>=1");
// }


//练习:1.判定一个整数 n 判定其是否是素数
//   2.输出1~30 间所有的素数  
//     循环 i:1~30
//        每次判定i是否是素数  
//          若是则打印

for(int i=1; i<=30;i++){
//----运算 i是否是素数-------
//能整除的总次数
int count = 0;//循环  i从2到n-1
for(int j=2; j<=i-1; j++){
//判定 若 n%i == 0
if(i%j==0){
//则count++
count++;
}
}

if(count == 0){
System.out.println(i);
}
}


/*------判定给定数是否是素数-------*/
// int n = 97;
// int count = 0;//能整除的总次数
// //循环  i从2到n-1
// for(int i=2; i<=n-1; i++){
// //判定 若 n%i == 0
// if(n%i==0){
// //则count++
// count++;
// }
// }
//
// //循环完了  判定  count==0
// if(count == 0){
// System.out.println("是素数");
// }else{
// System.out.println("不是素数");
// }




// int i=1;
// for (;i<=5; ) {
// System.out.println("*"+i);
// i++;
// }
//
// int count = 1;
// while (count<=5) {
// System.out.println("*"+count);
// count++;
// }
}


}
/* * Copyright 2019 TP-Link Technologies Co., Ltd. All rights reserved. * * */ #include "nano_loop.h" struct nano_loop_context* nano_loop_new(void) { struct nano_loop_context *ctx = osal_malloc(sizeof(struct nano_loop_context)); if (ctx) { memset(ctx, 0, sizeof(struct nano_loop_context)); INIT_LIST_HEAD(&(ctx->timeouts)); } return ctx; } static void nano_loop_run_events(struct nano_loop_context *ctx, int timeout) { struct nano_loop_fd *sock; nano_switch_fd_t fds[NANO_LOOP_MAX_REGISTERED_FDS], fd; SH_INT32 i, ret, len; if (ctx->pending_fds_num <= 0) { for (i = 0; i < ctx->registered_num; i++) fds[i] = ctx->registered_fds[i]->fd; len = ctx->registered_num; ret = nano_switch_select(fds, &len, timeout); if (ret == 0 && len > 0) { ctx->pending_fds_num = len; for (i = 0; i < len; i++) ctx->pending_fds[i] = fds[i]; } } if (ctx->pending_fds_num > 0) { fd = ctx->pending_fds[ctx->pending_fds_num - 1]; ctx->pending_fds_num--; for (i = 0; i < ctx->registered_num; i++) { sock = ctx->registered_fds[i]; if (sock->fd == fd) { sock->cb(sock, NANO_LOOP_READ); /* registered_fds array may be changed in cb, since we need process cb only once, so we must break here */ break; } } } } int nano_loop_fd_add(struct nano_loop_context *ctx, struct nano_loop_fd *sock) { if (sock->registered) nano_loop_fd_delete(sock); if (!ctx) ctx = sock->ctx; else sock->ctx = ctx; if (!ctx || ctx->registered_num >= NANO_LOOP_MAX_REGISTERED_FDS || sock->fd == NULL) return -1; ctx->registered_fds[ctx->registered_num++] = sock; sock->registered = TRUE; return 0; } int nano_loop_fd_delete(struct nano_loop_fd *fd) { int i; if (!fd->registered || !fd->ctx) return 0; fd->registered = FALSE; for (i = 0; i < fd->ctx->registered_num; i++) { if (fd->ctx->registered_fds[i] == fd) { fd->ctx->registered_fds[i] = fd->ctx->registered_fds[fd->ctx->registered_num - 1]; fd->ctx->registered_num--; break; } } return 0; } int nano_loop_timeout_add(struct nano_loop_context *ctx, struct nano_loop_timeout *timeout) { struct nano_loop_timeout *tmp; struct list_head *h; if (timeout->pending) return -1; if (!ctx) ctx = timeout->ctx; else timeout->ctx = ctx; if (!ctx) return -1; h = &(ctx->timeouts); list_for_each_entry(tmp, &(ctx->timeouts), list) { if (tmp->time > timeout->time) { h = &tmp->list; break; } } list_add_tail(&timeout->list, h); timeout->pending = TRUE; return 0; } int nano_loop_timeout_set(struct nano_loop_context *ctx, struct nano_loop_timeout *timeout, int msecs) { if (msecs < 0) return -1; if (timeout->pending) nano_loop_timeout_cancel(timeout); timeout->time = osal_time_ms() + msecs; return nano_loop_timeout_add(ctx, timeout); } int nano_loop_timeout_cancel(struct nano_loop_timeout *timeout) { if (!timeout->pending) return -1; list_del(&timeout->list); timeout->pending = FALSE; return 0; } int nano_loop_timeout_remaining(struct nano_loop_timeout *timeout) { SH_UINT64 tv; if (!timeout->pending) return -1; tv = osal_time_ms(); return timeout->time - tv; } static int nano_loop_get_next_timeout(struct nano_loop_context *ctx, SH_UINT64 tv) { struct nano_loop_timeout *timeout; if (list_empty(&(ctx->timeouts))) return -1; timeout = list_first_entry(&(ctx->timeouts), struct nano_loop_timeout, list); if (timeout->time <= tv) return 0; else return timeout->time - tv; } static void nano_loop_process_timeouts(struct nano_loop_context *ctx, SH_UINT64 tv) { struct nano_loop_timeout *t; while (!list_empty(&(ctx->timeouts))) { t = list_first_entry(&(ctx->timeouts), struct nano_loop_timeout, list); if (t->time > tv) break; nano_loop_timeout_cancel(t); if (t->cb) t->cb(t); } } static void nano_loop_clear_timeouts(struct nano_loop_context *ctx) { struct nano_loop_timeout *t, *tmp; list_for_each_entry_safe(t, tmp, &(ctx->timeouts), list) nano_loop_timeout_cancel(t); } int nano_loop_run(struct nano_loop_context *ctx) { SH_UINT64 tv; ctx->nano_loop_cancelled = FALSE; while (!ctx->nano_loop_cancelled) { tv = osal_time_ms(); nano_loop_process_timeouts(ctx, tv); if (ctx->nano_loop_cancelled) break; tv = osal_time_ms(); nano_loop_run_events(ctx, nano_loop_get_next_timeout(ctx, tv)); } return 0; } void nano_loop_done(struct nano_loop_context *ctx) { if (ctx) { nano_loop_clear_timeouts(ctx); osal_free(ctx); } } 分析一下这些代码的用途和原理
最新发布
12-09
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值