C++ 快速入门计划——第三天

C++ 循环 循环类型 :
While循环: 当给定条件为真时,重复语句或语句组。它会在执行循环主体之前测试条件。

#include <iostream>
using namespace std;
//while自加循环
int main ()
{
   // 局部变量声明
   int a = 10;
   // while 循环执行
   while( a < 20 )
   {
       cout << "a 的值:" << a << endl;
       a++;
   }
   return 0;
}

for循环:多次执行一个语句序列,简化管理循环变量的代码。


#include <iostream>
using namespace std;
//for自加循环
int main ()
{
   // for 循环执行 ; ;为无线循环
   for( int a = 10; a < 20; a = a + 1 )
   {
       cout << "a 的值:" << a << endl;
   }
   return 0;
}

do While循环:除了它是在循环主体结尾测试条件外,其他与 while 语句类似。

#include <iostream>
using namespace std;
 
int main ()
{
   // 局部变量声明
   int a = 10;
   // do 循环执行
   do
   {
       cout << "a 的值:" << a << endl;
       a = a + 1;
   }while( a < 20 ); //执行...当满足...
   return 0;
}

嵌套式循环: 您可以在 while、for 或 do…while 循环内使用一个或多个循环。

#include <iostream>
#include<math.h>

using namespace std;

int main()
{
    int i,j;
    for(i=2;i<100;i++)
    {
        for(j=2;j<=(double)sqrt((double)i);j++)
        {
            if(!(i%j))
                break;//不是质数
        }
        if(j>(double)sqrt((double)i))
            cout<<i<<"是质数\n";
    }
        return 0;
}

其他类型的嵌套语句模型:
while

while(condition)
{
   while(condition)
   {
      event(s);
   }
   event(s); // 可以放置更多的语句
}

do while

do
{
   event(s); // 可以放置更多的语句
   do
    {
      event(s);
    }while( condition );
}while( condition );

控制类型的语句
break:当 break 语句出现在一个循环内时,循环会立即终止,且程序流将继续执行紧接着循环的下一条语句。若是出现在case后面则只是终止其中一个case。

#include <iostream>
using namespace std;
 
int main ()
{
   // 局部变量声明
   int a = 10;
   // do 循环执行
   do
   {
       cout << "a 的值:" << a << endl;
       a = a + 1;
       if( a > 15)
       {
          // 终止循环
          break;
       }
   }while( a < 20 );
 
   return 0;
}

continue:continue 会跳过当前循环中的代码,强迫开始下一次循环。对于 for 循环,continue 语句会导致执行条件测试和循环增量部分。对于 while 和 do…while 循环,continue 语句会导致程序控制回到条件测试上。

#include <iostream>
using namespace std;
 
int main ()
{
   // 局部变量声明
   int a = 10;
   // do 循环执行
   do
   {
       if( a == 15)
       {
          a = a + 1;
          continue;// 跳过迭代
       }
       cout << "a 的值:" << a << endl;
       a = a + 1;
   }while( a < 20 );
return 0;
}

C++ 判断
if

#include <iostream>
using namespace std;
 
int main ()
{
   int a = 10;
   if( a < 20 )
   {
       cout << "a 小于 20" << endl;
   }
   cout << "a 的值是 " << a << endl;
   return 0;
}

if…else

#include <iostream>
using namespace std;
 
int main ()
{
   int a = 100;
   if( a < 20 )
   {
       cout << "a 小于 20" << endl; //真
   }
   else if( a == 20 )
   {
       cout << "a 的值是 20" << endl; //if与else中间是可以加无限个else if的
   }
   else
   {
       cout << "a 大于 20" << endl; //假
   }
   cout << "a 的值是 " << a << endl;
 
   return 0;
}

嵌套if

#include <iostream>
using namespace std;
 
int main ()
{
   int a = 100;
   int b = 200;
   
   if( a == 100 )
   {
       if( b == 200 )
       {
          cout << "a 的值是 100,且 b 的值是 200" << endl;
       }
   }
   cout << "a 的准确值是 " << a << endl;
   cout << "b 的准确值是 " << b << endl;
 
   return 0;
}

switch:一个 switch 语句允许测试一个变量等于多个值时的情况。每个值称为一个 case,且被测试的变量会对每个 switch case 进行检查。
规则:

  • switch 语句中的 expression 必须是一个整型或枚举类型,或者是一个 class 类型,其中 class 有一个单一的转换函数将其转换为整型或枚举类型。
  • 在一个 switch 中可以有任意数量的 case 语句。每个 case 后跟一个要比较的值和一个冒号。
  • case 的 constant-expression 必须与 switch 中的变量具有相同的数据类型,且必须是一个常量或字面量。
  • 当被测试的变量等于 case 中的常量时,case 后跟的语句将被执行,直到遇到 break 语句为止。
  • 当遇到 break 语句时,switch 终止,控制流将跳转到 switch 语句后的下一行。
  • 不是每一个 case 都需要包含 break。如果 case 语句不包含 break,控制流将会 继续 后续的 case,直到遇到 break 为止。
  • 一个 switch 语句可以有一个可选的 default case,出现在 switch 的结尾。default case 可用于在上面所有 case 都不为真时执行一个任务。default case 中的 break 语句不是必需的。
#include <iostream>
using namespace std;
 
