首页 > 解决方案 > 包含两个整数的代码如何逐步操作 C++?

问题描述

我对 C++ 编程很陌生,之前我已经回顾了 python 中的 while 循环,但是这两个整数在这里让我感到困惑。如果您能向我解释这个 while 循环是如何逐步运行的,我会非常高兴。

#include<iostream>
using namespace std;
int main() {
    int i;
    int j; 
    while(i<10 || j < 5) { // i kucuk ondan kucuk oldu surece {} icerisindeki islemler surekli olarak while() loopu ile tekrarlancak 
        cout<<"i: "<<i<<" "<<"j: "<<j<<endl;
        i = i + 1;  // eger bu kod olmaz ise, sonsuza dek i degerine basacak
        j = j + 1;
    }
    return 0;
}

标签: c++while-loop

解决方案


#include<iostream>
using namespace std;
int main() {
/* You should first give a starting value
 to the I and j, otherwise they 
 will get a random number and your while won't work*/ 
    int i=0; 
    int j=0;
/* so as the word says "while" - while (something here is true)do the 
following code between the starting 
brackets and the finishing brackets. When it's not True skip the loop and go to the next line, in this example we will go to return 0 */ 

/*The value of i is 0 and the value of j is 0, and we first check if 0(i)<10 that's true next we check the other one 
if 0(j) < 5 yes do the following block*/
/* the || mean "or' if either one of them 
is true do the following block of code between the brackets*/

    while(i<10 || j < 5) {
        //we print
        cout<<"i: "<<i<<" "<<"j: "<<j<<endl;
        //we are incrementing the values of i and j for 1;
        i = i + 1;  
        j = j + 1;

        /*so what happens now it jumps again to the while
          and checks if the statement is true, now i = 1 and j = 1; 
          and this runs until i is 10 because only then the i won't be lesser then 
          10 and j won't be lesser then 5 it will be false*/ 
       */

    }
    //close the program
    return 0;
}

希望我很清楚!


推荐阅读