Is there a way to exit an 'if' statement on condidtion?

I have an if statement that I want the program to exit if a condition is met:
1
2
3
4
5
6
7
8
9
10
 for(j=0;j<10;j++)
        if((syndrome[0]==hmatrix[0][j]) && (syndrome[1]==hmatrix[1][j])&& 
            (syndrome[2]==hmatrix[2][j]) && (syndrome[3]==hmatrix[3][j]))
       	break;
       	if(j==10)
       	printf(" ");// currently put the print " " so program will compile
       	else  
        	{	
                edata[j]=!edata[j];
            }                              

on line 6 above, is there a way to get out of this so I am not printing blanks on the screen?? Thanks!
use exit() to exit your program:

http://www.cplusplus.com/reference/cstdlib/exit/
What do you mean "exit"?

If you mean just abandoning code in if and going further, you can do like so:

1
2
3
4
5
6
7
8
if(j == 10)
{
   // nothing
}
else
{
//code...
}


If you mean that you want program to exit; it depends. You can always do it, but it can be harder depending on how your program looks like.
If it's simple, you can use exit() function;
1
2
if(j == 10)
  exit(-1); // terminate program with -1 return value 


And for larger programs that would require you to design it properly, but it doesn't look like you are writing programs this advanced :)

You can always leave for loop with break, if return statement from main loop is afterwards, and your for loop is in the main():

1
2
3
4
5
6
7
8
for(j = 0; j < 10; ++j)
{
//code...

 if(j == 10)
  break;
//code...
}


Just like you did with syndrome.

Cheers!
you can not do :
1
2
3
4
5
6
7
8
if(j == 10)
{
   // nothing
}
else
{
//code...
}

the program will not compile:

ldu1.cc: In member function 'void ldu1::correct_lcw(int*)':
ldu1.cc:222:9: error: expected primary-expression before 'else'
ldu1.cc:222:9: error: expected ';' before 'else'

that is why i had the blank print statment. but i want to clean this up and i dont want to print a thousands blanks everytime the program runs.

but that is essentially what i want. to do nothing; prehaps i will try to return 0; edit: return 0; wont work in a void function so i need some other do nothing statement.

EDIT: sorry that empty statement does work. I forgot the braces, duh. Thanks!
Last edited on
Topic archived. No new replies allowed.