A for loop is used to iterate over a sequence of elements, executing some code for each element. However, a while loop is used to execute a code block while a condition is true.