在C语言中定义常量的两种方式

在C语言编程中定义常量有两种方法。

  • const关键字
  • #define预处理器

1. const关键字

const关键字用于定义C语言编程中的常量。

const float PI=3.14;
C

现在,PI变量的值不能改变。

1. if语句

if语句的语法如下 -

if(expression){
//code to be executed
}

我们来看一个简单的c语言if语句的示例代码,创建一个源文件:if-statement.c,代码如下所示 -

#include<stdio.h>
#include<conio.h>
void main() {
int number = 0; printf("enter a number:");
scanf("%d", &number); if (number % 2 == 0) {
printf("%d is even number\n", number);
} }
C

执行上面示例代码,得到以下结果 -

enter a number:100
100 is even number

2.if-else语句

如果condition为true或false都要执行对应代码块,则可使用C语言中的if-else语句来实现。if-else语句的语法如下:

if(expression){
//code to be executed if condition is true
}else{
//code to be executed if condition is false
}

if else-if语句

if else-if语句用于从多个条件执行一个代码。 if else-if语句的语法如下:

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
}

4.嵌套if

嵌套if语句就是在一个if语句中嵌套一个或多个if语句,创建一个源文件:nested_if.c,参考如下示例代码:

#include<stdio.h>  

void main() {
int score = 0;
printf("enter a score:");
scanf("%d", &score); if (score >= 60) { // 下面是嵌套if-else语句
if (score <= 80) {
printf("分数大于60小于80,中等水平\n");
}else if (score > 80 && score < 90) {
printf("分数大于60小于80,成绩良好\n");
}else{// 大于 90 以上
printf("分数大于90,成绩优秀\n");
}
}else {
printf("分数小于 60 分,不及格~!\n");
}
}

C语言中的switch语句用于从多个条件执行代码。 就像if else-if语句一样。

C语言中switch语句的语法如下:

switch(expression){
case value1:
//code to be executed;
break; //optional
case value2:
//code to be executed;
break; //optional
...... default:
code to be executed if all cases are not matched;
}
C

C语言中switch语句的规则如下 -

  1. switch表达式必须是整数或字符类型。
  2. case值必须是整数或字符常量。
  3. case值只能在switch语句中使用。
  4. switch case中的break语句不是必须的。这是一个可选项。 如果在switch case中没有使用break语句,则匹配case值后将执行所有后的语句。它被称为通过C语言switch语句的状态。
有效的Switch 无效的Switch 有效的Case 无效的Case
switch(x) switch(f) case 3; case 2.5;
switch(x>y) switch(x+2.5) case ‘a’; case x;
switch(a+b-2)   case 1+2; case x+2;
switch(func(x,y))   case ‘x’>’y’; case 1,2,3;

switch语句直通到尾

在C语言中,switch语句是通过的,这意味着如果在switch case中不使用break语句,则匹配某个case之后的所有的case都将被执行。

我们来试试通过下面的例子来了解switch语句的状态。创建一个源文件:switch-fall-through.c,其代码如下所示 -

#include<stdio.h>
#include<conio.h>
void main() {
int number = 0; printf("enter a number:");
scanf("%d", &number); switch (number) {
case 10:
printf("number is equals to 10\n");
case 50:
printf("number is equal to 50\n");
case 100:
printf("number is equal to 100\n");
default:
printf("number is not equal to 10, 50 or 100\n");
}
}
C

执行上面示例代码,得到以下结果 -

enter a number:10
number is equals to 10
number is equal to 50
number is equal to 100
number is not equal to 10, 50 or 100
请按任意键继续. . .

C语言中的循环用于执行代码块或程序的一部分多次。换句话说,它多次迭代代码或代码组。

为什么使用C语言中的循环?

假设你必须打印一个二维的表格,那么你可需要编写10行代码。

但是,如果通过使用循环语句,您只能通过2或3行代码来实现。

C语言循环的优点

  1. 它节省(减少)代码量。
  2. 它有助于遍历数组的元素(这在下一页中介绍)。

C语言的循环类型

C语言中有三种类型的循环,如下所示:

  1. do while
  2. while
  3. for

1. do-while循环

它迭代代码,直到条件(condition)为false。 这里,条件(condition)是在代码之后给出的。所以循环体至少一次,而不管条件(condition)求值是真还是假。

如果你希望代码必须至少执行一次,那使用do-while循环是一个不错的选择。

C语言中do-while循环的语法如下:

do{
//code to be executed
}while(condition);
C

