Acetaldehyde and methyl ketones (CH3 .CO.R) react rapidly with halogens (Cl2, Br2 or I2) in the presence of alkali to form haloform. This reaction is usually known as haloform reaction since haloform (CHX3) is the main product. It involves the formation of carbanion.

Diethyl ketone (C2H5.CO.C2H5) has no -COCH3 group, hence it does not undergo haloform reaction. Haloform reaction is used as a diagnostic test for detecting the presence of -COCH3 group in a compound.
Thus the haloform reaction may also be used for distinguishing the methyl ketones from otherketones since the former forms haloform while the latter does not form any haloform, e.g.,

It is important to note that ethyl alcohol (CH3CH2OH) and secondary alcohols having one of the alkyl groups as methyl, although does not contain a carbonyl group, also respond halofrom reaction. It is due to the fact that such alcohols are first oxidised by halogen to acetaldehyde (CH3CHO) or methyl ketone which in turn gives the haloform reaction because of the presence -CO-CH3 grouping.
(a) \(CH_3CH_2OH \xrightarrow [(O)]{X_2}CH_3CHO\)
(b) \(R-CHOHCH_3 \xrightarrow {(O)} R.CO.CH_3\)
Thus in short haloform reaction is given by all compounds containing either of the following grouping.

Remember that hypohalite does not attack carbon-carbon double bond present in the molecule.
For example,
\(C_6H_5-CH=CH.COCH_3 \xrightarrow {NaOI}\)
\(C_6H_5CH=CHCOOH +CHI_3\)