0

HELP

#include <iostream> using namespace std; int main() { int i=1; while(i<10) { cout << i << endl ; if(i==5) { continue ; } ++i; } return 0; } Output - 1 2 3 4 5 5 5 5 ... WHY THIS IS HAPPENING

17th Jun 2018, 12:23 PM
Aveek Bhattacharyya
Aveek Bhattacharyya - avatar
5 Answers
+ 2
when continue operator is execute then increment of counter (i++) is never happened. you need to do like that: ... if(i==5) { i++; continue; } i++; ...
17th Jun 2018, 12:35 PM
Indiegma
Indiegma - avatar
+ 1
At i = 5 it will skip that particular iteration and continue to execute another iteration. Here i is not incremented in if statement therefore i value will be 5 and it will create an infinite loop. If your code is like this if(i==5) { i++; continue; } Than output will be 1 2 3 4 6 7 8 9
17th Jun 2018, 12:42 PM
Meet Mehta
Meet Mehta - avatar
+ 1
Right Indiegma. first i++ than continue.
17th Jun 2018, 1:26 PM
Meet Mehta
Meet Mehta - avatar
0
Thanks everyone
17th Jun 2018, 12:51 PM
Aveek Bhattacharyya
Aveek Bhattacharyya - avatar
- 2
meet mehta. if(i==5){ continue; i++;} your code will not work correctly, because i++ will be skipped always
17th Jun 2018, 12:59 PM
Indiegma
Indiegma - avatar