1

I have this function I'm making to count voter preferences in C. It's set so that if it finds a candidate that has not been eliminated and matches the current preference we're checking to then add a vote. I then finish it to set

 preference = candidate_count +1; 

and terminate the while loop, but could I use return here? Could I use return to exit the nested for loop and the while loop to go back to the main for loop?

In a situation where the candidate is eliminated I do

 else preference++; j = 0;

If I did return here would it go back to the nested for loop. I'm just a little confused because it seems you can write code without ever using return if you set some base to terminate the program.

 for(int i =0; i<voter_count;i++)
 {
    //restart the for loop and look for preference 1
    int preference =0;
    while (preference <candidate_count)
    {
    for(int j = 0; j<candidate_count;j++)
    {
         if(preferences[i][j] ==  preference)
        {
            if(candidates[j].eliminated == false)
            {
              candidates[j].votes ++;
              preference = candidate_count +1; 
             }   
            else
             preference++;
             j = 0;
            }
        }
    }
}
Mike S
  • 9
  • 1
  • Is that code inside `main` or some other function? In a regular function `return` can be used to immediately exit the entire function (if in `main` that would exit the function and also the whole process). If you just want to exit the controlling loop use `break` instead. – kaylum Apr 30 '20 at 00:22
  • It's based off this problem set, https://cs50.harvard.edu/x/2020/psets/3/runoff/, currently learning on my own through the class and they kind of gloss over simple stuff like this in favor of higher level lessons. So what you're saying is that return only exits the current function that it's in so if it's inside multiple for loops it would only go back to the nearest one? – Mike S Apr 30 '20 at 00:47
  • "exits the current function that it's in so if it's inside multiple for loops it would only go back to the nearest one". The first part of the sentence is true but not the second. The whole function exits. So it doesn't return to the "nearest for loop". No other code inside the function runs as soon as `return` is encountered. – kaylum Apr 30 '20 at 00:54
  • Also, you would do better to learn this very basic stuff systematically by consulting a C book or tutorial. SO is not the most productive way to learn C fundamentals. This may help: [The Definitive C Book Guide and List](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) – kaylum Apr 30 '20 at 00:54
  • Thanks! I'll be going with head first from the list :). – Mike S Apr 30 '20 at 01:12

1 Answers1

0

return will exit the function and go back to its caller. For example, in the following code,

int foo(int bar){
    int tmp1=0,tmp2=0;
    while (bar--){
        tmp1=bar;
        while (tmp1--){
            tmp2++
            if (tmp2>5){
                 return tmp2;       /* (A) */
            }
        }
   }
   printf("world\n");      /* (X) */
   return tmp2;
}

int main(){
/* whatever */
    foo(10); /* (B) */
    printf("hello\n");  /* (C) */
/* whatever */
}

After the program hits (B), it will jump to function foo. When it hits (A), it will jump to (C), back to its caller. Line (X) will never get executed.

For breaking out nested loop but not out of the entire function, use goto.

user12986714
  • 741
  • 2
  • 8
  • 19