i am new to c++ language and i have a problem with this loop.
#include<iostream>
using namespace std;
int main(){
int iterador=0;
while (iterador<=10){
cout<<iterador<<endl;
iterador+=1;
cout<<"si";
}
}
It is supposed to print the numbers from 0 to 10 and then print something (I put "yes" to mislead) but when I run it, it prints it as follows:
0
si1
si2
si3
si4
si5
si6
si7
si8
si9
si1
si
and what I wanted was this!
0
1
2
3
4
5
6
7
8
9
1
si
Can someone explain to me why C++ does this, why it does this or how C++ works with loops?
Thank you!
It is what Jesus says. I encourage you to try the following:
for (int i = 0; i <= 10; ++i) cout << i << endl; cout << "Si" << endl;
It's like a while but with fewer lines of code. What it comes to do is while the iterator that has been defined to 0 is less than or equal to the number that was marked, it increases the i and for each increase do what I indicate. In this case, display the iteration number on the screen.
Everything inside your while will be executed every loop, try to get cout<<"if"; of the while