\documentclass{beamer}
\mode<presentation>
{
%  \usetheme{AnnArbor}
%  \usetheme{Dresden}
%  \usetheme{Montpellier}
%  \usetheme{Antibes}
%  \usetheme{Frankfurt}
%  \usetheme{PaloAlto}
%  \usetheme{Bergen}
%  \usetheme{Goettingen}
%  \usetheme{Pittsburgh}	%!!
%  \usetheme{Berkeley}
%  \usetheme{Hannover}
%  \usetheme{Rochester}		%!!!
%  \usetheme{Berlin}
%  \usetheme{Ilmenau}
%  \usetheme{Singapore}
%  \usetheme{Boadilla}		%viel platz
%  \usetheme{JuanLesPins}
%  \usetheme{Szeged}		%!
%  \usetheme{boxes}
%  \usetheme{Luebeck}
%  \usetheme{Warsaw}
% \usetheme{Copenhagen}
  \usetheme{Madrid}
%  \usetheme{Darmstadt}
%  \usetheme{Malmoe}
%  \usetheme{default}
%  \usetheme{Marburg}

%  \setbeamercovered{transparent}
  % oder auch nicht

  %navigation aus, klaut nur platz
  %\setbeamertemplate{navigation symbols}{}
  \beamertemplatenavigationsymbolsempty  
}
\usepackage[latin1]{inputenc}
%\usepackage[german]{babel}
\usepackage{latexsym}
\usepackage{amsfonts}
%\usepackage{natbib}
\usepackage{fancyhdr}
\usepackage{graphicx}
\usepackage{subfigure}
% oder was auch immer

\usepackage[latin1]{inputenc}
% oder was auch immer

\usepackage{times}
\usepackage[T1]{fontenc}
% Oder was auch immer. Zu beachten ist, das Font und Encoding passen
% müssen. Falls T1 nicht funktioniert, kann man versuchen, die Zeile
% mit fontenc zu löschen.

\usepackage{listings} 

\lstset{
language=Matlab,                % choose the language of the code
tabsize=2,
frame=none, 
numbers=left, 
numberstyle=\tiny, 
numbersep=5pt,
stepnumber=2,
basicstyle=\tiny
}

\title[]% (optional, nur bei langen Titeln nötig)
{Beyond the Mouse -- A Short Course on Programming}

\subtitle
{4. Fundamental Programming Principles II:\\
Control Structures (flow control)
}

\author[Ronni Grapenthin]% (optional, nur bei vielen Autoren)
{Ronni Grapenthin}
\institute[UAF/GI] % (optional, aber oft nötig)
{
Geophysical Institute, University of Alaska Fairbanks
}

% - Namen müssen in derselben Reihenfolge wie im Papier erscheinen.
% - Der \inst{?} Befehl sollte nur verwendet werden, wenn die Autoren
%   unterschiedlichen Instituten angehören.

% - Der \inst{?} Befehl sollte nur verwendet werden, wenn die Autoren
%   unterschiedlichen Instituten angehören.
% - Keep it simple, niemand interessiert sich für die genau Adresse.

\date[\today] % (optional, sollte der abgekürzte Konferenzname sein)
{\today}
% - Vollee oder abgekürzter Name sind möglich.
% - Dieser Eintrag ist nicht für das Publikum gedacht (das weiß
%   nämlich, bei welcher Konferenz es ist), sondern für Leute, die die
%   Folien später lesen.

%\AtBeginSection[]
%{
%  \frame{
%    \frametitle{Outline}
%    \tableofcontents[currentsection,currentsubsection]
%  }
%}

% Falls Aufzählungen immer schrittweise gezeigt werden sollen, kann
% folgendes Kommando benutzt werden:

%\beamerdefaultoverlayspecification{<+->}

%\pgfdeclareimage[width=1.75cm,height=1.75cm]{logo_hu}{redoubt_images/pdc_cloud_2.pdf}
%\titlegraphic{\pgfuseimage{logo_hu}}

\setbeamertemplate{footline}[frame number]

\begin{document}

