
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  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 09 - Loop}

\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 21\\
	      \vskip 1\baselineskip
\end{itemize}
%\vspace{2in}
\end{frame} %-------------------------------------------------------------|


\section{while}

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

%  \begin{verbatim}
\texttt{while ( condition )}\\
\texttt{$\{$}\\
\qquad\texttt{executable1;}\\
\qquad\texttt{$\dots$}\\
\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} %-------------------------------------------------------------|

\section{operators}

\begin{frame} %-----------------------------------------------------------|
  \frametitle{Some Operators}
  \framesubtitle{~}

	\large

 \begin{itemize}
 	\item Compound assignment operators
 	     \begin{itemize} 	
 	        \item x $=$ x $+$ 1; \qquad $<==>$ \qquad x $+$$=$ 1;
          \item y $=$ y $-$ 15; \qquad $<==>$ \qquad  y $-$$=$ 15;
          \item count $=$ count $*$ 3; \qquad $<==>$ \qquad  count $*$ $=$ 3 ;
          \item k $=$ k $/$ 3; \qquad $<==>$ \qquad  k $/$ $=$ 3 ;
          \item z $=$ z $\%$ 3;  \qquad $<==>$ \qquad  z $\%$ $=$ 3;
 	     \end{itemize}
 		\vskip 1\baselineskip
 	\item Increment and decrement operators
 	     \begin{itemize} 	
 	        \item x $=$ x $+$ 1; \qquad $<==>$ \quad x$+$$+$ \quad(or $+$$+$x);
          \item y $=$ y $-$ 1; \qquad $<==>$ \quad y$-$$-$ \quad(or $-$$-$y);
          \end{itemize} 	
\end{itemize}
\vspace{2in}
\end{frame} %-------------------------------------------------------------|


\section{for}

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

 \begin{itemize}
 \item \texttt{Repetition: for a fixed number of times}
\vskip 1\baselineskip
\texttt{int x;}\\
\texttt{for ( x = 0; x < maxValue; x++)}\\
\texttt{$\{$}\\
\qquad \texttt{executable1;}\\
\qquad \texttt{$\dots$}\\
\texttt{$\}$}\\
\vskip 1\baselineskip
\item Should NOT make a change in conditional variable!

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


\begin{frame} %-----------------------------------------------------------|
  \frametitle{\texttt{for} loop: example}
  \framesubtitle{~}

 \begin{itemize}
 \item Example: Sum of 1,2,3,$\dots$100
\vskip 0.5\baselineskip
\texttt{int x, sum=0;}\\
\texttt{for ( x = 1; x <= 100; x++ )}\\
\qquad \texttt{sum += x;}\\

\vskip 1\baselineskip

 \item Counter initialized
 \item Condition is tested and loop repeated if condition holds
 \item Counter is incremented (or decremented)

 \vskip 0.5\baselineskip
\Large OR\\
\normalsize
\texttt{int x, sum=0;}\\
\texttt{for ( x = 100; x >= 1; x----)}\\
\qquad \texttt{sum += x;}\\

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




\section{do-while}

\begin{frame} %-----------------------------------------------------------|
  \frametitle{do while loop}
  \framesubtitle{~}
\vskip 0.5\baselineskip
 \begin{itemize}
 \item Repetition: At least once
\vskip 0.5\baselineskip
\texttt{do}\\
\texttt{$\{$}\\
\qquad\texttt{executable1;}\\
\qquad\texttt{$\dots$}\\
\qquad{change variable in conditional statement;}\\
\texttt{$\}$ while ( check condition );}\\
\vskip 1\baselineskip

 \item Similar to while loop
 \item Execute loop body at least once
 \item Don't miss ";"
 \end{itemize}

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


\begin{frame} %-----------------------------------------------------------|
  \frametitle{do while loop: example}
  \framesubtitle{~}

 \begin{itemize}
 \item Example: \\
 \vskip 0.5\baselineskip
\texttt{int num;}\\
\texttt{do}\\
\texttt{$\{$}\\
\qquad\texttt{printf(``Input a positive integer number:");}\\
\qquad\texttt{scanf(``$\%$d", $\&$num);}\\
\texttt{$\}$ while(num<=0);}\\
\vskip 1\baselineskip
 \item Loop is entered the first cycle
 \item Condition is tested after each loop is completed
 \item Loop is repeated as long as condition holds
 \item Conditional variable changed, otherwise, infinite loop
 \end{itemize}

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


\begin{frame} %-----------------------------------------------------------|
  \frametitle{Cheat Sheet}
  \framesubtitle{~}
	\texttt{while ( condition )}\\
	 $\{$ \\
	 \qquad \texttt{code...}\\
	  $\}$\\
\vskip 1\baselineskip	
	\texttt{do}\\
	 $\{$ \\
	 \qquad \texttt{code... }\\
	 $\}$ \texttt{while ( condition );}\\
\vskip 1\baselineskip
	\texttt{for ( i = 0; i < n; i++ )}\\
	$\{$ \\
	\qquad \texttt{code...}\\
	 $\}$\\
\vskip 1\baselineskip
\end{frame} %-------------------------------------------------------------|


\end{document} 