int main ()
{
   // 局部变量声明
   char grade = 'D';
   
   switch(grade)
   {
   case 'A' :
      cout << "很棒!" << endl; 
   break;
   
   case 'B' :
   
   case 'C' :
      cout << "做得好" << endl;
   break;
   
   case 'D' :
      cout << "您通过了" << endl;
   break;
   
   case 'F' :
      cout << "最好再试一下" << endl;
   break;
   
   default :
      cout << "无效的成绩" << endl;
   }
   cout << "您的成绩是 " << grade << endl;
return 0;
}

switch嵌套:可在一个 switch内使用另一个 switch 语句。

#include <iostream>
using namespace std;
 
int main ()
{
   int a = 100;
   int b = 200;
 
   switch(a) {
      case 100: 
         cout << "这是外部 switch 的一部分" << endl;
         switch(b) {
            case 200:
               cout << "这是内部 switch 的一部分" << endl;
                    }
               }
   cout << "a 的准确值是 " << a << endl;
   cout << "b 的准确值是 " << b << endl;
   return 0;
}

C++ 函数 (重点)
函数声明告诉编译器函数的名称、返回类型和参数。函数定义提供了函数的实际主体。C++ 标准库提供了大量的程序可以调用的内置函数。例如,函数 strcat() 用来连接两个字符串,函数 memcpy() 用来复制内存到另一个位置。函数还有很多叫法,比如方法、子例程或程序,等等。
例如:

int max(int num1, int num2) //int返回类型 , max函数名称(函数定义)
//int max(int , int) 在这种也可以
{
   // 局部变量声明(函数主体)
   int result;
 
   if (num1 > num2) //比较大小
      result = num1;
   else
      result = num2;
 
   return result; 
}
#include <iostream>
using namespace std;
 
// 函数声明
int max(int num1, int num2);
 
int main ()
{
   int a = 100;
   int b = 200;
   int ret;
   // 调用函数来获取最大值
   ret = max(a, b);
 
   cout << "Max value is : " << ret << endl;
 
   return 0;
}
 
// 函数定义
int max(int num1, int num2) 
{
   int result;
 
   if (num1 > num2)
      result = num1;
   else
      result = num2;
 
   return result; 
}

函数参数:如果函数要使用参数,则必须声明接受参数值的变量。这些变量称为函数的形式参数。形式参数就像函数内的其他局部变量,在进入函数时被创建,退出函数时被销毁。当调用函数时,有三种向函数传递参数的方式:

第一种——传值调用:该方法把参数的实际值复制给函数的形式参数。在这种情况下,修改函数内的形式参数对实际参数没有影响。一般来说,这意味着函数内的代码不会改变用于调用函数的实际参数(通过swap交换函数)

#include <iostream>
using namespace std;
 
// 函数声明
void swap(int x, int y);
 
int main ()
{
   int a = 100;
   int b = 200;
 
   cout << "交换前,a 的值:" << a << endl;
   cout << "交换前,b 的值:" << b << endl;

   swap(a, b);   // 调用函数来交换值
 
   cout << "交换后,a 的值:" << a << endl;
   cout << "交换后,b 的值:" << b << endl;
 
   return 0;

	//下面为函数定义
	/*
	void swap(int x, int y)
      {
         int temp;
  		 	temp = x; 
  		 	x = y;    
  		 	y = temp; 
  		 return;
	   }
	*/
}

第二种——指针调用:把参数的地址复制给形式参数。在函数内,该地址用于访问调用中要用到的实际参数。这意味着,修改形式参数会影响实际参数。按指针传递值,参数指针被传递给函数,就像传递其他值给函数一样。因此相应地,在下面的函数 swap() 中,需提前声明函数参数为指针类型,该函数用于交换参数所指向的两个整数变量的值。

#include <iostream>
using namespace std;

// 函数声明
void swap(int *x, int *y);

int main ()
{
   int a = 100;
   int b = 200;
 
   cout << "交换前,a 的值:" << a << endl;
   cout << "交换前,b 的值:" << b << endl;

   //&a 表示指向 a 的指针,即变量 a 的地址 
   swap(&a, &b); 

   cout << "交换后,a 的值:" << a << endl;
   cout << "交换后,b 的值:" << b << endl;
 
   return 0;
}

/* 函数定义
	void swap(int *x, int *y)
	{
   		int temp;
   			temp = *x;    //保存为x的地址
   			*x = *y;      //把 y 赋值给 x 
   			*y = temp;    //把 x 赋值给 y
  		 return;
	}
*/

第三种——引用调用:该方法把参数的引用复制给形式参数。在函数内,该引用用于访问调用中要用到的实际参数。这意味着,修改形式参数会影响实际参数。

#include <iostream>
using namespace std;
 
void swap(int &x, int &y);
 
int main ()
{
   int a = 100;
   int b = 200;
 
   cout << "交换前,a 的值:" << a << endl;
   cout << "交换前,b 的值:" << b << endl;
 
   swap(a, b);
 
   cout << "交换后,a 的值:" << a << endl;
   cout << "交换后,b 的值:" << b << endl;
 
   return 0;
}

/* 函数定义
	void swap(int &x, int &y)
	{
   		int temp;
   			temp = x;    //保存为x的地址
   			x = y;      //把 y 赋值给 x 
   			y = temp;    //把 x 赋值给 y
  		 return;
	}
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值