Thread: Quick question on if statments

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    29

    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. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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;
    load = 0;
    for (int x = 0; x < 12; x++)
      { if (capacity[x] > maxcap)
           { maxcap = capacity[x];
               load = x; } }
    
    // same for cars if needed
    
     printf("Your ride.... %d people.... ",capacity[load]);

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    He may not have learned arrays yet, if he hasn't learned else.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    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. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    29
    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. #7
    Registered User
    Join Date
    Jan 2011
    Posts
    29
    Nevermind, I just set all of the > signs to >=.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CommonTater View Post
    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
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very quick math question
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-26-2005, 11:05 PM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM