for(int count=0; count<100 && rand()%100 != 1; cout << count << endl, ++count) ;
[grin]
(does the same as my previous code)
for(int count=0; count<100 && rand()%100 != 1; cout << count << endl, ++count) ;
while(more lines to proccess && (result == 0)){ result = do_op_1(); if(result == 0) { result = do_op_2(); } if(result == 0) { result = do_op_3(); } if(result == 0) { result = do_op_4(); }}
int search_array (int *array, int num_elements, int key);
int search_array (int *array, int num_elements, int key){ int index; for (index = 0 ; index < num_elements ; ++index) { if (array [index] == key) break; } return index < num_elements ? index : -1;}
int search_array (int *array, int num_elements, int key){ int found = 0, index; for (index = 0 ; !found && index < num_elements ; ++index) { if (array [index] == key) found = 1; } // the above loop does an extra '++index' when the item has been found! return found ? index - 1 : -1;}
int search_array (int *array, int num_elements, int key){ int index; for (index = 0 ; array [index] != key && index < num_elements ; ++index) { } return index < num_elements ? index : -1;}
Quote:Original post by DrNecessiterReturns not at the end of a function are considered 'incorrect' more often than break itself is, so actually your solution isn't really one =-)
Many times, if I find myself putting a break in a loop that's a signal to me that the loop should probably be in a separate function, and the break should really be a return.[...]