Q: Theorem number section wise when there is no subsection I am using article class.
There is no any chapter. It has some sections only.
In my first section there are five subsections.
I want the Theorem and Definition numbers as per section wise, like Theorem 1.2.4 or Definition 1.5.2, etc.
In further sections there are no subsections.
In the second section since there is no subsection. So here I want like, Theorem 2.1 or Definition 2.4, etc that is without subsection number.
Please suggest the suitable commands.

A: You can define a new theorem Definition with the counter depending on section and subsection and the output of the counter \theDefinition use \thesection as long as subsection counter is less than 1 and \thesubsection otherwise:
\documentclass{article}

\usepackage{ntheorem}
\usepackage{chngcntr}

\newtheorem{Definition}{Definition}[subsection]% Counter `Definition` depends on `subsection` …
\counterwithin*{Definition}{section}% … and also on `section`.

\renewcommand*{\theDefinition}{% Output of `Definition` counter:
  \ifnum\value{subsection}<1 % if `subsection` counter is less than 1 (mostly `\subsection` not yet used in this `\section`)
    \thesection% show \thesection, e.g., 1
  \else% otherwise
    \thesubsection% show \thesubsection, e.g., 1.1
  \fi
  .\arabic{Definition}% followed by "." and Arabic number of Definition
}

\usepackage{mwe}

\begin{document}
\section{Test section}
\begin{Definition}[Definition on section level]
\blindtext
\end{Definition}
\subsection{Test subsection}
\begin{Definition}[Definition in subsection level]
\blindtext
\end{Definition}

\section{Second test section}
\begin{Definition}[Second theorem on section level]
\blindtext
\end{Definition}
\subsection{Second test subsection}
\begin{Definition}[Second theorem in subsection level]
\blindtext
\end{Definition}

\end{document}

Results in:


A: Here is an abstraction of the procedure, very useful if you eventually change your mind about the numbering.
\documentclass{article}
\usepackage{amsthm}
\usepackage{xparse,chngcntr}

\ExplSyntaxOn
\NewDocumentCommand{\newtheorembysubsection}{mom}
 {
  \IfNoValueTF{#2}
   {
    \newtheorem{#1}{#3}[subsection]
    \cs_set:cpn { the#1 } { \thesection. \deepesh_subsection: \arabic{#1} }
    \counterwithin*{#1}{section}
   }
   {
    \newtheorem{#1}[#2]{#3}
   }
 }
\cs_new:Nn \deepesh_subsection:
 {
  \int_compare:nT { \value{subsection}>0 } { \arabic{subsection}. }
 }
\ExplSyntaxOff

\newtheorembysubsection{theorem}{Theorem}

\theoremstyle{definition}
\newtheorembysubsection{definition}[theorem]{Definition} % could be \newtheorem

\begin{document}

\section{Test section}

\begin{theorem}
A theorem, numbered 1.1
\end{theorem}

\begin{definition}
A definition, numbered 1.2
\end{definition}

\subsection{Test subsection}

\begin{definition}
A definition, numbered 1.1.1
\end{definition}

\begin{theorem}
A theorem, numbered 1.1.2
\end{theorem}

\section{Test second section}

\begin{theorem}
A theorem, numbered 2.1
\end{theorem}

\subsection{Test subsection}

\begin{theorem}
A theorem, numbered 2.1.1
\end{theorem}

\end{document}

In case you decide to number theorems by section (as it should be, I find this scheme quite complex for the reader), you just change \newtheorembysubsection into \newtheorem, possibly adding a trailing [section] if needed.

