In this paper we consider deterministic and stochastic versions of discrete time analogs of optimization problems of the Bolza type. The functionals are assumed to be convex, but we make no differentiability assumptions and allow for the explicit or implicit presence of constraints both on the state x_t and the increments delta x_t. The deterministic theory serves to set the stage for the stochastic problem. We obtain optimality conditions that are always sufficient and which are also necessary if the given problem satisfies a strict feasibility condition and, in the stochastic case, a bounded recourse condition. This is a new condition that bypasses the uniform boundedness restrictions encountered in earlier work on related problems.