有关do-while循环的流程图和示例,请参考阅读:http://www.yiibai.com/cprogramming/do-while-loop-in-c.html

2. while循环

像do while循环一样,它迭代代码,直到条件为假。 这里,条件(condition)是在代码之前给出的。所以代码可能一次都不执行。

如果用户不知道迭代次数,则使用while循环更好一些。

C语言中while循环的语法如下:

while(condition){
//code to be executed
}
C

有关while循环的流程图和示例,请参考阅读:http://www.yiibai.com/cprogramming/while-loop-in-c.html

3. for循环

像while循环一样,它迭代代码,直到条件(condition)为false。 这里,在代码之前给出初始化,条件和增量/减量。所以代码可能一次都不执行。

如果用户知道迭代次数,使用for循环是一种比较好选择。

C语言中for循环的语法如下:

for(initialization;condition;incr/decr){
//code to be executed
}

要执行程序或代码的一部分几次或多次,我们可以使用C语言的do-while循环。 在do和while之间给出的代码将被执行,直到条件(condition)成为true。

在do-while循环中,语句在条件之前给出,所以语句或代码将至少有一次执行。换句话说,我们可以说do-while循环执行语句一次或多次。

如果你希望至少执行一次代码,使用do-while循环是最好不过的选择。

do-while循环语法

C语言do-while循环的语法如下:

do{
//code to be executed
}while(condition);

do-while循环的例子

下面给出了C语言的简单程序,while循环来打印连续的数据。创建一个源文件:do-while-example.c,其代码如下所示 -

#include <stdio.h>
#include <conio.h>
void main() {
int i = 1, number = 0;
printf("Enter a number: ");
scanf("%d", &number);
do {
printf("%d \n", (i));
i++;
} while (i <= number);
}
C

执行上面代码,得到以下结果 -

Enter a number: 12
1
2
3
4
5
6
7
8
9
10
11
12
请按任意键继续. . .

无限do-while循环

如果在do while循环中使用条件表达式的值1,则它将运行无限次数。

do{
// 要执行的语句
}while(1);

C语言中的while循环用于多次迭代程序或语句的一部分。

在while循环中,条件在语句之前给出。 所以它与do while循环有点不同,while循环可能一次不会执行语句,而do while循环至少循环一次。

当C语言中使用while循环时

如果迭代次数不确定或未知,则应优先考虑使用C语言while循环。

C语言中while循环的语法

c语言中while循环的语法如下:

while(condition){
//code to be executed
}
C

C语言中的while循环的流程图,如下所示 -

C语言中while循环的例子

下面来看看看打印表的一个while循环程序。创建一个源文件:while-example.c,其源代码如下所示 -

#include <stdio.h>
#include <conio.h>
void main() {
int i = 1; while (i <= 10) {
printf("%d \n", i);
i++;
} }
C

执行上面示例代码,得到以下结果 -

1
2
3
4
5
6
7
8
9
10

C语言中的无限循环

如果在while循环中传递1作为条件,则程序将运行无限次数。

while(1){
//statement
}

goto语句被称为C语言中的跳转语句。用于无条件跳转到其他标签。它将控制权转移到程序的其他部分。

goto语句一般很少使用,因为它使程序的可读性和复杂性变得更差。

语法

goto label;
C

goto语句示例

让我们来看一个简单的例子,演示如何使用C语言中的goto语句。

打开Visual Studio创建一个名称为:goto的工程,并在这个工程中创建一个源文件:goto-statment.c,其代码如下所示 -

#include <stdio.h>
void main() {
int age; gotolabel:
printf("You are not eligible to vote!\n"); printf("Enter you age:\n");
scanf("%d", &age);
if (age < 18) {
goto gotolabel;
}else {
printf("You are eligible to vote!\n");
} }
You are not eligible to vote!
Enter you age:
12
You are not eligible to vote!
Enter you age:
18
You are eligible to vote!


