vendredi 28 août 2020

The value of iterator not updating inside an if statement in a for loop.. (Check the code for reference)

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int t;
    scanf("%d", &t);
    while (t--)
    {
        int n, ans = 0;
        scanf("%d", &n);
        int s[n];
        bool flag = false;
        for (int i = 0; i < n; i++)
        {
            scanf("%d", &s[i]);
        }
        for (int i = 0; i < n; i++)
        {
            s[i] -= 1;
            if (s[i] < 0)
                break;
            ans++;
            if (i == (n - 1))
            {
                i=0;
                flag = true;
            }
        }
        printf("\n%d\n", ans);
        printf("%d", flag);
    }

    return 0;
}

I want to know why the value of i is not updating to i=0 when i == n-1, as I have written in the code. The bool flag is just there to check if the if statement was rendering at all or not (It is). Kindly help me out with this.

Aucun commentaire:

Enregistrer un commentaire