Database progamming languages like O2, E, and O++ include the ability to iterate trough a set. nested iterators can be used to express joins. this paper descrbes compile-time optimizations similiar to relational transformations like join reordering for such progamming constructs. this paper also shows how to use a standard transformation-based optimizer to optimize these joins. an optimizer built using the EXODUS optimizer generator [GRAE87] was added to the bell labs O++ [AGRA89]compiler. we ised the resulting optimizing compiler to experimentaly validate the ideas in this paper. the expriments show that this technique can significantly improve the performance of database programming languages.