使用if语句时应注意的问题(初学者)

(1)在三种形式的if语句中,在if关键字之后均为表达式。该表达式通常是逻辑表达式或关系表达式,但也可以是其他表达式,如赋值表达式等,甚至也可以是一个变量。

例:if(a=5)语句;

if(b)语句;

只要表达式的值为非零,即为“真”。

比较:

#include<stdio.h>

void main()
{
    int a,b;
    scanf("%d%d",&a,&b);
    if (a=b)
    {
        printf("%d\n",a);
    }
}
#include<stdio.h>

void main()
{
    int a,b;
    scanf("%d%d",&a,&b);
    if (a==b)
    {
        printf("%d\n",a);
    }
}

注;若if后的a==5输入成a=5,则输出结果永远为真。

如何避免:将a==5改为5==a(习惯上的改变,这只是一个例子)

(2)在if语句中,条件判断表达式必须用括号括起来,在语句之后必须加分号。

(3)在if语句的三种形式中,所有的语句应为单个语句,如果想要在满足条件时执行一组(多个语句),则必须把这一组语句用{}括起来组成一个复合语句。但要注意的是在}后不能再加;号。(建议单个语句也用{}括起来,方便以后插入新语句)

例:

#include<stdio.h>

void main()
{
    int a,b;
    scanf("%d%d",&a,&b);
    if (a>b)
    {
        a++;
        b++;
    }
    else
    {
        a=0;
        b=10;
    }
    printf("%d,%d",a,b);
}

补例1:写一个程序完成下列功能:

1、输入一个分数score;

2、score<60      输出E

3、60<=score<70             输出D

4、70<=score<80     输出C

5、80<=score<90             输出B

6、90<=score      输出A

#include<stdio.h>

void main()
{
    int score;
    printf("input a score\n");
    scanf("%d",&score);
    if(score<60)
    {
        printf("The score is E");
    }
    else if((score>60 || score==60)&&score<70)
    {
        printf("The score is D ");
    }
    else if((score>70 || score==70)&&score<80)
    {
        printf("The score is C");
    }
    else if((score>80 || score==80)&&score<90)
    {
        printf("The score is B");
    }
    else if(score>90||score==90)
    {
        printf("The score is A");
    }
}

补例2:输入3个数a,b,c,要求按由小到大的顺序输出。

提示:if  a>b  将a和b互换;

     if  a>c  将a和c互换;

     if  b>c  将b和c互换;

#include<stdio.h>

void main()
{
    int a,b,c,temp;
    printf("input three numbers\n");
    scanf("%d%d%d",&a,&b,&c);
    if(a>b)
    {
        temp=a;
        a=b;
        b=temp;
    }
    if(a>c)
    {
        temp=a;
        a=c;
        c=temp;
    }
    if(b>c)
    {
        temp=b;
        b=c;
        c=temp;
    }
    printf("%d %d %d \n",a,b,c);
}

原文地址:https://www.cnblogs.com/lvfengkun/p/10217437.html

时间: 2024-08-30 04:38:40

使用if语句时应注意的问题(初学者)的相关文章

MySQL运行内存不足时应采取的措施?

排除故障指南:MySQL运行内存不足时应采取的措施? 天一阁@ 老叶茶馆 1周前 导读 排除故障指南:MySQL运行内存不足时应采取的措施? 翻译团队:知数堂藏经阁项目 - 天一阁 团队成员:天一阁-冷锋. 天一阁-Judy .天一阁-神谕 译文校稿:叶师傅 原文出处:<What To Do When MySQL Runs Out of Memory: Troubleshooting Guide> https://www.percona.com/blog/2018/06/28/what-to-

MySQL运行内存不足时应采取的措施

导读 排除故障指南:MySQL运行内存不足时应采取的措施? 原文出处:<What To Do When MySQL Runs Out of Memory: Troubleshooting Guide> https://www.percona.com/blog/2018/06/28/what-to-do-when-mysql-runs-out-of-memory-troubleshooting-guide/ 原文作者:Alexander Rubin 关键词:memory.memory leaks

数据库表中存在Text类型的属性时,写sql语句时需要注意喽!

之前,习惯性地写查询语句时,查询条件用“=”判断.今天写程序的时候,查询时突然报了一个错误:数据类型text 和varchar 在equal to 运算符中不兼容.查看了一下数据库发现,其中有一个属性(例如Name:)是Text类型的,这时查询条件Name='张三' 就会报错.查找相关资料发现Text类型的属性不能用“=”判断相等,因为它不支持,可以用“like”判断,例如:Name like '张三' . 另外还有几点需要注意: (1):Text字段类型不能直接用replace函数来替换,必须

PL/SQL Developer中输入SQL语句时如何自动提示字段

在PL/SQL Developer中编写sql语句时,如果无法自动提示字段那是一件痛苦的事情,工作效率又低,在此演示下如何在PL/SQL Developer工具中自动提示字段,让开发者省时又省心,操作步骤如下:tools–>preferences–>user interface–>code assistant–>在右边选中authomatically activated即可,如 PL/SQL Developer中输入SQL语句时如何自动提示字段,布布扣,bubuko.com

nginx配置if错误语句时出错一例

server{ if (!-e $request_filename)        {                rewrite ^(.*)$ /index.php last;        } } 如果if 没有放在location时,在做memc缓存时,在错误日志里会提示以下信息 2014/05/12 10:45:51 [error] 11520#0: *325726 srcache_fetch: cache sent truncated response body while send

hive执行query语句时提示错误:org.apache.hadoop.ipc.RemoteException: java.io.IOException: java.io.IOException:

hive> select product_id, track_time from trackinfo limit 5; Total MapReduce jobs = 1 Launching Job 1 out of 1 Number of reduce tasks is set to 0 since there's no reduce operator org.apache.hadoop.ipc.RemoteException: java.io.IOException: java.io.IOEx

在做APP前端开发时应注意的一些问题

在做APP前端开发时应注意的一些问题 在整个app开发流程中,app前端开发是一个必不可少的环节,也是一个在app开发过程中重量级的角色.说到这,那么在app应用的前端开发中,又要注意什么问题呢?一.什么是app软件前端开发 App前端开发是移动前端开发中的一个方面,主要是指用户能够看到和接触到的app层面,比如app客户端界面,包括ios客户端和安卓客户端界面. App前端开发使用的技术是html+css+js,同时移动软件前端开发还需要基于PhoneGap等开发平台调用手机核心功能接口(包括

hive运行query语句时提示错误:org.apache.hadoop.ipc.RemoteException: java.io.IOException: java.io.IOException:

hive> select product_id, track_time from trackinfo limit 5; Total MapReduce jobs = 1 Launching Job 1 out of 1 Number of reduce tasks is set to 0 since there's no reduce operator org.apache.hadoop.ipc.RemoteException: java.io.IOException: java.io.IOEx

【Stackoverflow好问题】重写(Override)equlas和hashCode方法时应考虑的问题

问题 重写(Override)equlas和hashCode方法时应考虑哪些问题? 精华回答 理论上讲(偏程序语言和数学层面) equals() 定义了对象的相等关系(自反性.对称性.传递性)(有点抽象,更详细说明,请参考javadoc) . 另外,它还具有一致性(也就是说,如果一个对象没有修改,那么这个方法应总是返回相同的值),此外,o.equals(null)应当总是返回false. hashCode()(javadoc)也必须是一致性的(也就是说,如果equal的结果没有变,那么hashc