bread,return,continue的区别

本文详细解释了break、continue、goto和return四种控制流语句的功能及使用场景,包括它们在循环、条件判断中的作用及区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一。break

    1. break语句形式: break;
    2. break语句功能:
A. switch语句中,break语句会终止其后语句的执行,退出switch语句。
B. 使一个循环立即结束,也就是说在循环中遇到break语句时,循环立即终止,
程序转到循环体后的第一个语句去继续执行。
    3. 注:
A. break语句在循环中使用时,总是与if一起使用,当条件满足(或不满足)
时,负责退出循环。
       B. 如果循环体中使用switch语句,而break出现在switch语句中,则它只用
于结束switch,而不影响循环。
       C. break语句只能结束包含它的最内层循环,而不能跳了多重循环。
    4. 例: break语句的执行只能从while循环中退出,继续执行for循环的其它语句
而不是退出外层循环。
       for()

       {

           :

           :

           while()

           {

              :

              :

              if() break;

              :

              :

           }

           :

           :

       }

 

 

二。continue   

    1.continue语句形式: continue;
    2.continue语句功能:   它只能出现在循环体中,其功能是立即结束本次循环,
即遇到continue语句时,不执行循环体中continue后的语句,立即转去判断循环条件是否成立。  

3.Continue与break语句的区别: continue只是结束本次循环,而不是终止整个循
环语句的执行,break则是终止整个循环语句的
执行,转到循环语句后的下一条语句去执行。
     程序表达式及流程图如下:
(1.)        while(表达式1)                  (2.)while(表达式1)

{                                    {

      :                                   :

           If(表达式2) break;                  if(表达式2) continue;

           :                                    :

       }                                    }

 

 

三.goto

1.goto语句形式:goto语句是无条件转向语句,其一般形式为:
                 goto 语句标号;

    2.功能:goto语句往往用来从多重循环中跳出。它在解决一些特定问题时很方便,但由于goto语句难于控制,尽量少用。
    3.例:
           IN:

              For()

              {

                   :

                   :

                   Goto IN;

               }

             

   

四.Return

   

1.return语句形式: return (返回值);

    2.功能:return可以返回一个函数的值,并且跳出这个函数;

 

      Void doSomething(){

       do whatever is required by this method …

       return;

    }
 
只要遇到return语句,程序就在那一行代码停止执行,执行控制将立刻返回到调用该程序的代码处。
对于返回值类型为void的程序,return关键字单独作为完整的语句使用:
return;
 
1.    对于返回类型为void的程序,return;语句是可选的。如果省略这条语句,隐含表明程序的最后一行有一个return;语句。即,下面两个版本的doSomething程序是等价的:
 
void doSomething(){

     int x=3;

     int y=4;

     int x=x+y;

}


void doSomething(){

     int x=3;

     int y=4;

     int x=x+y;

     return;

}

 

2.    对于返回类型非void的程序体,必须至少包括一条明确的return语句。这种情况下,return关键字后面必须跟随一个求值类型和程序声明的返回类型一致的表达式。例如,如果程序定义为具有int返回类型,那么下列任何一种return语句都可以接受:
 
return 0; //returning a constant integer value

return x; //returning the value of x(assuming that x has previously been declared to be an int)

return x+y; //returning the value of the expression”x+y”(here,we`re assuming that “x+y” evaluates to an int value)

return (int)z; //casting the value of z(assume z was declared as a double to an int value)
 
3.    如果程序定义为具有boolean返回类型,那么下列任何一种return语句都可以接受:
 
return false;   //returning a Boolean constant value

return outcome; //returning the value of variable outcome (assuming that outcome has previously been declared to be of type Boolean)

return(x<3);    //returning the Boolean value that results when the value of x is compared to 3: if x if less than 3, this method returns a value of true; otherwise, it returns false.
 
程序体可以包含不只一条return语句。但好的编程习惯是一个程序中只在最末尾包含一条return语句。再看一下前面讨论过的isHornorsStudent程序,这个程序有两条return语句:
boolean isHonorsStudent(){

if(gpa>=3.5) return true; //first return statement

else return false; //second return statement

}

 

