-循环语句分析 

循环语句的基本工作方式

- 通过条件表达式判断是否执行循环体

- 条件表达式循环if语句表达式的原则

do、while、for的区别

- do语句先执行后判断,循环体至少循环一次

- while语句先判断后执行,循环体可能不执行

- for语句先判断后执行,相比while更简洁

三者在使用上的区别:

 #include <stdio.h>

 int f1(int n)
{
int ret = ; if( n > )
{
do
{
ret += n;
n--;
}
while( n > );
} return ret;
} int f2(int n)
{
int ret = ; while( n > )
{
ret += n;
n--;
} return ret;
} int f3(int n)
{
int ret = ;
int i = ; for(i=; i<=n; i++)
{
ret += i;
} return ret;
} int main()
{
printf("%d\n", f1());
printf("%d\n", f2());
printf("%d\n", f3()); return ;
}

-循环语句分析

  

break和continue的区别

- break表示终止循环的执行

- continue表示终止本次循环,进入下一次循环

思考:

switch能否用continue关键字?为什么?

答:不行,因为continue是用于循环的,而switch语句根本不是循环。

 #include <stdio.h>

 void f1(int n)
{
int i = ; for(i=; i<=n; i++)
{
if( (i % ) == )
{
break;
} printf("%d ", i);
} printf("\n");
} void f2(int n)
{
int i = ; for(i=; i<=n; i++)
{
if( (i % ) == )
{
continue;
} printf("%d ", i);
} printf("\n");
} int main()
{
f1();
f2(); return ;
}

do和while 结合的妙用

实例三:下面的例子为了执行强制free();来防止内存泄漏。

 #include <stdio.h>
#include <malloc.h> int func(int n)
{
int i = ;
int ret = ;
int* p = (int*)malloc(sizeof(int) * n); do
{
if( NULL == p ) break; if( n < ) break; if( n > ) break; for(i=; i<n; i++)
{
p[i] = i;
printf("%d\n", p[i]);
} ret = ;
}while( ); printf("free(p)\n"); free(p); return ret;
} int main()
{
if( func() )
{
printf("OK\n");
}
else
{
printf("ERROR\n");
} return ;
}

小结:

for循环先进入循环再进入循环体

for循环适合于循环次数固定的场合

while循环先进行判断再进入循环体执行

while循环适合于循环次数不定的场合

do ... while 循环先执行循环体再进行条件判断

do ... while 循环体至少执行一次循环体

C语言循环语句工程用法的更多相关文章

  1. go语言循环语句 for

    Go语言中的循环语句只支持for关键字,而不支持while和do-while结构. sum := 0 for i := 0; i < 10; i++ { sum += i } 无限循环的写法: ...

  2. while,do while和for循环语句的用法

    一.while的用法 //循环 int i = 10; while(i > 0){ if(i==8) {i--; continue;//跳过 } System.out.println(--i); ...

  3. Go 语言循环语句

    在不少实际问题中有许多具有规律性的重复操作,因此在程序中就需要重复执行某些语句. 以下为大多编程语言循环程序的流程图: Go 语言提供了以下几种类型循环处理语句: 循环类型 描述 for 循环 重复执 ...

  4. GO语言学习(十一)Go 语言循环语句

    Go 语言提供了以下几种类型循环处理语句: 循环类型 描述 for 循环 重复执行语句块 循环嵌套 在 for 循环中嵌套一个或多个 for 循环 语法 Go语言的For循环有3中形式,只有其中的一种 ...

  5. 详解Python中的循环语句的用法

    一.简介 Python的条件和循环语句,决定了程序的控制流程,体现结构的多样性.须重要理解,if.while.for以及与它们相搭配的 else. elif.break.continue和pass语句 ...

  6. 子数涵数·C语言——循环语句

      之前,我们讲过了编程中的三种结构(顺序.条件.循环),现在我们来看一下循环语句如何编写. 一.while循环语句(先判断后执行) 1 #include<stdio.h> 2 int m ...

  7. R语言 循环语句、分支语句和中止语句-控制流篇

    for 循环 用法 for (n in m) expr 若n在m中则运行 expr while 循环 用法 while (condition) expr 当符合condition时运行expr rep ...

  8. for循环语句的用法

    1.for(int i : index){}用法[又称是foreach用法]: 比如: public class Test { public static void main(String[] arg ...

  9. C#语言循环语句for嵌套

随机推荐

  1. 纪中12日T1 2307. 选择

    2307. 选择 (File IO): input:choose.in output:choose.out 时间限制: 1000 ms  空间限制: 262144 KB  具体限制   Goto Pr ...

  2. Process、管理者权限、注册表、xml修改

    //判断是否有管理者权限 WindowsPrincipal principal = new WindowsPrincipal(WindowsIdentity.GetCurrent()); if (!p ...

  3. JN_0018:运行窗口不显示

    1,新建vbs文件 CreateObject("WScript.Shell").Run "cmd /c E:/OneDrive/NodeJS/WebServer/star ...

  4. 用JavaScript设计和创建对象

    通过在优锐课的java学习分享中,get很多学习新技能,分享给大家参考学习. 介绍 在阅读此分步指南之前,你可能需要关注面向对象编程的介绍. 以下步骤中包含的Java代码与该文章理论中使用的Book对 ...

  5. ng--todolist

    todolist小案例 该案例的模板文件下载地址 走外国服务器, ̄□ ̄|| app.module.ts import { BrowserModule } from '@angular/platform ...

  6. 关于BaseServlet的使用

    一篇很棒的参考 https://blog.csdn.net/weixin_42425970/article/details/84279257

  7. Docke-ce 安装

    Docker-ce 的安装 安装系统工具 yum install -y yum-utils device-mapper-persistent-data lvm2 添加docker镜像源 yum-con ...

  8. Easyui-Tree和Combotree使用注意事项-sunziren

    版权声明:本文为sunziren原创文章,博客园首发,转载务必注明出处以及作者名称. Easyui-Tree和Combotree所使用的数据结构是类似的,在我的上一篇文章<Easyui-Tree ...

  9. 服务起不来,查看ps axj 看服务是否为守护进程(TPGID 为-1)

    在linux命令行中输入: ps axj 查看服务进程的 TPGID 字段的值是否为-1 ,为-1表示为守护进程 不为-1表示不是守护进程,服务启动不起来,或者启动起来后又被杀死了

  10. Leetcode Week4 Find Minimum in Rotated Sorted Array II

    Question Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforeha ...