
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  CSCE 150A -- Problem Solving Using Computers
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\documentclass[pdftex]{beamer}

\input{beamerPreamble}
\usepackage[T1]{fontenc}
%\usepackage{tikz}\text
%\usetikzlibrary{shapes.callout}

\title[CSCE150A]{Computer Science \& Engineering 150A\\
    Problem Solving Using Computers -- Laboratory}
\subtitle{Lecture 08 - Loop Statement}

\author[~]{Shuai Xie\vskip 1\baselineskip (Adapted from Derrick Stolee, Lin Liu \& Shuai Xie )} %

\date{Spring 2010}
%\usepackage{listings}
\usepackage{multicol}
\begin{document}
%\lstset{language=C}
\begin{frame}
  \titlepage
  \begin{center}
     \vfill
     \email{sxie@cse.unl.edu}
  \end{center}
\end{frame}

\section{Assignment}

\begin{frame} %-----------------------------------------------------------|
  \frametitle{Announcement: CodeLab assignment}
  \framesubtitle{~}

\begin{itemize}
	\item The 4rd CodeLab assignment\\
	\vskip 1\baselineskip
	 \qquad Due:  23:59, Mar 17\\
	      \vskip 1\baselineskip
\end{itemize}
%\vspace{2in}
\end{frame} %-------------------------------------------------------------|


\section{Review}

\begin{frame} %-----------------------------------------------------------|
  \frametitle{If...else if...else...}
  \framesubtitle{~}
\footnotesize
\texttt{if ( logical expression 1)}\\
$\{$\\
\quad \texttt{executable statement 1;}\\
\quad \texttt{\dots}\\
$\}$\\
\texttt{else if ( logical expression 2)}\\
$\{$\\
\quad \texttt{executable statement 2;}\\
\quad \texttt{\dots}\\
$\}$\\
\texttt{else}\\
$\{$\\
\quad \texttt{executable statement 3;}\\
\quad \texttt{\dots}\\
$\}$\\
\large
\begin{itemize}
\item if$\dots$else if$\dots$else$\dots$ is more efficient than if$\dots$if$\dots$if$\dots$
\end{itemize}

\vspace{2in}
\end{frame} %-------------------------------------------------------------|


\begin{frame} %-----------------------------------------------------------|
  \frametitle{Multiple conditions in if statement}
  \framesubtitle{~}

\texttt{if ( ( logical expression 1) $\&$$\&$ ( logical expression 2) $\&$$\&$ ( logical expression 3 ) )}\\
$\{$\\
\quad \texttt{executable statement;}\\
\quad \texttt{\dots}\\
$\}$\\

\vspace{2in}
\end{frame} %-------------------------------------------------------------|


\begin{frame} %-----------------------------------------------------------|
  \frametitle{Nested if statement}
  \framesubtitle{~}

\texttt{if ( logical expression ) } \\
$\{$\\
\qquad \texttt{ if ( logical expression ) }\\
\quad \quad$\{$\\
\qquad \qquad \texttt{executable statement;}\\
\qquad \qquad \texttt{\dots}\\
\quad \quad$\}$\\
\qquad \texttt{else}\\
\quad \quad$\{$\\
\qquad \qquad \texttt{executable statement;}\\
\qquad \qquad \texttt{\dots}\\
\quad \quad$\}$\\
$\}$\\

\vspace{2in}
\end{frame} %-------------------------------------------------------------|

\begin{frame} %-----------------------------------------------------------|
  \frametitle{Switch statement}
  \framesubtitle{~}

\footnotesize
\texttt{switch ( controlling\_variable ) }\\
$\{$\\
\quad \quad \texttt{ case 1:}  \\
\quad \quad \qquad \texttt{ executable group 1 ;}  \\
\quad \quad \qquad \texttt{ break ;}  \\
\quad \quad \texttt{ case 2:}  \\
\quad \quad \qquad \texttt{ executable group 2 ;}  \\
\quad \quad \qquad \texttt{ break ;}  \\
\quad \quad \qquad \texttt{ \dots}  \\
\vskip 0.5\baselineskip
\quad \quad \texttt{ default:}  \\
\quad \quad \qquad \texttt{ executable group last ;}  \\
$\}$\\

\begin{itemize}
\item \texttt{controlling\_variable: int, char}
\item \texttt{char variable:  'c' ;}
\item \texttt{break;}
\item \texttt{other cases $-$$-$ default :}
\end{itemize}

\vspace{2in}
\end{frame} %-------------------------------------------------------------|


\section{While Loop}

\begin{frame} %-----------------------------------------------------------|
  \frametitle{Loops}
  \framesubtitle{~}

 \begin{itemize}
 \item \texttt{loop or iteration: do sth. more than once.}
 \vskip 0.5\baselineskip
 \item \texttt{C language supports three kinds of loops:}
    \begin{itemize}
    \item \texttt{while loop}
    \item \texttt{for loop}
    \item \texttt{do while loop}
    \end{itemize}
  \vskip 0.5\baselineskip
  \item \texttt{loop condition:}
   \begin{itemize}
   \item condition-controlled looping (while, do while)\\
 A condition changed which will cause loop stop;\\
   \item counter-controlled looping (for)\\
 Loop runs for a certain number of times;\\
   \end{itemize}
      \vskip 0.5\baselineskip
    \item Infinite loop: loop runs for ever.
 \end{itemize}