C常量与控制语句的更多相关文章

  1. Java程序员的Golang入门指南(下)

    Java程序员的Golang入门指南(下) 4.高级特性 上面介绍的只是Golang的基本语法和特性,尽管像控制语句的条件不用圆括号.函数多返回值.switch-case默认break.函数闭包.集合 ...

  2. vim-colors-config

    在vim中,主题是以插件形式存在.其中系统自带的主题,存放在$VIMRUNTIME/colors文件夹下,以*.vim命名.(注:查看$VIMRUNTIME请在vim中执行 :echo $VIMRUN ...

  3. Oracle 练习

    --简单的select语句select deptno,dname,loc from DEPT where deptno='40';--描述表结构 部门表desc dept;--雇员表desc emp; ...

  4. 速战速决 (1) - PHP: 概述, 常量, 变量, 运算符, 表达式, 控制语句

    [源码下载] 速战速决 (1) - PHP: 概述, 常量, 变量, 运算符, 表达式, 控制语句 作者:webabcd 介绍速战速决 之 PHP 概述 常量 变量 运算符 表达式 控制语句 示例1. ...

  5. 点评阿里JAVA手册之编程规约(命名风格、常量定义、代码风格、控制语句、注释规约)

    下载原版阿里JAVA开发手册  [阿里巴巴Java开发手册v1.2.0] 本文主要是对照阿里开发手册,注释自己在工作中运用情况. 本文难度系数为一星(★) 码出高效.码出质量. 代码的字里行间流淌的是 ...

  6. PL/sql中如何声明变量,常量,控制语句及for,loop,while和顺序控制的使用

    pl/sql 什么是PL/SQL PL/SQL是结合oracle过程语言和机构化查询运行(SQL) 的一种扩展语言.使用PL/SQL可以编写具有很多高级功能的程序,有以下优点 PL/SOL可以采用过程 ...

  7. python27期day01:变量、常量、注释、PEP8开发规范、数据类型、Python2和Python3的区别、用户输入、流程控制语句、作业题

    1.变量:将程序中运行的中间值临时存储起来,以便下次使用. 2.变量命名规范:数字.字母.下划线.建议驼峰体.变量名具有可描述性.不能使用中文和拼音.不能数字开头和使用关键字('and', 'as', ...

  8. day 01 常量 注释 int(整型) 用户交互input 流程控制语句if

    python的编程语言分类(重点) if 3 > 2: 编译型: 将代码一次性全部编译成二进制,然后再执行. 优点:执行效率高. 缺点:开发效率低,不能跨平台. 代表语言:C 解释型: 逐行解释 ...

  9. python大法好——变量、常量、input()、数据类型、字符串、格式化输出、运算符、流程控制语句、进制、字符编码

    python基础知识 1.变量 变量:把程序运算的中间结果临时存到内存里,以备后面的代码可以继续调用. 作用:A.存储数据. B.标记数据. 变量的声明规则: A:变量名只能是字母,数字或下划线任意组 ...

随机推荐

  1. bzoj1452

    题解: 二位树状数组 然后我开了300*300*300就T了 代码: #include<bits/stdc++.h> using namespace std; ; ],q; int fin ...

  2. PHP爬虫

    使用PHP的curl扩展抓取页面数据 PHP的curl扩展是PHP支持的允许你与各种服务器使用各种类型的协议进行连接和通信的库. 本程序是抓取知乎的用户数据,要能访问用户个人页面,需要用户登录后的才能 ...

  3. 演示stop暴力停止线程导致数据不一致的问题,但是有些有趣的发现 (2017-07-03 21:25)

    如注释所言 /** * Created by weiwei22 on 17/7/3. * * 这里主要是为了演示stop导致的数据不一致的问题.stop会暴力的结束线程并释放锁,所以有可能在恰好写了一 ...

  4. ef core code frist

    https://docs.microsoft.com/zh-cn/ef/core/get-started/aspnetcore/new-db?view=aspnetcore-2.1 1.先创建对应的实 ...

  5. 1093 字符串A+B

    给定两个字符串 A 和 B,本题要求你输出 A+B,即两个字符串的并集.要求先输出 A,再输出 B,但重复的字符必须被剔除. 输入格式: 输入在两行中分别给出 A 和 B,均为长度不超过 10​6​​ ...

  6. 一、JAVA变量类型:②局部变量

    局部变量声明在方法.构造方法或者语句块中: public class Test{ public void pupAge(){ int age = 0; //定义age为方法pupAge中的局部变量,并 ...

  7. lvs三种模式的优缺点对比

    电面只回答上来少部分,所以......恶补.总结

  8. restful规范整理

    restful的十条规范 restful一共有十条规范,但其并不是规定.可以不去遵守,是一种软件风格 1.API与客户端交互,通常使用https协议 2.域名:https://api.baidu.co ...

  9. 1159 Palindrome

    Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 68562   Accepted: 23869 Desc ...

  10. 【Java】字符串工具类

    import android.annotation.SuppressLint; import java.io.UnsupportedEncodingException; import java.uti ...