Problem with While

1

I'm new to the C ++ language and I have a problem with this cycle.

#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 confuse) but when you execute it, it prints it to me in the following way:

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, because it does so or how does C ++ work with cycles?

Thank you!

    
asked by DDR 29.05.2018 в 02:27
source

2 answers

1

Everything that goes in your while is going to be executed in each cycle, try to get cout < < "yes"; of the while

#include<iostream>
using namespace std;

int main(){
int iterador=0;
 while (iterador<=10){
    cout<<iterador<<endl;
    iterador+=1;

  }
    cout<<"si";

}
    
answered by 29.05.2018 / 02:31
source
2

It's what Jesus says. I encourage you to try to do the following:

for (int i = 0; i <= 10; ++i) cout << i << endl; cout << "Si" << endl;

It's like a while but with less code lines. What comes to do is while the iterator that has been defined at 0 is less than or equal to the number that frame, increases the i and by each increase do what I indicate. In this case, take out the iteration number on the screen.

    
answered by 08.12.2018 в 20:08