C语言教程
C语言控制语句
C语言函数
C语言数组
C语言指针
C语言字符串
C语言数学函数
C语言结构
C语言文件处理
C预处理器

C语言if else

C中的if-else语句用于根据某些特定条件执行操作。在且仅当给定条件为真时,才执行if块中指定的操作。
C语言中的if语句具有以下变体。
if语句 If-else语句 If-else-if 语句 if嵌套

If语句

if语句用于检查某些给定条件并根据该条件的正确性执行某些操作。它主要用于需要针对不同条件执行不同操作的场景。 if语句的语法如下。
if(expression){
//code to be executed
}
C中if语句的流程图
if c中的语句
看到一个简单的C语言if语句示例。
#include<stdio.h>  
int main(){  
int number=0;  
printf("Enter a number:");  
scanf("%d",&number);  
if(number%2==0){  
printf("%d is even number",number);  
}  
return 0;
}  
输出
Enter a number:4
4 is even number
enter a number:5

程序以查找三个中的最大数目。

#include <stdio.h>
int main()
{
    int a, b, c; 
     printf("Enter three numbers?");
    scanf("%d %d %d",&a,&b,&c);
    if(a>b && a>c)
    {
        printf("%d is largest",a);
    }
    if(b>a  && b > c)
    {
        printf("%d is largest",b);
    }
    if(c>a && c>b)
    {
        printf("%d is largest",c);
    }
    if(a == b && a == c) 
    {
        printf("All are equal"); 
    }
}
输出
Enter three numbers?
12 23 34
34 is largest 

If-else语句

if-else语句用于针对单个条件执行两项操作。 if-else语句是if语句的扩展,使用它我们可以执行两个不同的操作,即一个用于该条件的正确性,另一个用于条件的不正确性。在这里,我们必须注意,if and else块不能同时执行。总是最好使用if-else语句,因为它总是对每个if条件调用一个else情况。下面给出了if-else语句的语法。
if(expression){
//code to be executed if condition is true
}else{
//code to be executed if condition is false
}
C语言中if-else语句的流程图
c语言中的if
让我们看一个简单的示例,使用C语言中的if-else语句检查数字是偶数还是奇数。
#include<stdio.h>  
int main(){  
int number=0;  
printf("enter a number:");  
scanf("%d",&number);   
if(number%2==0){  
printf("%d is even number",number);  
}  
else{  
printf("%d is odd number",number);  
}   
return 0;
}  
输出
enter a number:4
4 is even number
enter a number:5
5 is odd number

检查某人是否有投票资格的程序。

#include <stdio.h>
int main()
{
    int age; 
    printf("Enter your age?"); 
    scanf("%d",&age);
    if(age>=18)
    {
        printf("You are eligible to vote..."); 
    }
    else 
    {
        printf("Sorry ... you can't vote"); 
    }
}
输出
Enter your age?18
You are eligible to vote...
Enter your age?13
Sorry ... you can't vote

If-else-if语句

if-else-if阶梯语句是if-else语句的扩展。在针对不同条件要执行多种情况的情况下使用它。在if-else-if阶梯语句中,如果条件为true,则将执行if块中定义的语句,否则,如果其他条件为true,则将最后执行else-if块中定义的语句。如果没有一个条件为真,则将执行else块中定义的语句。可能有多个else-if块。它类似于switch case语句,如果没有匹配的case,则执行默认值而不是else块。
if(condition1){
//code to be executed if condition1 is true
}else if(condition2){
//code to be executed if condition2 is true
}
else if(condition3){
//code to be executed if condition3 is true
}
...
else{
//code to be executed if all the conditions are false
}
C语言中else-if阶梯语句的流程图
if-else-if阶梯语句在c
下面给出了用C语言编写的if-else-if语句的示例。
#include<stdio.h>  
int main(){  
int number=0;  
printf("enter a number:");  
scanf("%d",&number);   
if(number==10){  
printf("number is equals to 10");  
}  
else if(number==50){  
printf("number is equal to 50");  
}  
else if(number==100){  
printf("number is equal to 100");  
}  
else{  
printf("number is not equal to 10, 50 or 100");  
}  
return 0;
}  
输出
enter a number:4
number is not equal to 10, 50 or 100
enter a number:50
number is equal to 50

根据指定分数计算学生成绩的程序。

#include <stdio.h>
int main()
{
    int marks; 
    printf("Enter your marks?");
    scanf("%d",&marks); 
    if(marks > 85 && marks <= 100)
    {
        printf("Congrats ! you scored grade A ..."); 
    }
    else if (marks > 60 && marks <= 85) 
    {
        printf("You scored grade B + ...");
    }
    else if (marks > 40 && marks <= 60) 
    {
        printf("You scored grade B ...");
    }
    else if (marks > 30 && marks <= 40) 
    {
        printf("You scored grade C ..."); 
    }
    else 
    {
        printf("Sorry you are fail ..."); 
    }
}
     
输出
Enter your marks?10
Sorry you are fail ...
Enter your marks?40
You scored grade C ...
Enter your marks?90
Congrats ! you scored grade A ...
     

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4