三目运算符

A?B:C

等价于

if(A)

  B;

else

  C;

实例:

int i;

i=(3>2?5:1)  //如果3》2为真,i的值为5,否则为1

printf(“%d”,i);

逗号表达式

格式

(A,B,C,D)

功能: 从左到右执行

    最终表达式的值是最后一项的值

实例:

#include <stdio.h>
int main(void)
{
int i;
int j=2;
i=(j++,++j,j+2,j-3);
printf("%d\n",i);

return 0;
}

结果为1。

while循环

1.执行顺序

格式: while(表达式)

    语句;

2.与for的相互比较

for和while可以相互转换,但for的逻辑性更强,更不容易出错

for(1;2;3)

    A;

等价于

1;

while(2)

{

A;

3;

}

//for 与 while的转换
#include <stdio.h>
int main(void)
{
int sum=0;
int i;
/*for(i=1;i<101;i++)
sum=sum+i;
*/
i=1;
while (i<101)
{
sum=sum + i;//语句一
i++; //语句二 语句一与语句二顺序不可以调转
}
printf("%d\n",sum);

return 0;
}

3.实例

从键盘输入一个数字,如果该数字是回文数,返回yes,否则返回no。

回文数:正着写和倒着写都一样  比如:121

#include <stdio.h>
int main(void)
{
int i;
int m;
int sum=0;
printf("请输入您要判断的数字:");
scanf("%d",&i);
m=i;
while(m)
{
sum= sum *10+m%10;
m/=10;//等价于m=m/10
}
if(sum ==i)
printf("yes\n");
else
printf("no!\n");

return 0;
}

//斐波拉契数列 1 2 3 5 8 13 21 34
#include <stdio.h>
int main(void)
{
int n;
int f1,f2,f3;

int i;
f1=1;
f2=2;
printf("请输入您要输入的序列:");
scanf("%d",&n);
if (1==n)
{
f3=1;
}
else if(2==n)
{
f3=2;
}
else
{
for (i=3; i<=n;i++)
{
f3=f1+f2;
f1=f2;
f2=f3;
}
}
printf("%d\n",f3);
return 0;
}

do... while:主要用于人机交互

格式:do

{

...

} while(表达式);

do ...while..并不等价于for,也不等价于while

代码整理格式:全选(ctrl+a)---》alt+f8

//斐波拉契数列 1 2 3 5 8 13 21 34
#include <stdio.h>
int main(void)
{

int n;
int f1,f2,f3;
char ch;
int i;
f1=1;
f2=2;
do
{
printf("请输入您要输入的序列:");
scanf("%d",&n);
if (1==n)
{
f3=1;
}
else if(2==n)
{
f3=2;
}
else
{
for (i=3; i<=n;i++)
{
f3=f1+f2;
f1=f2;
f2=f3;
}
printf("%d\n",f3);
}

printf("您想继续吗(y/n):");
scanf(" %c", &ch); //%c前面要加一个空格(空白符的问题)
}//while (1);
while ('y'==ch || 'Y'==ch);

return 0;
}

随机推荐

  1. mac显示所有文件、不产生.DS_Store文件

    1.mac的Finder显示所有文件: defaults write com.apple.finder AppleShowAllFiles -bool true killall Finder 2.ma ...

  2. while do while 区别

    一.while语句的一般形式为:while(表达式)语句其中表达式是循环条件,语句为循环体.while语句的语义是:计算表达式的值,当值为真(非0)时, 执行循环体语句. int i=10; whil ...

  3. LeetCode Shortest Distance from All Buildings

    原题链接在这里:https://leetcode.com/problems/shortest-distance-from-all-buildings/ 题目: You want to build a ...

  4. centos7 systemctl命令

    systemctl命令是系统服务管理器指令,它实际上将 service 和 chkconfig 这两个命令组合到一起. 实例: 启动nfs服务:systemctl start nfs-server.s ...

  5. Java并发控制:ReentrantLock Condition使用详解

    生产者-消费者(producer-consumer)问题,也称作有界缓冲区(bounded-buffer)问题,两个进程共享一个公共的固定大小的缓冲区.其中一个是生产者,用于将消息放入缓冲区:另外一个 ...

  6. pthread_attr_setdetachstate

    pthread_create函数可以指定新创建线程的属性. pthread_attr_setdetachstate() set  detach state attribute in thread at ...

  7. SQLServer2005+附加数据库时出错提示操作系统错误5(拒绝访问)错误5120的解决办法

    SQLServer2005+ 附加数据库时出错提示操作系统错误5(拒绝访问)错误5120的解决办法 我们在用Sql SQLServer2005+附加数据库文件时弹出错误信息如下图的处理办法: 方案一: ...

  8. Hadoop:搭建hadoop集群

    操作系统环境准备: 准备几台服务器(我这里是三台虚拟机): linux ubuntu 14.04 server x64(下载地址:http://releases.ubuntu.com/14.04.2/ ...

  9. matlab中实现Gabor滤波器

    1.spatialgabor.m描述gabor函数 % SPATIALGABOR - applies single oriented gabor filter to an image%% Usage: ...

  10. FingerGestures 屏蔽NGUI的方法

    在Google搜到的帖子中提到的方法 有一个地方是错误的(折腾了好久 哎) http://www.tasharen.com/forum/index.php?topic=127.0 Camera NGU ...