An optical encoder comprising: a scale that has a first periodic pattern and a second periodic pattern that has a period (P2) longer than a period (P1) of the first periodic pattern; and a detector that is movable relative to the scale, wherein the detector is switchable between a first detecting state in which the detector is configured to read the first periodic pattern by changing a combination in a plurality of read elements and outputs a first signal having a changing period that depends on the first periodic pattern, and a second detecting state in which the detector is configured to read the second periodic pattern and outputs a second signal having a changing period that depends on the second periodic pattern, and wherein the optical encoder further comprises a processor configured to execute a first process for calculating a relative displacement amount (x_i) between the scale and the detector by using one of the first signal and the second signal obtained from the detector that is set to one of the first detecting state and the second detecting state, and a second process for calculating a relative position (θ1) of one of the scale and the detector relative to the other of the scale and the detector by using both of the first signal and the second signal obtained from the detector that is set to each of the first detecting state and the second detecting state, characterized in that the processor is configured to execute the second process while the processor executes the first process, the processor is configured to calculate a first relative position (θ2) of the one to the other when a calculation of the relative position starts, and then to calculate a second relative position (θ3) of the one to the other based on the relative displacement amount and the first relative position. a scale that has a first periodic pattern and a second periodic pattern that has a period (P2) longer than a period (P1) of the first periodic pattern; and a detector that is movable relative to the scale, wherein the detector is switchable between a first detecting state in which the detector is configured to read the first periodic pattern by changing a combination in a plurality of read elements and outputs a first signal having a changing period that depends on the first periodic pattern, and a second detecting state in which the detector is configured to read the second periodic pattern and outputs a second signal having a changing period that depends on the second periodic pattern, and wherein the optical encoder further comprises a processor configured to execute a first process for calculating a relative displacement amount (x_i) between the scale and the detector by using one of the first signal and the second signal obtained from the detector that is set to one of the first detecting state and the second detecting state, and a second process for calculating a relative position (θ1) of one of the scale and the detector relative to the other of the scale and the detector by using both of the first signal and the second signal obtained from the detector that is set to each of the first detecting state and the second detecting state, characterized in that the processor is configured to execute the second process while the processor executes the first process, the processor is configured to calculate a first relative position (θ2) of the one to the other when a calculation of the relative position starts, and then to calculate a second relative position (θ3) of the one to the other based on the relative displacement amount and the first relative position. <EOS>