Skip to content
Snippets Groups Projects
Commit 4fc07e09 authored by Walmes Marques Zeviani's avatar Walmes Marques Zeviani
Browse files

Complementa revisão de programação.

parent 1544df3a
No related branches found
No related tags found
No related merge requests found
...@@ -46,7 +46,7 @@ source("config/setup.R") ...@@ -46,7 +46,7 @@ source("config/setup.R")
\end{frame} \end{frame}
%----------------------------------------------------------------------- %-----------------------------------------------------------------------
\begin{frame}[fragile, allowframebreaks]{Estruturas de controle} \begin{frame}[t, fragile, allowframebreaks]{Estruturas de controle}
\begin{minipage}[t]{0.55\linewidth} \begin{minipage}[t]{0.55\linewidth}
...@@ -118,6 +118,12 @@ source("config/setup.R") ...@@ -118,6 +118,12 @@ source("config/setup.R")
<statments if FALSE and FALSE> <statments if FALSE and FALSE>
} }
@ @
\begin{itemize}
\item O R não tem o contrutor \texttt{elif}.
\item Quando o código tem uma linha, não precisa de \texttt{\{\}}.
\end{itemize}
\end{minipage} \end{minipage}
\framebreak %------------------------------ \framebreak %------------------------------
...@@ -148,10 +154,13 @@ source("config/setup.R") ...@@ -148,10 +154,13 @@ source("config/setup.R")
@ @
\begin{itemize} \begin{itemize}
\item O loop é executado enquanto a condição for \texttt{TRUE}.
\item \texttt{break} e \texttt{next} podem ser usados. \item \texttt{break} e \texttt{next} podem ser usados.
\item Cuidado com loops sem condições de parada (loops infinitos). \item Cuidado com loops sem condições de parada (loops infinitos).
\end{itemize} \end{itemize}
\framebreak %------------------------------
\begin{block}{Pergunta} \begin{block}{Pergunta}
\begin{enumerate} \begin{enumerate}
\item Quando usar \texttt{for} e \texttt{while}? \item Quando usar \texttt{for} e \texttt{while}?
...@@ -163,8 +172,200 @@ source("config/setup.R") ...@@ -163,8 +172,200 @@ source("config/setup.R")
{\large Laço de repetição com condional (repeat)} {\large Laço de repetição com condional (repeat)}
<<eval = FALSE>>=
repeat {
<statments before 1st condition>
if (<1st condition>) break
<statments before 2nd condition>
if (<2nd condition>) break
<statments after 2nd condition>
}
@
\begin{itemize}
\item O loop é executado enquanto as condiçõs forem \texttt{TRUE}.
\item \texttt{break} e \texttt{next} podem ser usados.
\item As condições podem ser várias e estar em diferentes posições
dentro do código.
\end{itemize}
\framebreak %------------------------------
\begin{block}{Pergunta}
\begin{enumerate}
\item Quando usar \texttt{while} e \texttt{repeat}?
\item Qual dos dois é mais geral?
\end{enumerate}
\end{block}
\framebreak %------------------------------
{\large Estrutura de casos (switch)} {\large Estrutura de casos (switch)}
<<eval = FALSE>>=
switch(<element>,
<value_1> = {
<statments 1st condition>
},
<value_2>= {
<statments 2nd condition>
},
{
<statments last condition>
})
@
\begin{itemize}
\item \texttt{switch()} pode representar estruturas de \texttt{if else
if} planas.
\item A condição é sempre a de igualdade.
\item Geralmente \texttt{switch()} é usado para igualdade de valor de
\emph{strings}.
\end{itemize}
\end{frame} \end{frame}
%-----------------------------------------------------------------------
\begin{frame}[t, fragile, allowframebreaks]{Alguns problemas}
Qual o resultado desse código?
<<eval = FALSE>>=
x <- 7
if (x > 3) {
print("3")
if (x < 5) {
print("5")
if (x == 7) {
print("7")
}
}
}
@
\framebreak
Quais números são exibidos com a execução?
<<eval = FALSE>>=
i <- 3
while (i >= 0) {
print(i)
i <- i - 1
}
@
\framebreak
Quais números são exibidos com a execução?
<<eval = FALSE>>=
i <- 5
while (TRUE) {
print(i)
i <- i - 1
if (i <= 2) {
break
}
}
@
Como ficaria se fosse usado \texttt{repeat}.
\framebreak
Qual o valor de \texttt{j}?
<<eval = FALSE>>=
j <- 0
for (i in 1:10) {
if (j %% 3 == 0) {
j <- 1
}
j <- i
}
@
\end{frame}
%-----------------------------------------------------------------------
\begin{frame}[t, fragile, allowframebreaks]{Funções}
{\large Protótipo da função}
<<eval = FALSE>>=
<funname> <- function(<arg_1>, <arg_2>, <arg_n>) {
<statments>
return(<values>)
}
<funname>(<arg_1> = <value_1>, <arg_2> = <value_2> , <arg_n> = <value_n>)
@
\framebreak
\begin{minipage}[t]{0.47\linewidth}
{\large Exemplo: fórmula de Báskara}
\begin{equation*}
x = \dfrac{-b \pm \sqrt{b^2 - 4 a c}}{2 a}
\end{equation*}
\end{minipage}
\hspace{1em}
\begin{minipage}[t]{0.47\linewidth}
<<results = "hold">>=
baskara <- function(a, b, c) {
stopifnot(a != 0)
delta <- b^2 - 4 * a * c
if (delta > 0) {
den <- 2 * a
sqrt_delta <- sqrt(delta)
x1 <- (-b - sqrt_delta)/den
x2 <- (-b + sqrt_delta)/den
return(c(x1, x2))
} else {
return(NULL)
}
}
baskara(-3, 2, 1)
# baskara(3, 2, 1)
# baskara(0, 2, 1)
@
\end{minipage}
\end{frame}
%-----------------------------------------------------------------------
%-----------------------------------------------------------------------
{
\usebackgroundtemplate{\includegraphics[height=\paperheight, width=\paperwidth]{../img/looking-ahead.jpg}}
% \setbeamersize{text margin left=30mm}
\begin{frame}[b]{}
\hspace*{0.5\linewidth}
\begin{minipage}[t]{0.5\linewidth}
\hi{Próximo assunto}
\begin{itemize}
\item Representação de código.
\item Práticas de implementação.
\item Programação funcional.
\end{itemize}
\hi{Semana que vem}
\begin{itemize}
\item Sabatina no Moodle.
\item Aula com outro professor (talvez).
\end{itemize}
\end{minipage}
\end{frame}
}
%-----------------------------------------------------------------------
\end{document} \end{document}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment