In this paper, we propose a distributed algorithm that allows unmanned aerial vehicles (UAVs) to dynamically learn their optimal 3D locations and associate with ground users while maximizing the network’s sum-rate. Our approach is referred to as ’Learn-As-You-Fly’ (LAYF) algorithm. LAYF is based on a decomposition process that iteratively breaks the underlying optimization into three subproblems. First, given fixed 3D positions of UAVs, LAYF proposes a distributed matching-based association that alleviates the bottlenecks of bandwidth allocation and guarantees the required quality of service. Next, to address the 2D positions of UAVs, a modified version of K-means algorithm, with a distributed implementation, is adopted. Finally, in order to optimize the UAVs altitudes, we study a naturally defined game-theoretic version of the problem and show that under fixed UAVs 2D coordinates, a predefined association scheme, and limited interference, the UAVs altitudes game is a potential game where UAVs can maximize the limited interference sum-rate by only optimizing a local utility function. Our simulation results show that the network’s sum-rate is improved as compared to both a centralized suboptimal solution and a distributed approach that is based on closest UAVs association.