A hallmark of intelligent behavior is the ability to flexibly respond to external sensory inputs based on dynamically changing rules. A central question is how neurons in the brain implement computations underlying intelligent behaviors. The neocortical pyramidal neurons use their elaborated dendritic arbors to segregate a plethora of inputs and dynamically integrate them--a process known as dendritic computation--which may play important roles in rule-dependent sensory processing. However, evidence directly linking dendritic computation with intelligent cognitive behaviors has been absent. Here we combine two-photon imaging and a rule-switching flexible categorization task in mice to show that a projectome-defined extratelencephalic (ET) cortical layer 5 (L5) neurons in the auditory cortex integrate dendritic rule information and somatic sensory input to enable rule-dependent flexible categorization. The apical dendrite and soma within the same ET neurons exhibit distinct compartmental representations for sensory and rule information, with the soma predominantly encoding sensory information and the dendrites representing inferred task rule. Simultaneous optogenetic dendritic inhibition and two-photon imaging revealed that dendritic rule coding is essential for somatic output of flexible categorization. Our findings indicate that nonlinear dendritic integration of rule and sensory information constitutes a neuronal computational mechanism underlying rule-switching flexible decision-making.