I want an example to have the same number as the current definition. So if the last definition is definition 2, the example should be example 2. (Is this a good idea?). Currently with the following code it is example 2.1 for definition 2. (I don't think that I am going to have multiple examples for one definition).
\documentclass[]{scrbook}
\usepackage[T1]{fontenc}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{lipsum}
\usepackage{amsthm}
\usepackage[english]{cleveref}
% Config
\theoremstyle{definition}
\newtheorem{definition}{Definition}
\theoremstyle{definition}
\newtheorem{example}{Example}[definition]
\theoremstyle{plain}
\newtheorem{theorem}{Theorem}[section]
\begin{document}
\begin{definition}[Foo]\label{def:foo}
\lipsum[2]
\end{definition}
\begin{example}[Example for Foo]\label{ex:foo}
\lipsum[2]
\end{example}
% But: not every example needs a definition.
\begin{definition}[Bar]
\lipsum[2]
\end{definition}
% The numbering should depend on the definition here, which it does.
\begin{definition}[FooBar]
\lipsum[2]
\end{definition}
\begin{example}[Example for FooBar]
\lipsum[2]
\end{example}
\end{document}
Which gives the output (except for the cref line, this is another post):