Skip to content
Advertisement

Change only one of the many iteration

I have 0-10 for loop I want to change the value to 100 when is only 4 and the rest will not be affected and no break how should I achieve that. And as for the petitioner, do not repel [him].

class Main
{
    public static void main(String[] args)
    {

        System.out.println("Hello world!");

        int n = 0;

        for (int i = 0; i < 10; i++)
        {
            // for example
      
            /*
                0           0
                1           1
                2           2
                3           3
                4 = 100     100
                5           5
                6           6
                7           7
                8           8
                9           9
                10          10
            */
    
            if (i == 4)
            {
                i = 100;
            }
            else
            {
                System.out.println(i);
            }
        }
    }
}

Advertisement

Answer

Another Solution!!

public class Main
{
    public static void main(String[] args)
    {

        System.out.println("Hello world!");


        for (int i = 0; i <=10; i++)
        {
            // for example
      
            /*
                0           0
                1           1
                2           2
                3           3
                4 = 100     100
                5           5
                6           6
                7           7
                8           8
                9           9
                10          10
            */
    
            if (i == 4)
            {
                i = 100;
                System.out.println(i);
                i=4;
                continue;
            }
            else
            {
                System.out.println(i);
            }
        }
    }
}
Advertisement