Printing values of an array using "while"

clara_gscr

New member
Joined
Jun 10, 2022
Messages
1
Programming Experience
Beginner
Hello.
I am a beginner in programming and I was doing some exercises my teachers assigned and run into some problems.
The exercise says that I have to print in the unity console the values of an array if when they're divided by 2 the remainder is 0.
But I have to use "While" and I only know how to do it with "for". I've run out of ideas.
Thank you in advance for any replies.
This is the code i tried:
C#:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestCondicionales2 : MonoBehaviour
{
   
    public int[] numbers = new int[25];
    int i;

    void Start()
    {
        while(i < numbers.Length)
        {
            if(numbers[i] / 2 = 0)
            {
                Debug.Log(numbers);
            }
        }
 
A for loop that looks like:
C#:
for (init; condition; increment)
{
    body;
}

Is just syntactic sugar for the following:
C#:
init;
while (condition)
{
    body;
    increment;
}

Although the two have the same effect, the the use of one instead of the other is to try to show the intent of the programmer.
 
Last edited:
if when they're divided by 2 the remainder is 0
Look closer at your code in post #1. Did you mean to use the modulus operator to get the the remainder? You are currently using the integer division operator which will give you back an integer quotient.
 
Back
Top Bottom