Dear all,

I have a quick question about if-else-if control flow (you'd think by now these questions would be unnecessary, but here I am):

If I was told to rewrite a function that looked like this (this is an exam question so I am not asking the exact question in order to be within the honor code)

#define STUFF

int My_Function(int, int); 

void int(main){
   ... code here
}

int My_Function(int a, int b){
  int c = 0; 
  if( a < b) c = b; 
  else if (b >= a) c = b; 
  else c = STUFF; 
  return c; 
}

Issues with this code:

1. No need for the definition
2. No need for two arguments.
3. The else condition is never reached.
4. I could just replace with a one-line function return (input);

So, am I missing anything? Thanks for the help in advance!

Recommended Answers

All 9 Replies

3. The else condition is never reached.

the reason the else statement is never reached is because all the possible outcomes can already be found in the if and if else statement

the reason the else statement is never reached is because all the possible outcomes can already be found in the if and if else statement

Scratch that I mean if and else if statements

Member Avatar for v3ga

Scratch that I mean if and else if statements

The condition b<a is never reached in the if& else-if

commented: right..sorry didn't notice +4
Member Avatar for v3ga

Dear all,

I have a quick question about if-else-if control flow (you'd think by now these questions would be unnecessary, but here I am):

If I was told to rewrite a function that looked like this (this is an exam question so I am not asking the exact question in order to be within the honor code)

#define STUFF

int My_Function(int, int); 

void int(main){
   ... code here
}

int My_Function(int a, int b){
  int c = 0; 
  if( a < b) c = b; 
  else if (b >= a) c = b; 
  else c = STUFF; 
  return c; 
}

Issues with this code:

1. No need for the definition
2. No need for two arguments.
3. The else condition is never reached.
4. I could just replace with a one-line function return (input);

So, am I missing anything? Thanks for the help in advance!

There seems to be something wrong with the conditions. Do you mean if(b<=a) c=a in the second line... or there's no need of the 1st line at all

We won't reach an understanding until you at least tell us what it is supposed to do.

Sorry, allow me to clarify:

This code is meant to be stupid, and our task is to determine how stupid. We are supposed to determine what these functions do and rewrite them using better coding convention, removing unnecessary statements, etc.

So, this is the first of a series of questions and it seems asinine. The others were clever (a disguised and very convoluted memcpy, data structures, etc), but this one just seems too simple.

It seems like it is simply the assignment of c=a.

You're right; I did make a BIG mistake. The code should be:
else if (b <= a) c = b;

Oh, I know why the conditions are not being met, I just wanted to post this here in case I was missing something. Basically I want to ask if the function:

#define STUFF

int My_Function(int, int); 

void int(main){
   ... code here
}

int My_Function(int a, int b){
  int c = 0; 
  if( a < b) c = b; 
  else if (b <= a) c = b; 
  else c = STUFF; 
  return c; 
}

\

Can be rewritten as this function as maintain all of the logical flow:

int My_Function_Rewritten(int a, int b)
    return(b);

Looks like it.
Do you have to do anything with line 6?

No, that is what is so weird about this. It seems like it is just that simple.

It is causing me to second guess myself. You know?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.