建议一下代码根据注释一段一段敲,改变数值去理解每一个语法的奥妙...............
# include<stdio.h>
#include "stdafx.h"
#include "iostream"
using namespace std;
void function();
int main()
{
function();
return 0;
}
void function(){
//判断的
/*-------if语句(判断a的值)-------*/
int a; //定义一个变量a
a = 1; //给a赋值=1
if (a = 1)
{
cout << "a的值是1\n";
}
else
{
}
if (a != 1)
{
cout << "a的值不是1\n";
}
else
{
cout << "a的值是" << a<<"\n";
}
/*-------switch语句(判断成绩为abcde的分数)-------*/
char grade = 'b';
switch (grade)
{
case 'a': cout << "90--100\n"; break;
case 'b': cout << "80--99\n"; break;
case 'c': cout << "70--79\n"; break;
case 'd': cout << "60--69\n"; break;
case 'e': cout << "<60\n"; break;
default:cout << "error\n";
}
/*-------if和else if的搭配相当于switch(判断成绩为abcde的分数)-------*/
if (grade == 'a')
{
cout << "90--100\n";
}
else if (grade == 'b')
{
cout << "80--99\n";
}
else if (grade == 'c')
{
cout << "70--79\n";
}
else if (grade == 'd')
{
cout << "60--69\n";
}
else if (grade == 'e')
{
cout << "<60\n";
}
else
{
cout << "error\n";
}
//循环语句
/*-------for循环(从1循环到10输出3遍)-------*/
for (int i = 1; i <= 3; i++)
{
//cout << i<<"\n";
for (int j = 1; j <= 10; j++)
{
cout << j << "\n";
}
}
/*-------while循环(从100循环到110)-------*/
int num = 100;
while (num<=110)
{
cout << num << "\n";
++num;
}
/*-------do-while循环(当num_1结果等于103的时候输出我是103)-------*/
int num_1 = 100;
do{
num_1++;
}
while (num_1 == 103);
cout << "我是103\n";
system("pause");
}