doushenmao9036 2011-10-11 08:22
浏览 18
已采纳

什么时候循环有用吗?

As you all probably know, do loops execute at least once, even if the statement is false — while the while loop would never execute even once if the statement is false.

When are do loops useful? Could someone give me a real life example?

  • 写回答

3条回答 默认 最新

  • douliangpo0128 2011-10-11 08:25
    关注

    They're basically useful when you want something to happen at least once, and maybe more.

    The first example that comes to mind is generating a unique ID (non sequentially) in a database. The approach I sometimes take is:

    lock table
    do {
        id = generate random id
    } while(id exists)
    insert into db with the generated id
    unlock table
    

    Basically it will keep generating ids until one doesn't exist (note: potentially an infinite loop, which I might guard against depending on the situation).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 武汉岩海低应变分析软件,导数据库里不显示波形图
  • ¥15 CreateBitmapFromWicBitmap内存释放问题。
  • ¥30 win c++ socket
  • ¥30 CanMv K210开发板实现功能
  • ¥15 C# datagridview 栏位进度
  • ¥15 vue3页面el-table页面数据过多
  • ¥100 vue3中融入gRPC-web
  • ¥15 kali环境运行volatility分析android内存文件,缺profile
  • ¥15 写uniapp时遇到的问题
  • ¥15 vs 2008 安装遇到问题