# Thread: A very basic question

1. ## A very basic question

I just started learning C a few days ago, and I bought a book called "C How to Program." As it turns out it was intended to be a college textbook, but I've found it very helpful so far. Right now I'm working on an exercise in which you enter three numbers and the computer is supposed to determine, among other things, which is smallest and which is largest using a single statement if command.

My first attempt went something like this:

if ( x < y < z)
printf( "smallest is %d", x );
if ( x < z < y )
printf( "smallest is %d", x );

and so on for each variable. Obviously I used the same structure for finding the largest number. Clumsy, I admit, but I didn't have much to work with in the way of knowledge. As it turned out, this returned nothing. Absolutely nothing. My first question is why this happened. I guess maybe the if function doesn't handle multiple operators in that format, but it sure as heck didn't tell me that in the book.

Next I tried another structure, one that I thought of earlier but didn't think would work:

if ( x < y, x < z )
printf(...);

This sort of worked, but it always returned either two smallest and one largest numbers or two largest and one smallest numbers. I checked it out and found that whenever the second condition (the one after the comma) was true, it would satisfy the function and go on to the next line. In other words, the first condition doesn't matter at all.

Now this may seem very elementary to you all, but it has stopped me dead in my tracks. Any help here would be greatly appreciated, but if you could Socratically lead me in the right direction rather than directly telling me the answer (if that's even possible for such a simple dilemma as this) I would be even more thankful.

Ash 2. if (x < y < z) is
evaluated as
if ((x < y) < z) or if (x < (y < z)).
What you want to do is use the && operator. 3. Nick,

After I made my post I found the && solution on a C tutorial website, quite by accident. I've tried it, and as you know it works, but the thing that irks me is that I was given no knowledge of that from the book (which is a start from scratch textbook) before the exercise. Nevertheless, I thank you for your help. From the looks of things, I may be bothering you guys a lot with little stuff like this, cause I've already found another problem, which is either a typo in the book or another failure on the part of the author. Does anyone know of a C Board that is specifically geared for beginners? Thanks again.

Ash 4. Yes u r right it is a good book, I have a copy of the 2nd Edition. There is some good stuff in there. 5. I think that this is a beginning C board. I mean, once you get the syntax down, half of the nonsense here shouldn't be too tough to understand. 6. Hi
sample code, alter to fit

#include <stdio.h>

int x, y;

int main(void) {
/* Input the two values to be tested. */

printf("\nInput an integer value for x: ");
scanf("%d", &x);
printf("\nInput an integer value for y: ");
scanf("%d", &y);

/* Test values and print result. */

if (x == y)
printf("x is equal to y\n");
else
if (x > y)
printf("x is greater than y\n");
else
printf("x is smaller than y\n");
system("pause");
return 0;
}

sall 7. I thank everyone for their kind responses and advice. However, I need to be a little more specific about the criterion for this particular exercise. The only things I am supposed to use are single statement if commands, the equational and relational operators (or whatever they're called: ==, !=, =<, =>, <, >, +, -, /, *, %) and integer variables. I can't use else, &&, or much else for that matter, simply cause I haven't learned it yet. If anyone has a simple solution for this, I would appreciate it, but if not it's no big deal. Thanks again.

Ash 8. Ah, coding without else statements. So you're trying to cut down on jumps, eh? Super leet. ;p

This is a pretty familiar problem for some reason. I don't think you want the code put in front of your face, so I'll just give you a few lines.
Code:
```//Blah blah blah
int min;
// ...
min = x;
// ...
printf ("The smallest value is %d", min);
// ...```
That's the organized solution, and likely what they want. Although there's a way to kind of simulate having && with ifs.
Code:
```// if (A && B) doX();== if (A) if (B) doX();
if (x < y)
{
if (x < z)
{
printf ("%d is the tiny one.", x);
}
} // The brackets are there for aesthetic purposes only.```
But doing it that way is evil... eeeeevil. 9. hmm, if you could use arrays you could use nested if statements to find the minimum of a "range" of numbers, but not sure if you've learned that yet. I'm doing C programming in class and we had to do median/mode and find the min/max stuff... Popular pages Recent additions 