Early-life experience influences subsequent maturation and function of the adult brain, sometimes even in a sex-specific manner, but underlying molecular mechanisms are poorly understood. We describe here how juvenile experience defines sexually dimorphic synaptic connectivity in the adult Caenorhabditis elegans nervous system. Starvation of juvenile males disrupts serotonin-dependent activation of the CREB transcription factor in a nociceptive sensory neuron, PHB. CREB acts through a cascade of transcription factors to control expression of an atypical cadherin protein, FMI-1/Flamingo/CELSR. During postembryonic development, FMI-1 promotes and maintains synaptic connectivity of PHB to a command interneuron, AVA, in both sexes, but a serotonin-dependent transcriptional regulatory cassette antagonizes FMI-1 expression in males, thereby establishing sexually dimorphic connectivity between PHB and AVA. A critical regulatory node is the CREB-target LIN-29, a Zn finger transcription factor that integrates four layers of information: sexual specificity, past experience, time and cell-type specificity. Our findings provide the mechanistic details of how an early juvenile experience defines sexually dimorphic synaptic connectivity.