\documentclass{beamer} %include lhs2TeX.fmt \author{Piyush P Kurur\\ Office no: 224\\ Dept. of Comp. Sci. and Engg.\\ IIT Kanpur} \usepackage{tikz} \usepgfmodule{matrix} \usetikzlibrary{chains} \newcommand{\Token}[2][]{\node(#2#1)[token]{#2};} \newcommand{\Symbol}[2][]{\node(#2#1)[symbol]{#2};} \newcommand{\Point}[1]{\node(#1)[point]{#1};} \usepackage{multicol} \usetikzlibrary{positioning,shapes,chains} \usetikzlibrary{shapes.symbols} \usetikzlibrary{matrix} \usepackage{dot2texi} \input{../include/grammar.tex} \title{Fundamentals of Computing: Lecture 5} \date{August 5, 2009} \begin{document} \begin{frame} \maketitle \end{frame} \begin{frame} \frametitle{Summary of previous lecture} \begin{block}{Expressions} \begin{itemize} \item Integer expressions. \item Floating point expressions |float| and |double|. \item Boolean expressions (or rather the lack of boolean expressions) \end{itemize} \end{block} \begin{block}{Assignment} \begin{itemize} \item Assignment is also an expression \item Special assignements liket |i++| and |m *= 4| \item Problem with |==| and |=|. \end{itemize} \end{block} \end{frame} \begin{frame} \frametitle{Statement} \begin{block}{Simple statement} A simple statement is just an expression followed by |;| (semicolon). eg |x = 100;| \pause |f(x);| \end{block} \pause \begin{block}{Compound statement} A sequence of statements enclosed in a pair of braces eg \begin{spec} { foo = x; bar = y; } \end{spec} \end{block} \end{frame} \begin{frame} \frametitle{Control flow statements} Normally statements are executed in the sequence in which they are given. What if we want to change ? \begin{itemize} \item Conditional statements \item Loop statements. \end{itemize} \end{frame} \begin{frame} \frametitle{Conditional statement} \begin{spec} if ( expression ) statement if ( expression ) statement1 else statement2 \end{spec} \pause eg \begin{spec} if ( x > 42 ) { printf("Value too large"); } else { printf("just right") } \end{spec} \end{frame} \begin{frame} \frametitle{While loop} \begin{spec} while( expression ) statement \end{spec} \pause Example summing numbers till |n| \begin{spec} sum = 0; i = 0; while( i < n ) { sum += i; i++; } printf("sum till %d is %d",n,sum); \end{spec} \end{frame} \begin{frame} \frametitle{For loop} \begin{spec} for( initialisation ; condition; update) statement \end{spec} eg \begin{spec} for(int i = 0; i <= n; i++) { sum +=i; } printf("Sum till %d is %d",n,sum); \end{spec} \pause \newpage \begin{spec} for(int i = 0; i <= n; i++) sum +=i; printf("Sum till %d is %d",n,sum); \end{spec} \end{frame} \end{document} %[start chain, every node/.style={on chain, join},every join/.style={->}] % % \& \& |[symbol]|{expr} \& |[symbol]{binary op} \& \node[symbol]{expr}\\ % % \& \& \node[symbol]{unary op} \& \node[symbol]{expr} \\ \begin{frame} \frametitle{Grammar of expressions} \begin{tikzpicture}[tip/.style={->},join/.style={rounded corners}, thick,draw=black!50, hv path/.style={to path={-|(\tikztotarget)}}, vh path/.style={to path={|-(\tikztotarget)}}] \matrix[ampersand replacement =\&, row sep=0.25cm, column sep=0.5cm] { %%% Constant line \& \& \Point{c1}\& \Token{constant} \& \Point{c2}\\ \& \& \Point{v1}\& \Symbol{variable} \& \Point{v2}\\ \Point{p0}\& \Point{p1} \& \& \& \& \Point{p2} \& \Point{p3} \\ \& \& \Symbol[1]{expr} \& \Symbol{bin-op} \& \Symbol[2]{expr}\\ \& \& \Symbol{un-op} \& \Symbol[3]{expr}\\ }; \begin{scope} [start chain]; \chainin(p0); \chainin(p1); \begin{scope}[start branch=const] \chainin(c1)[join =by {vh path,tip}]; \chainin(constant); \chainin(c2); \chainin(p2)[join =by {hv path, tip}]; \end{scope}; \end{scope} \end{tikzpicture} \end{frame}