He had to get the Federal armies out of the Shenandoah Valley during growing season, else he’d be unable to feed his own army. Moving into Pennsylvania and swinging toward Washington would pretty much ensure the Federal armies would follow. He also hoped his move would pull Federal troops out of Mississippi, as Vicksburg was vital to the Southern war effort. The Pennsylvania campaign was both offensive and defensive. No question but that he’d have taken Washington if he had the opportunity...that’s war.
Yes but Lees move was ill-fated to say the least. He not only lost at Gettysburg but the very next day, July 4, 1863 Vicksburg fell after Grants month-long siege of it. This geographically split the Confederacy in two and gave the North control of the vital Mississippi River and forced Davis to move to the capitol to Richmond. And for some eighty odd years afterward the city of Vicksburg didn’t celebrate The Fourth Of July.