break if statements without exit loop

hello guys, is there a way to avoid chechking if statements when it is complited and dont exit with the loop. for example;
for(int i=0;i<10;i++)
{
if(i==1) {stop checking this if but dont exit in the loop and check another if};
else if(i==5) {same and so on};
}
is there any ideas??
I don't understand your question.
Give a real example.
And always put code in code tags: http://www.cplusplus.com/forum/articles/16853/
Use
continue
to jump ahead to the next cycle through the loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include<string>

int main() {
	
	for (int i = 0; i < 10; i++) {
		if (i == 1) {
			continue;
		}
		else {
			std::cout << i;
		}
	}
	
	return 0;
}
Last edited on
Manga, if statement in yout code will be checking ten times and i need when it is completed
(i==1) if statement die and dont check another i
closed account (E0p9LyTq)
break in place of continue.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

int main()
{

   for (int i = 0; i < 10; i++)
   {
      if (i == 1)
      {
         break;
      }
      else
      {
         std::cout << i;
      }
   }
}
Last edited on
biwkina wrote:
Manga, if statement in yout code will be checking ten times and i need when it is completed
(i==1) if statement die and dont check another i


Why are people even attempting to respond to this drivel? You're just guessing what the ... wants.

@biwkina, You're just babbling. Either learn English or get someone who speaks your native tongue and proper English to ask your question for you.
don't loop. its only 10, and you are making a mess of it with the conditions.
just do this:

statement for i = 0;
statement for i = 1;
statement for i = 2;
statement for i = 3;
statement for i = 4;
statement for i = 5;
statement for i = 6;
statement for i = 7;
statement for i = 8;
statement for i = 9;

there are other ways if you had a lot more than 10, but unrolling here looks best to me -- you don't have to loop, you don't need to worry about i anymore, just do the statements as needed in order, you don't need the conditions, etc. It cleans up nicely.


Last edited on
Hi, biwkina.
Please, try to add further details. In a for-loop, the variable you loop on takes a different value at every iteration; therefore, in your example, only one ‘if’ will result in being true at every iteration:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

int main()
{
    for(int i = 0; i < 10; ++i) {
        if(i == 1) { std::cout << "This instruction will execute only once\n"; }
        if(i == 5) { std::cout << "Guess what? I'll execute only once\n"; }
        if(i == 6) { std::cout << "Surprise! This sentence won't be repeated\n"; }
    }
    return 0;
}


Output:
This instruction will execute only once
Guess what? I'll execute only once
Surprise! This sentence won't be repeated

What are you trying to achieve, instead?
Topic archived. No new replies allowed.