\frame{
	\begin{columns}[t]
	\column{0.5\textwidth}
		\titlepage
	\column{0.5\textwidth}
		\begin{center}
		\includegraphics[width=.75\textwidth]{images/xkcd_2009_04_14_well_2_panel_2.pdf}
		\end{center}
		\begin{flushright}
			\tiny{``The Uncomfortable Truths Well'',\\ http://xkcd.com/568 (April 13, 2009)}
		\end{flushright}
	\end{columns}
}

%\frame{
%  \frametitle{Outline}
%  \tableofcontents
%}

\frame{
	\frametitle{Post Assignment 3: }
		\begin{center}
		Had fun at \\
		``The Uncomfortable Truths Well''?
		\end{center}

}

\frame{
	\frametitle{Some Comments \dots}
		\begin{itemize}
		\item You don't have to start with an empty file -- that's intimidating: use old file as `template'
		\item For Matlab: make it a habit to include `{\tt clear;}' and `{\tt clc;}' at the beginning of your scripts 
		\item Keep things nice and clean: definition of function in function file; use of function on command line or in script file (for Matlab that is)
		\end{itemize}

}

\section[Operator Precedence]{Operator Precedence}
\frame{
	\frametitle{For Reference \dots}
	It's usually a good idea to check the rules for operator precedence in the documentation of a programming language. \\[10pt]

	For {\bf MATLAB} that is:\\[10pt]
	\lstset{captionpos=b,
		tabsize=3,
		frame=lines, 
		numbers=none, 
		stepnumber=0, 
		escapeinside=,
		showstringspaces=false,
		numbersep=10pt}
	\lstinputlisting{./listings/04_fundamentals/operators.txt}

	\alert{Keep in mind that this may 
be different for another programming language!}
}

\section[Control Flow]{Control Flow}
\frame{
	\frametitle{Control Flow -- Redirecting the stream}
		\begin{center}
		\includegraphics<1>[width=10cm]{images/02_control_flow_01.pdf}
		\includegraphics<2>[width=10cm]{images/02_control_flow_02.pdf}
		\includegraphics<3>[width=10cm]{images/02_control_flow_03.pdf}
		\includegraphics<4>[width=10cm]{images/02_control_flow_full.pdf}
		\end{center}

}

\frame{
	\frametitle{Control Flow -- Redirecting the stream}
	\begin{block}{Flow control turns batch processing into programming:}
	\begin{itemize}
		\item (high level) programming languages allow different behavior based on conditions {\bf you} define -- {\bf flow control}
		\item A condition can be {\tt true} (1) or {\tt false} (0). 
		\item You test a condition using the operators: {\tt <, <=, >, >=, ==, !=} (find equiv. in each respective language)
		\item Function often give numeric return values as answer to a test. In Matlab {\tt strcmp('compare', 'strings')} will return {\tt false}.
	\end{itemize}	
	\end{block}
}


\frame{
	\frametitle{Logic 101}
	Use logic to connect multiple conditions and test for certain cases:\\[10pt]
	\begin{columns}[t]
	\column{3cm}<2->
		{\bf `NOT' (`\~{}', '!'):} \\
		\center{
		\begin{tabular}{|c|c|}
			\hline
			a	& 	!a \\\hline
			0	&	1 \\\hline
			1	&	0 \\\hline
		\end{tabular} 
		}
	\column{4cm}<3->
		{\bf `AND' (`\&\&'):}\\
		\center{
		\begin{tabular}{|c|c|c|}
			\hline
			a	&	b	& a \&\& b \\\hline
			0	&	0	&	0 \\\hline
			0	&	1	&	0 \\\hline
			1	&	0	&	0 \\\hline
			1	&	1	&	1 \\\hline
		\end{tabular} 
		}
	\column{4cm}<4->
		{\bf `OR' (`||'):}\\
		\center{
		\begin{tabular}{|c|c|c|}
			\hline
			a	&	b	& a || b \\\hline
			0	&	0	&	0 \\\hline
			0	&	1	&	1 \\\hline
			1	&	0	&	1 \\\hline
			1	&	1	&	1 \\\hline
		\end{tabular} 
		}
	\end{columns}
	\begin{block}{Examples}<5->
	\begin{itemize}
		\item<5->`Friday Beer': {\bf not} younger than 21 {\bf and} it must be Friday. Beer today?\\
		\item<6->`Game of life': heart beat {\bf or} self perception. Still alive?
	\end{itemize}
	\end{block}
}

\frame{
	\frametitle{Control flow (0) -- {\tt statements} and such}
	We need a little bit of a formal definition for the following slides. Bear with me
	\begin{block}{Formal language definitions}
		\begin{columns}[T]
		\column{0.75cm} ~
		\column{12cm}
		\lstinputlisting{./listings/04_fundamentals/bnf.txt}
		\end{columns}
		\begin{flushright}
		{\tiny Listing 2.1: bnf.txt}
		\end{flushright}
	\end{block}
}

\frame{
\lstset{stepnumber=0}
	\frametitle{Control flow (1) -- {\tt if - then - else}}
	\begin{block}{Formal}
		\lstset{language=}
		\lstinputlisting{./listings/04_fundamentals/if_formal.txt}
	\end{block}
	\begin{columns}[T]
	\column{5.5cm}
		\begin{block}{Matlab}<2->
		\lstset{language=Matlab}
		\lstinputlisting{./listings/04_fundamentals/if_example.m}
		\end{block}
		\column{5.5cm}
		\begin{block}{C-Shell}<3->
		\lstset{language=csh}
		\lstinputlisting{./listings/04_fundamentals/if_example.csh}
		\end{block}
	\end{columns}
}

\frame{
\lstset{stepnumber=0}
	\frametitle{Control flow (2) -- condition controlled loop: {\tt while} }
	\begin{block}{Formal}
		\lstset{language=}
		\lstinputlisting{./listings/04_fundamentals/while_formal.txt}
	\end{block}
	\begin{columns}[T]
	\column{5.5cm}
		\begin{block}{Matlab}<2->
		\lstset{language=Matlab}
		\lstinputlisting{./listings/04_fundamentals/while_example.m}
		\end{block}
		\column{5.5cm}
		\begin{block}{C-Shell}<3->
		\lstset{language=csh}
		\lstinputlisting{./listings/04_fundamentals/while_example.csh}
		\end{block}
	\end{columns}
	
}
\frame{
\lstset{stepnumber=0}
	\frametitle{Control flow (3) -- count controlled loop: {\tt for} }
	\begin{block}{Formal}
		\lstset{language=}
		\lstinputlisting{./listings/04_fundamentals/for_formal.txt}
	\end{block}
	\begin{columns}[T]
	\column{5.5cm}
		\begin{block}{Matlab}<2->
		\lstset{language=Matlab}
		\lstinputlisting{./listings/04_fundamentals/for_example.m}
		\end{block}
		\column{5.5cm}
		\begin{block}{C-Shell}<3->
		\lstset{language=csh}
		\lstinputlisting{./listings/04_fundamentals/foreach_example.csh}
		\end{block}
	\end{columns}
}

\frame{
\lstset{stepnumber=0}
	\frametitle{Control flow (4) -- breaking out and continuing loops: {\tt break, continue} }
	\begin{columns}[T]
	\column{5.5cm}
		\begin{block}{Matlab}<1->
		\lstset{language=Matlab}
		\lstinputlisting{./listings/04_fundamentals/for_break_example.m}
		\end{block}
		\column{5.5cm}
		\begin{block}{C-Shell}<2->
		\lstset{language=csh}
		\lstinputlisting{./listings/04_fundamentals/foreach_break_example.csh}
		\end{block}
	\end{columns}
}

\frame{
\lstset{stepnumber=0}
	\frametitle{Control flow (5) -- {\tt for} as {\tt while} }
	\begin{columns}[T]
	\column{5.5cm}
		\begin{block}{Matlab -- {\tt for} }<1->
		\lstset{language=Matlab}
		\lstinputlisting{./listings/04_fundamentals/for_example.m}
		\end{block}
		\column{5.5cm}
		\begin{block}{Matlab -- {\tt while}}<2->
		\lstset{language=Matlab}
		\lstinputlisting{./listings/04_fundamentals/for_as_while.m}
		\end{block}
	\end{columns}
}

\frame{
\lstset{stepnumber=0}
	\frametitle{Control flow (6) -- Error control: {\tt try-catch} }
	\begin{block}{Formal}
		\lstset{language=}
		\lstinputlisting{./listings/04_fundamentals/try_catch.txt}
	\end{block}
	\begin{block}{Matlab}<2->
	\lstset{language=Matlab}
	\lstinputlisting{./listings/04_fundamentals/try_catch_example.m}
	\end{block}
}


\frame{
	\frametitle{Don't you ever dare to {\tt goto}!}
	\begin{center}
	\includegraphics[width=.95\textwidth]{images/xkcd_goto.png}
	\end{center}
	\begin{flushright}
		\tiny{``GOTO'', http://xkcd.com/292}
	\end{flushright}

}

\section[Good Practice]{Good Practice}
\frame{
	\frametitle{Good Practice}
	\begin{block}{How to make your code readable (language independent)}<1->
	\begin{itemize}
	\item use indentations to structure your code (align comments etc)
	\item use meaningful variable and function names ({\tt sec} instead of {\tt i} and {\tt listFiles()} instead of {\tt lfls()})
	\item decide for one formatting and naming scheme and stick to it; no matter which one it is.
	\item comment your code
	\item do not over comment your code!
	\item {\tt try} and {\tt catch} errors
	\item selfstudy: {\footnotesize http://www.google.com/search?hl=en\&q=good+programming+style\&btnG=Search}
	\end{itemize}
	\end{block}

}


\end{document}
