for loop returns immediately in its first iteration | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 1

for loop returns immediately in its first iteration

can someone let me know how to fix my seating function. As long as there is a zero in the array, "airline" I need it to return false, so the while loop continues in the main function but my for loop in the function, seating, returns "true" immediately after its first iteration. here's the code https://code.sololearn.com/cCftD4xKPhyg

10th Apr 2022, 5:19 PM
David
David - avatar
5 Respostas
+ 2
bool seating(bool array[]) { for (int i = 0; i < 10; i++) { if (array[i] == 0) { return false; } } return true; } //if-else: either if or else executed as alternatives..
10th Apr 2022, 5:21 PM
Jayakrishna šŸ‡®šŸ‡³
+ 1
JayakrishnašŸ‡®šŸ‡³ you have been such a big help. Thank you so much! I just realized you answered this in my other question and i didnt pock up the ā€œreturn trueā€ change
10th Apr 2022, 5:24 PM
David
David - avatar
+ 1
Yes. return causes to come out of function. if-else :=> if block executed when if condition is true, otherwise control goes to execute else block if any.. Hope it helps.. You're welcome..
10th Apr 2022, 5:31 PM
Jayakrishna šŸ‡®šŸ‡³
+ 1
David Remember never return in else part inside loop.
10th Apr 2022, 5:49 PM
AĶ¢J
AĶ¢J - avatar
+ 1
Thank you AĶ¢J
10th Apr 2022, 5:51 PM
David
David - avatar