How to go up or down to assign a child?

I want to find a child apart of a transform and then go one above or below it. I hope that explains it, its kind of difficult to explain but here’s the code:

            if (Input.GetKeyDown(KeyCode.W))
            {
                foreach (Transform child in hook)
                {
                    if (child.name == currentSegment.gameObject.name)
                    {
                        //what I want is getting the child I found and then going to the one above it
                        currentSegment = child++;
                    }
                }
            }
            else if (Input.GetKeyDown(KeyCode.S))
            {
                foreach (Transform child in hook)
                {
                    if (child.name == currentSegment.gameObject.name)
                    {
                        currentSegment = child--;
                    }
                }
            }

To go above, you can use transform.parent for any transform. To check for the children one level below, I’d do something like this.

Assuming all children have a Transform component attached to them - which they should by default - you could do a foreach cycle and then check if the current transform.parent is the parent you are looking for, like so:

Transform parent = transform;
 
foreach (Transform child in transform.GetComponentInChildren<Transform>()) 
{
    if (child.parent == parent) {
        // It is the parent's child
    }
}

If you want to go more deeper then you have to do it recursively, but you should be careful with that.