Bird
Raised Fist0
C++programming~20 mins

Return inside loops in C++ - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Return Loop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
What is the output of this C++ code with return inside a loop?

Look at the code below. What will it print when run?

C++
#include <iostream>

int findFirstEven(int arr[], int size) {
    for (int i = 0; i < size; i++) {
        if (arr[i] % 2 == 0) {
            return arr[i];
        }
    }
    return -1;
}

int main() {
    int numbers[] = {3, 7, 5, 8, 10};
    std::cout << findFirstEven(numbers, 5);
    return 0;
}
A8
B3
C-1
D10
Attempts:
2 left
๐Ÿ’ก Hint

Return inside the loop stops the function immediately when condition is met.

โ“ Predict Output
intermediate
2:00remaining
What does this function return when no return is inside the loop?

What will this C++ function return when called with the array below?

C++
#include <iostream>

int findNegative(int arr[], int size) {
    for (int i = 0; i < size; i++) {
        if (arr[i] < 0) {
            break;
        }
    }
    return 0;
}

int main() {
    int data[] = {1, 2, 3, 4, 5};
    std::cout << findNegative(data, 5);
    return 0;
}
AUndefined behavior
B1
CCompilation error
D0
Attempts:
2 left
๐Ÿ’ก Hint

Notice the function returns 0 after the loop finishes.

๐Ÿ”ง Debug
advanced
2:00remaining
Why does this function always return 0?

Examine the code below. The function is supposed to return the first positive number, but it always returns 0. Why?

C++
#include <iostream>

int firstPositive(int arr[], int size) {
    for (int i = 0; i < size; i++) {
        if (arr[i] > 0) {
            return 0;
        }
    }
    return -1;
}

int main() {
    int nums[] = {-3, -1, 4, 5};
    std::cout << firstPositive(nums, 4);
    return 0;
}
AThe function returns 0 inside the loop instead of the positive number.
BThe loop never runs because size is zero.
CThe function has a syntax error and won't compile.
DThe function returns -1 before the loop starts.
Attempts:
2 left
๐Ÿ’ก Hint

Look carefully at what is returned inside the loop.

๐Ÿ“ Syntax
advanced
2:00remaining
Which option causes a compilation error due to return inside loop?

Which of the following code snippets will cause a compilation error?

Aint foo() { for(int i=0; i<3; i++) return i; }
Bint foo() { for(int i=0; i<3; i++) { if(i==2) return; } return 0; }
Cint foo() { for(int i=0; i<3; i++) { if(i==2) return i; } return 0; }
Dint foo() { for(int i=0; i<3; i++) { if(i==2) return i; } }
Attempts:
2 left
๐Ÿ’ก Hint

Check if the return statements match the function's return type.

๐Ÿš€ Application
expert
2:00remaining
What is the output of this nested loop with return inside inner loop?

Consider the following C++ code. What will it print?

C++
#include <iostream>

int nestedLoop() {
    for (int i = 1; i <= 3; i++) {
        for (int j = 1; j <= 3; j++) {
            if (i * j > 3) {
                return i * j;
            }
        }
    }
    return 0;
}

int main() {
    std::cout << nestedLoop();
    return 0;
}
A0
B6
C4
D9
Attempts:
2 left
๐Ÿ’ก Hint

The function returns as soon as the product i*j is greater than 3.

Practice

(1/5)
1.

What happens when a return statement is executed inside a loop in a C++ function?

easy
A. The loop runs twice before the function stops.
B. Only the current loop iteration stops, and the loop continues.
C. The function immediately stops and returns the value.
D. The loop skips to the next iteration without stopping the function.

Solution

  1. Step 1: Understand the role of return in functions

    The return statement immediately ends the function and sends back a value.
  2. Step 2: Analyze return inside a loop

    When return is inside a loop, it stops the entire function, not just the loop.
  3. Final Answer:

    The function immediately stops and returns the value. -> Option C
  4. Quick Check:

    Return inside loop = function stops [OK]
Hint: Return inside loop ends function immediately [OK]
Common Mistakes:
  • Thinking return only stops the loop
  • Confusing return with continue or break
  • Assuming code after return runs
2.

Which of the following is the correct syntax to return the first even number found in a vector nums using a loop?