\vspace{2in}
\end{frame} %-------------------------------------------------------------|


\begin{frame} %-----------------------------------------------------------|
  \frametitle{While loop}
  \framesubtitle{~}

%  \begin{verbatim}
\texttt{while ( condition )}\\
\texttt{$\{$}\\
\qquad\texttt{executable1;}\\
\qquad\texttt{executable2;}\\
\qquad{change variable in conditional statement}\\
\texttt{$\}$}\\
%  \end{verbatim}
 \begin{itemize}
 \item Condition: any boolean value or expression
 \item Test condition. If it's true: execute loop body, repeats; false: skipped.
 \item Must make a change in conditional variable within the loop to avoid infinite loop.
 \item Get program to stop: "CTRL+C"
 \end{itemize}

\vspace{2in}
\end{frame} %-------------------------------------------------------------|



\begin{frame} %-----------------------------------------------------------|
  \frametitle{While loop: example 1}
  \framesubtitle{~}

\#include $<$stdio.h$>$\\
\vskip 0.4\baselineskip
int main(void)\\
$\{$ \\
\quad \texttt{int x = 0;}\\
\quad \texttt{while( x < 1 || x > 100 ) }\\
\quad$\{$ \\
\qquad \texttt{printf("Input a number between 1 and 100$\setminus$n");}\\
\qquad \texttt{scanf("$\%$d",$\&$x);}\\
\quad$\}$ \\
\quad \texttt{printf("The square of $\%$d is $\%$d$\setminus$n", x, x * x );}\\
$\}$\\
\vskip 0.5\baselineskip
\texttt{Note: use scanf to change loop condition.}\\

\vspace{2in}
\end{frame} %-------------------------------------------------------------|


\begin{frame} %-----------------------------------------------------------|
  \frametitle{While loop: example 2}
  \framesubtitle{~}

\texttt{int sum(int n)}\\
$\{$ \\
\quad \texttt{int i = 1;}\\
\quad \texttt{int sum = 0;}\\
\quad \texttt{while ( i <= n ) }\\
\quad$\{$ \\
\qquad \texttt{sum = sum + i;}\\
\qquad \texttt{i = i + 1;}\\
\quad$\}$ \\
\quad \texttt{return sum; }\\
\large
$\}$ \\

\vskip 0.5\baselineskip
\texttt{Note: use i + 1 to change loop condition.}\\

\vspace{2in}
\end{frame} %-------------------------------------------------------------|

\section{Basic Operation}

\begin{frame} %-----------------------------------------------------------|
  \frametitle{Exchange values of two variables x and y}
  \framesubtitle{~}
\begin{itemize}
\item \texttt{Use the third temporary variable:}
\end{itemize}
\vskip 0.5\baselineskip
\qquad \texttt{int tmp;}\\
\vskip 0.5\baselineskip
\qquad \texttt{tmp = x;}\\
\qquad \texttt{x = y;}\\
\qquad \texttt{y = tmp;}\\
\vskip 0.5\baselineskip

\vspace{2in}
\end{frame} %-------------------------------------------------------------|


\begin{frame} %-----------------------------------------------------------|
  \frametitle{Order of three variables x, y and z}
  \framesubtitle{~}
\begin{itemize}
\item \texttt{Six cases:} \\

\end{itemize}
\vskip 0.5\baselineskip
\qquad \texttt{if ( x <= y $\&$$\&$ y <= z )}\\
\vskip 0.5\baselineskip
\qquad \texttt{else if ( x <= z $\&$$\&$ z <= y )}\\
\vskip 0.5\baselineskip
\vskip 0.5\baselineskip
\qquad \texttt{else if ( y <= x $\&$$\&$ x <= z )}\\
\vskip 0.5\baselineskip
\qquad \texttt{else if ( y <= z $\&$$\&$ z <= x )}\\
\vskip 0.5\baselineskip
\vskip 0.5\baselineskip
\qquad \texttt{else if ( z <= x $\&$$\&$ x <= y )}\\
\vskip 0.5\baselineskip
\qquad \texttt{else if ( z <= y $\&$$\&$ y <= x )}\\
\vskip 0.5\baselineskip

\vspace{2in}
\end{frame} %-------------------------------------------------------------|




\begin{frame} %-----------------------------------------------------------|
  \frametitle{Minimum value of three variables x, y and z}
  \framesubtitle{~}
\begin{itemize}
\item \texttt{Three cases:} \\
\end{itemize}
\vskip 0.5\baselineskip
\qquad \texttt{if ( x <= y $\&$$\&$ x <= z )}\\
\qquad \qquad \texttt{min = x;}\\
\vskip 0.5\baselineskip
\qquad \texttt{else if ( y <= x $\&$$\&$ y <= z )}\\
\qquad \qquad \texttt{min = y;}\\
\vskip 0.5\baselineskip
\qquad \texttt{else if ( z <= x $\&$$\&$ z <= y )}\\
\qquad \qquad \texttt{min = z;}\\
\vskip 0.5\baselineskip
\vspace{2in}
\end{frame} %-------------------------------------------------------------|


\end{document} 