# Thread: Quick question on if statments

1. ## Quick question on if statments

I have a whole list of if statements in my program, and I need to know how to make it so that if an if statement is true, then it stops all operations after that. How do I do this?

These are my statements, for example I'm inputting values winch make the 6th, 8th and 10th statements true, how do I stop the program at the sixth?

Code:
```    if (capacity1 > capacity2 && capacity1 > capacity3 && capacity1 > capacity4 && capacity1 > capacity5 && capacity1 > capacity6 && capacity1 > capacity7 && capacity1 > capacity8 && capacity1 > capacity9 && capacity1 > capacity10 && capacity1 > capacity11 && capacity1 > capacity12) {
printf("Your ride can have at most %d people on it at one time.\n", capacity1);
printf("This can be achieved with trains of %d cars.\n", cars1);
}

if (capacity2 > capacity1 && capacity2 > capacity3 && capacity2 > capacity4 && capacity2 > capacity5 && capacity2 > capacity6 && capacity2 > capacity7 && capacity2 > capacity8 && capacity2 > capacity9 && capacity2 > capacity10 && capacity2 > capacity11 && capacity2 > capacity12) {
printf("Your ride can have at most %d people on it at one time.\n", capacity2);
printf("This can be achieved with trains of %d cars.\n", cars2);
}

if (capacity3 > capacity2  && capacity3 > capacity1 && capacity3 > capacity4 && capacity3 > capacity5 && capacity3 > capacity6 && capacity3 > capacity7 && capacity3 > capacity8 && capacity3 > capacity9 && capacity3 > capacity10 && capacity3 > capacity11 && capacity3 > capacity12) {
printf("Your ride can have at most %d people on it at one time.\n", capacity3);
printf("This can be achieved with trains of %d cars.\n", cars3);
}

if (capacity4 > capacity3 && capacity4 > capacity2 && capacity4 > capacity1 && capacity4 > capacity5 && capacity4 > capacity6 && capacity4 > capacity7 && capacity4 > capacity8 && capacity4 > capacity9 && capacity4 > capacity10 && capacity4 > capacity11 && capacity4 > capacity12) {
printf("Your ride can have at most %d people on it at one time.\n", capacity4);
printf("This can be achieved with trains of %d cars.\n", cars4);
}

if (capacity5 > capacity4 && capacity5 > capacity3 && capacity5 > capacity2 && capacity5 > capacity1 && capacity5 > capacity6 && capacity5 > capacity7 && capacity5 > capacity8 && capacity5 > capacity9 && capacity5 > capacity10 && capacity5 > capacity11 && capacity5 > capacity12) {
printf("Your ride can have at most %d people on it at one time.\n", capacity5);
printf("This can be achieved with trains of %d cars.\n", cars5);
}

if (capacity6 > capacity5 && capacity6 > capacity4 && capacity6 > capacity3 && capacity6 > capacity2 && capacity6 > capacity1 && capacity6 > capacity7 && capacity6 > capacity8 && capacity6 > capacity9 && capacity6 > capacity10 && capacity6 > capacity11 && capacity6 > capacity12) {
printf("Your ride can have at most %d people on it at one time.\n", capacity6);
printf("This can be achieved with trains of %d cars.\n", cars6);
}

if (capacity7 > capacity6 && capacity7 > capacity5 && capacity7 > capacity4 && capacity7 > capacity3 && capacity7 > capacity2 && capacity7 > capacity1 && capacity7 > capacity8 && capacity7 > capacity9 && capacity7 > capacity10 && capacity7 > capacity11 && capacity7 > capacity12) {
printf("Your ride can have at most %d people on it at one time.\n", capacity7);
printf("This can be achieved with trains of %d cars.\n", cars7);
}

if (capacity8 > capacity7 && capacity8 > capacity6 && capacity8 > capacity5 && capacity8 > capacity4 && capacity8 > capacity3 && capacity8 > capacity2 && capacity8 > capacity1 && capacity8 > capacity9 && capacity8 > capacity10 && capacity8 > capacity11 && capacity8 > capacity12) {
printf("Your ride can have at most %d people on it at one time.\n", capacity8);
printf("This can be achieved with trains of %d cars.\n", cars8);
}

if (capacity9 > capacity8 && capacity9 > capacity7 && capacity9 > capacity6 && capacity9 > capacity5 && capacity9 > capacity4 && capacity9 > capacity3 && capacity9 > capacity2 && capacity9 > capacity1 && capacity9 > capacity10 && capacity9 > capacity11 && capacity9 > capacity12) {
printf("Your ride can have at most %d people on it at one time.\n", capacity9);
printf("This can be achieved with trains of %d cars.\n", cars9);
}

if (capacity10 > capacity9 && capacity10 > capacity8 && capacity10 > capacity7 && capacity10 > capacity6 && capacity10 > capacity5 && capacity10 > capacity4 && capacity10 > capacity3 && capacity10 > capacity2 && capacity10 > capacity1 && capacity10 > capacity11 && capacity10 > capacity12) {
printf("Your ride can have at most %d people on it at one time.\n", capacity10);
printf("This can be achieved with trains of %d cars.\n", cars10);
}

if (capacity11 > capacity10 && capacity11 > capacity9 && capacity11 > capacity8 && capacity11 > capacity7 && capacity11 > capacity6 && capacity11 > capacity5 && capacity11 > capacity4 && capacity11 > capacity3 && capacity11 > capacity2 && capacity11 > capacity1 && capacity11 > capacity12) {
printf("Your ride can have at most %d people on it at one time.\n", capacity11);
printf("This can be achieved with trains of %d cars.\n", cars11);
}

if (capacity12 > capacity11 && capacity12 > capacity10 && capacity12 > capacity9 && capacity12 > capacity8 && capacity12 > capacity7 && capacity12 > capacity6 && capacity12 > capacity5 && capacity12 > capacity4 && capacity12 > capacity3 && capacity12 > capacity2 && capacity12 > capacity1) {
printf("Your ride can have at most %d people on it at one time.\n", capacity12);
printf("This can be achieved with trains of %d cars.\n", cars12);
}```

2. The keyword you may looking for is else.
Code:
```if (this)
{
}
else if (that)
{
}
else if (something)
{
}
else
{
everything that doesn't match anything above
}```
Quzah.

3. I'm thinking you should look into turning your capacity variables into a single array... Scan the array, find the highest capacity index in a variable and then feed that to a switch statement to tell you how many people must be in the cars.

Code:
```int capacity[12];
int cars[12];

maxcap = 0;
for (int x = 0; x < 12; x++)
{ if (capacity[x] > maxcap)
{ maxcap = capacity[x];

// same for cars if needed

4. He may not have learned arrays yet, if he hasn't learned else.

Quzah.

5. Originally Posted by quzah
He may not have learned arrays yet, if he hasn't learned else.

Quzah.
Well when I can crunch 12 very long lines down into 7 short ones... it may be worth his trouble to learn... Loops, arrays, conditionals... the stuff we C all the time...

6. I changed all the if's after the first one into else if statements but its still not working, it just runs my last else statement. As for arrays that's what all my classes next week are about haha.

7. Nevermind, I just set all of the > signs to >=.

8. Originally Posted by CommonTater
Well when I can crunch 12 very long lines down into 7 short ones... it may be worth his trouble to learn... Loops, arrays, conditionals... the stuff we C all the time...
I wasn't debating the optimal solution. I was trying to provide one he could understand now. Obviously he's going to eventually learn arrays.

Quzah