for (int i = 0; i < nums.size(); i++) {
    if (nums[i] % 2 == 0) {
        // return here
    }
}
easy
A. return nums[i];
B. return i;
C. return nums;
D. return i + 1;

Solution

  1. Step 1: Identify what to return

    The question asks to return the first even number, so we return the value at index i.
  2. Step 2: Match the correct return statement

    return nums[i]; returns the actual even number found.
  3. Final Answer:

    return nums[i]; -> Option A
  4. Quick Check:

    Return value, not index [OK]
Hint: Return the value, not the index, when asked for element [OK]
Common Mistakes:
  • Returning the index instead of the value
  • Returning the whole vector
  • Returning incorrect arithmetic on index
3.

What is the output of the following C++ function when called with findFirstPositive({-3, -1, 0, 4, 5})?

int findFirstPositive(std::vector<int> nums) {
    for (int i = 0; i < nums.size(); i++) {
        if (nums[i] > 0) {
            return nums[i];
        }
    }
    return -1;
}
medium
A. -1
B. 4
C. 0
D. 5

Solution

  1. Step 1: Trace the loop to find first positive

    The loop checks each element: -3 (no), -1 (no), 0 (no), 4 (yes, positive).
  2. Step 2: Return the first positive number found

    At 4, the function returns immediately with 4.
  3. Final Answer:

    4 -> Option B
  4. Quick Check:

    First positive = 4 [OK]
Hint: Return stops at first positive number found [OK]
Common Mistakes:
  • Returning -1 instead of first positive
  • Confusing 0 as positive
  • Returning last positive instead of first
4.

Identify the error in this function that tries to return the first negative number from a vector nums:

int findNegative(std::vector<int> nums) {
    for (int i = 0; i < nums.size(); i++) {
        if (nums[i] < 0) {
            return nums[i];
        } else {
            return 0;
        }
    }
    return -1;
}
medium
A. The loop condition is incorrect.
B. The function never returns any value.
C. The return type should be void.
D. The function returns 0 too early inside the loop.

Solution

  1. Step 1: Analyze the else return inside the loop

    The else block returns 0 immediately if the first element is not negative, stopping the function early.
  2. Step 2: Understand why this is wrong

    This prevents checking all elements; function should only return 0 after full loop if no negative found.
  3. Final Answer:

    The function returns 0 too early inside the loop. -> Option D
  4. Quick Check:

    Return in else inside loop causes early stop [OK]
Hint: Avoid return in else inside loop; return after loop instead [OK]
Common Mistakes:
  • Returning inside else causes premature exit
  • Thinking loop runs fully before return
  • Misunderstanding return type requirements
5.

Write a function that returns the index of the first zero in a vector nums. If no zero is found, return -1. Which of the following implementations is correct?

Option A:
int findZero(std::vector<int> nums) {
    for (int i = 0; i < nums.size(); i++) {
        if (nums[i] == 0) {
            return i;
        }
    }
    return -1;
}

Option B:
int findZero(std::vector<int> nums) {
    int index = -1;
    for (int i = 0; i < nums.size(); i++) {
        if (nums[i] == 0) {
            index = i;
        }
    }
    return index;
}

Option C:
int findZero(std::vector<int> nums) {
    for (int i = 0; i < nums.size(); i++) {
        if (nums[i] == 0) {
            break;
        }
    }
    return i;
}

Option D:
int findZero(std::vector<int> nums) {
    for (int i = 0; i < nums.size(); i++) {
        if (nums[i] == 0) {
            return nums[i];
        }
    }
    return -1;
}
hard
A. Option A
B. Option B
C. Option C
D. Option D

Solution

  1. Step 1: Verify the correct logic for finding first zero index

    The implementation that returns i immediately when nums[i] == 0 and -1 after the loop is correct.
  2. Step 2: Why others fail

    The one setting a variable continues the loop, returning the last zero's index. The break version doesn't return -1 if no zero found and i goes out of scope. The last returns the value 0 instead of index.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Return index immediately on first zero [OK]
Hint: Return index immediately when found, else after loop return -1 [OK]
Common Mistakes:
  • Returning value instead of index
  • Returning last zero index instead of first
  • Using break without returning causes scope errors
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard