Q:

ifpackageloaded question

why when i comment \usepackage{geometry} i get error?
\documentclass{book}

\usepackage{geometry}

\makeatletter
\@ifpackageloaded{geometry}
 {                               % i the package was loaded 
    \newcommand*{\qw}{\itshape}  % this is enabled, else is not enabled.                                                                                               
 }{}
\makeatother

\begin{document}
\qw hallo
\end{document}

A:

LaTeX defines several conditional macros, with a syntax very different from the primitive 
\if?<condition><true>\else<false>\fi

where \if? denotes any of the primitive conditionals. The conditionals defined by LaTeX have the form
\@ifsomething{<arg>}{<true>}{<false>}

or
\@ifsomething{<arg1>}{<arg2>}{<true>}{<false>}

Conditionals of the first kind are \@ifundefined and \@ifpackageloaded; among the second kind are \@ifpackagelater and \@ifpackagewith. The motivation for the distinction is that in the second kind we need something more to check a condition (the package name and an option name, for \@ifpackagewith).
The <true> and <false> code are executed respectively when the condition is satisfied or not. So
\@ifpackageloaded{geometry}
  {\newcommand\qw{\itshape}}
  {\newcommand\qw{\bfseries}}

is the answer. Either argument can be empty, of course.
You find an example of \@ifpackageloaded and some more information in Italian in this book (p. 78 for the main problem)
Note There is also \@ifdefinable<control sequence>{<true>} that has no "false" branch; it will execute the code <true> if the <control sequence> is undefined and raise an error otherwise (ignoring the <true> code).

A:

The else part should go in the second argument:
\documentclass{book}

%\usepackage{geometry}

\makeatletter
\@ifpackageloaded{geometry}
 {% if the package was loaded
  \newcommand*{\qw}{\itshape}%
 }
 {%else:
  \newcommand*{\qw}{\bfseries}%
 }
\makeatother

\begin{document}
\qw hallo
\end{document}

A:

I think what you want is
\documentclass{book}

\usepackage{geometry}

\makeatletter
\@ifpackageloaded{geometry}
 {
    \newcommand*{\qw}{\itshape}  % this is enabled
 }{
    \newcommand*{\qw}{\bfseries} % this is enabled?
}
\makeatother

\begin{document}
\qw hallo
\end{document}

The \@ifpackageloaded takes two arguments: what to do if the package is loaded or not. Your \if doesn't really do anything.
Edit: The question seems to have changed
Now the problem is that if the package is not loaded, then the macro \qw does not get defined. Something like this might be what you want
\documentclass{book}

\usepackage{geometry}

\makeatletter
\@ifpackageloaded{geometry}
 {
    \newcommand*{\qw}{\itshape}  % this is enabled
 }{
    \newcommand*{\qw}{} % this is enabled?
}
\makeatother

\begin{document}
\qw hallo
\end{document}

