Problem with a simple program (reading push button presseS)

and it goes into the while loop, but it completely ignores the nested "if" statement and increments the i variable regardless of the state of the push button and will stay in the while loop "x" number of times i have the while(i<=x) set for.

You never read the state of the switch after the while loop starts. Since pb had to be HIGH for the while loop to start, it is HIGH in the while loop, too.