使用局部boolean型变量result来重写这个程序,该变量捕获最终返回的true/false结果。在程序最末尾用一条return语句返回result变量的值:
boolean isHonorsStudent(){

boolean result = false;

if(gpa>=3.5) result = true;

else result = false;

return result;

}

如上述代码所示,由于已经给result变量赋予初值false,因此在else子句中明确地给它赋值false是不必要的,可以像下面这样简化isHornorsStudent程序:
boolean isHonorsStudent(){

boolean result = false;

if(gpa>=3.5) result = true;

return result;

}
 
但是有一种情况下多个return语句是可以接受的:程序需要执行一系列操作,在这个过程中,任何一步失败意味着整个程序失败。下面的伪代码阐明了这种情况:
 
function cs(n){

if(n==1) { retrun 1; }

if(n==2) { return; }

else { }

}

说明:   当参数为1时函数返回值为1并且跳出函数,

                当参数为2时函数返回值为空并且跳出函数,

                当函数为其他值时继续执行函数下面的语句,直到遇到下个return或则全部执行完语句在跳出函数


本文来自优快云博客,转载请标明出处:http://blog.youkuaiyun.com/wai631/archive/2009/07/24/4376219.aspx

namespace SandwichCalories { class Program { static void Main(string[] args) { // sandwich ingredients and their associated calories Dictionary<string, int> ingredients = new Dictionary<string, int>() { { "Bread", 100 }, { "Ham", 150 }, { "Lettuce", 10 }, { "Tomato", 20 }, { "Mayonnaise", 50 }, { "Cheese", 120 } }; // prompt user for calorie range Console.Write("Enter minimum calories: "); int min_calories = int.Parse(Console.ReadLine()); Console.Write("Enter maximum calories: "); int max_calories = int.Parse(Console.ReadLine()); // calculate the minimum and maximum calories for the sandwich int min_sandwich_calories = 2 * ingredients["Bread"] + ingredients.Values.Min() * 2; int max_sandwich_calories = 2 * ingredients["Bread"] + ingredients.Values.Max() * 2; // check if the calorie range is valid if (max_calories < min_sandwich_calories) { Console.WriteLine("Sorry, it is impossible to create a sandwich within the given calorie range."); } else { // create the sandwich List<string> sandwich = new List<string> { "Bread", "Bread" }; int sandwich_calories = 2 * ingredients["Bread"]; while (sandwich_calories < min_calories) { // add random ingredient string ingredient = ingredients.Keys.ElementAt(new Random().Next(ingredients.Count)); sandwich.Add(ingredient); sandwich_calories += ingredients[ingredient]; } while (sandwich_calories <= max_calories) { // add random ingredient string ingredient = ingredients.Keys.ElementAt(new Random().Next(ingredients.Count)); // check if the ingredient is the same as the previous one if (sandwich.Count >= 3 && ingredient == sandwich[sandwich.Count - 2]) { continue; } sandwich.Add(ingredient); sandwich_calories += ingredients[ingredient]; // check if the sandwich is already at the maximum calorie limit if (sandwich_calories == max_sandwich_calories) { break; } } // add the last slice of bread sandwich.Add("Bread"); // print the sandwich and its total calories Console.WriteLine("Your sandwich: " + string.Join(", ", sandwich)); Console.WriteLine("Total calories: " + sandwich_calories); } } } } 改进代码
06-10
按照我的要求给出不报错的代码,VS2019,PCL版本为1.10.1,给出完整代码,这些代码用于测量行李箱的长宽高,误差要在1cm以内,尽可能优化它,双目摄像头垂直在行李箱上方160cm左右,行李箱位于传送带上,在我给出的代码上进行二次开发 main: #include "gvCamera.h" #include <iostream> int main(int argc, char* argv[]) { std::cout << " **************************************** " << std::endl; std::cout << " ************** GV_D100 ***************** " << std::endl; std::cout << " **************************************** " << std::endl; std::shared_ptr<GvCamera> gvCamera = std::make_shared<GvCamera>(); bool irSwich = false; bool depthSwitch = true; bool rgbSwitch = true; bool alignDepthSwitch = false; gvCamera->setIRSwitch(irSwich); gvCamera->setDepthSwitch(depthSwitch); gvCamera->setRGBSwitch(rgbSwitch); if (alignDepthSwitch && depthSwitch) gvCamera->setDepthAlignSwitch(alignDepthSwitch); gvCamera->Preview3DCamera(); std::vector<float> calibData = gvCamera->getCalibrationParams(); std::cout << "Calibration Params : " << calibData[0] << " " << calibData[1] << " " << calibData[2] << " " << calibData[3] << std::endl; cv::Mat leftCameraMatrix = gvCamera->getLeftCameraMatrix(); std::cout << "leftCameraMatrix : " << leftCameraMatrix << std::endl; cv::Mat leftDistCoeffs = gvCamera->getLeftDistCoeffs(); std::cout << "leftDistCoeffs : " << leftDistCoeffs << std::endl; cv::Mat rightCameraMatrix = gvCamera->getRightCameraMatrix(); std::cout << "rightCameraMatrix : " << rightCameraMatrix << std::endl; cv::Mat rightDistCoeffs = gvCamera->getRightDistCoeffs(); std::cout << "rightDistCoeffs : " << rightDistCoeffs << std::endl; cv::Mat stereoRotation = gvCamera->getStereoRotation(); std::cout << "stereoRotation : " << stereoRotation << std::endl; cv::Mat stereoTranslation = gvCamera->getStereoTranslation(); std::cout << "stereoTranslation : " << stereoTranslation << std::endl; cv::Mat rgbCammeraMatrix = gvCamera->getRgbCameraMatrix(); std::cout << "rgbCameraMatrix : " << rgbCammeraMatrix << std::endl; cv::Mat rgbDistCoeffs = gvCamera->getRgbDistCoeffs(); std::cout << "rgbDistCoeffs : " << rgbDistCoeffs << std::endl; int waitTime = 0; while (true) { waitTime++; if (gvCamera->getIRSwitch()) { cv::Mat leftIr = gvCamera->getLeftIRImage(); cv::Mat rightIr = gvCamera->getRightIRImage(); if (!leftIr.empty()) cv::imshow("leftIr", leftIr); if (!rightIr.empty()) cv::imshow("rightIr", rightIr); } if (gvCamera->getDepthSwitch()) { cv::Mat depth = gvCamera->getDepthImage(); if (!depth.empty()) { //cv::imshow("depth", depth); cv::Mat nmt, colored; cv::convertScaleAbs(depth, nmt, 0.1); cv::applyColorMap(nmt, colored, cv::COLORMAP_JET); cv::imshow("depth ", colored); } } if (gvCamera->getRGBSwitch()) { cv::Mat rgb = gvCamera->getRgbImage(); if (!rgb.empty()) cv::imshow("rgb", rgb); if (waitTime > 5) { if (gvCamera->getDepthSwitch() && gvCamera->getRGBSwitch() && gvCamera->getDepthAlignSwitch()) { // 获取单通道的深度图 cv::Mat alignDepth = gvCamera->getAlignDepthImage(); if (!alignDepth.empty()) { cv::imshow("alignDepth", alignDepth); } cv::Mat alignColorDepth = gvCamera->getAlignColorDepthImage(); if (!alignColorDepth.empty()) cv::imshow("alignColorDepth", alignColorDepth); // 获取三通道的深度图 cv::Mat alignPointCloud = gvCamera->getAlinePointCloud(); if (!alignPointCloud.empty()) cv::imshow("alignPointCloud", alignPointCloud); } } } cv::waitKey(10); } return 0; } gvcamera.h: #include <dshow.h> #include "qedit.h" #include <vector> #include <functional> #include <Windows.h> #include <string> #include <iostream> #include <comutil.h> #include <atlstr.h> #include <opencv.hpp> #include <highgui.hpp> #include <iostream> #include "camera.hpp" #include <condition_variable> #include "VideoCapture.h" #include "SendDataByUSB.h" #include "depth2color.h" #pragma comment(lib, "strmiids") #pragma comment(lib, "comsupp.lib") #define IMAGE_W 640 #define IMAGE_H 400 class GvCamera { public: GvCamera(); ~GvCamera(); void Preview3DCamera(); void Stop3DCamera(); void initialVideo(); void processFrame(const unsigned char* buff, int len, VideoDevice* device); //void depth2RgbPointCloud(const cv::Mat& depthImage, const cv::Mat& color, const cv::Mat& cameraMatix, pcl::PointCloud<pcl::PointXYZRGB>::Ptr& pointcloud); //void depth2PointCloud(const cv::Mat& depthImage, const cv::Mat& cameraMatix, pcl::PointCloud<pcl::PointXYZ>::Ptr& pointcloud); //void ReadWriteConfig(bool bRead, bool bOpenRGB = false); bool ReadCameraCalibrationFactor(); bool ReadIR2IRCalibrationParams(); bool ReadIR2RGBCalibrationParams(); inline void setIRSwitch(bool showIR) { bShowIR = showIR; } inline bool getIRSwitch() { return bShowIR; } inline void setRGBSwitch(bool showRGB) { bShowRGB = showRGB; } inline bool getRGBSwitch() { return bShowRGB; }; inline void setDepthSwitch(bool showDepth) { bShowDepth = showDepth; } inline bool getDepthSwitch() { return bShowDepth; }; inline void setDepthAlignSwitch(bool showDepthAlign) { bShowDepthAlign = showDepthAlign; } inline bool getDepthAlignSwitch() { return bShowDepthAlign; }; //getImage inline cv::Mat getLeftIRImage() { std::lock_guard<std::mutex> lock(dataLock_); return leftIRImage_; }; inline cv::Mat getRightIRImage() { std::lock_guard<std::mutex> lock(dataLock_); return rightIRImage_; }; inline cv::Mat getDepthImage() { std::lock_guard<std::mutex> lock(dataLock_); return depthImage_; }; inline cv::Mat getAlignDepthImage() { std::lock_guard<std::mutex> lock(dataLock_); return alignDepthImage_; } inline cv::Mat getAlignColorDepthImage() { std::lock_guard<std::mutex> lock(dataLock_); return alignColorDepthImage_; } inline cv::Mat getRgbImage() { std::lock_guard<std::mutex> lock(dataLock_); return rgbImage_; }; inline cv::Mat getAlinePointCloud() { std::lock_guard<std::mutex> lock(dataLock_); return alignPointCloudImage_; } //get Depth params inline std::vector<float> getCalibrationParams() { return calibParams_; }; //get IR params inline cv::Mat getLeftCameraMatrix() { return leftCameraMatrix_; }; inline cv::Mat getLeftDistCoeffs() { return leftDistCoeffs_; }; inline cv::Mat getRightCameraMatrix() { return rightCameraMatrix_; }; inline cv::Mat getRightDistCoeffs() { return rightDistCoeffs_; }; inline cv::Mat getStereoRotation() { return stereoRotation_; }; inline cv::Mat getStereoTranslation() { return stereoTranslation_; }; //get rgb params inline cv::Mat getRgbCameraMatrix() { return rgbCameraMatrix_; }; inline cv::Mat getRgbDistCoeffs() { return rgbDistCoeffs_; }; private: Camera* m_pCamera; VideoCapture_HC* m_videoCapture; std::vector<float> calibParams_; bool m_bCloseAll = true; bool arr_bCloseWindows[4] = { false }; bool b_Find_Camera_Dev = true; bool bShowRGB; bool bShowIR; bool bShowDepth; bool bShowDepthAlign; bool dataReady_; // Image cv::Mat leftIRImage_; cv::Mat rightIRImage_; cv::Mat depthImage_; cv::Mat rgbImage_; cv::Mat alignDepthImage_; cv::Mat alignColorDepthImage_; cv::Mat alignPointCloudImage_; // params cv::Mat leftCameraMatrix_; cv::Mat leftDistCoeffs_; cv::Mat rightCameraMatrix_; cv::Mat rightDistCoeffs_; cv::Mat stereoRotation_; cv::Mat stereoTranslation_; cv::Mat rgbCameraMatrix_; cv::Mat rgbDistCoeffs_; //pcl::PointCloud<pcl::PointXYZRGB>::Ptr pointcloud_; //int pcdStartTime_; std::mutex cameraClock_; std::condition_variable cameraCond_; int nCurrent = 0x7f; CString strCameraSN; std::mutex dataLock_; gv::DepthAlignToColor depth2Color_; gv::DepthAlignToColor::Parameters d2cParams_; };
最新发布
07-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值