Simple C question

Associate
Joined
18 Jul 2004
Posts
1,866
Im currently writing a c program for some coursework, but ive encountered a bit of an issue. Ive tried to explain it below:


if tmode = 1 and cp<cpmin
do code a
if tmode = 2 and coef<coefmin
do code a
else
do code b

So basically, id like two if statements, that both refer to the same piece of code (code a), but without having to repeat code a twice?
 
Create a function which carries out the task of 'code a'.

Alternative is to do:

Code:
if((tmode == 1 && cp < cpmin) || (tmode == 2 && coef < coefmin))
{
   // code A
}
else
{
  // code b
}
 
Im currently writing a c program for some coursework, but ive encountered a bit of an issue. Ive tried to explain it below:


if tmode = 1 and cp<cpmin
do code a
if tmode = 2 and coef<coefmin
do code a
else
do code b

So basically, id like two if statements, that both refer to the same piece of code (code a), but without having to repeat code a twice?

doesn't look like c:

Code:
void do_a(){
	// a code here...

}

void do_b() {
	// b code here...
}

int main(){

	int tmode = 1;
	int cp = 0;
	int cpmin = 0;
	int coef = 0;
	int coefmin = 0;

	if ( (tmode == 1 && cp < cpmin) || (tmode == 2 && coef < coefmin)  ) {
		do_a();
	}
	else
		do_b();
	

	return 0;
}

haven't compiled it but i can't see why it wouldn't run...
 
Last edited:
Back
Top Bottom