\documentclass[10pt]{article}

\usepackage{hyperref}
\usepackage[a4paper, total={6in, 8in}, top=.75in, bottom=1in]{geometry}
\usepackage{graphicx}
\usepackage{comment}
\usepackage{url}
\usepackage{courier}
\usepackage{color}
\usepackage{amssymb}

\usepackage[normalem]{ulem}
\useunder{\uline}{\ul}{}
\DeclareGraphicsExtensions{.pdf,.png,.jpg}
\usepackage{color}
\newcommand*\ShiftLeft{\ll}
\newcommand*\ShiftRight{\gg}
\newcommand*\BitOr{\mathrel{|}}
\newenvironment{ppl}{\fontfamily{courier}\selectfont}{\par}


\definecolor{pblue}{rgb}{0.13,0.13,1}
\definecolor{pgreen}{rgb}{0,0.5,0}
\definecolor{pred}{rgb}{0.9,0,0}
\definecolor{pgrey}{rgb}{0.46,0.45,0.48}

\usepackage{listings}
\lstset{language=Java,
  showspaces=false,
  showtabs=false,
  breaklines=true,
  showstringspaces=false,
  breakatwhitespace=true,
  commentstyle=\color{pgreen},
  keywordstyle=\color{pblue},
  stringstyle=\color{pred},
  basicstyle=\ttfamily,
  moredelim=[il][\textcolor{pgrey}]{$$},
  moredelim=[is][\textcolor{pgrey}]{\%\%}{\%\%}
}
\begin{document}
\title{Building Blocks -- Statements}
\author{Dr. Yvon Feaster}
\date{}
\maketitle

\section*{Statements}
The syntax and semantics of statements in Java are nearly identical to those of `C' and `C++'.  There are two major  categories of statements in Java:  \textbf{Simple}, which are basic building blocks and \textbf{compound}, which are statements composed of other statements.  Examples of simple include {\em{expression}} statements, {\em{local variable declarations, break, continue,}} and {\em{return}} statements.  Compound statements include {\em{statement blocks, selection}}, statements, and {\em{try-catch}} statements.  \\

\noindent We will now take a look at examples of expression statements.\\

\noindent Assignments increment and decrement are considered statements because they have a terminating  semicolon at the end of the statement.  \\

x = 5;\\
\indent i++; and \\ 
\indent  - -j; \\

\noindent Creating an object or the invocation of a method is also a statement because of the terminating semicolon at the end of the statement.\\

point1 = new Point(); and \\
\indent point1.move(10,10);\\


\noindent A statement block consists of sequence of statements or local variable declarations enclosed in a pair of curly brackets.  The block of code can include simple or compound statements.  

\noindent \{

\indent Statement1;\\
\indent Statement2;\\
\indent Statement3;\\

\noindent \}

or

\noindent \{

\indent int i = 0;\\
\indent prime[i++] = 2;\\
\indent prime[i++] = 3;\\
\indent .......\\
\noindent \}

\noindent Local variable declarations are also simple statements.  I think you know what they look like so I won't give examples.\\

\noindent The return statement is also a statement.  Again you know what a return statement is so I will not spend time on return statements.\\

\noindent  The next several sections will cover \textbf{Decision-Making, Looping, and Branching} Statements.
\vspace{2 in}\footnote {Many of the following examples were borrowed from \url{http://www.tutorialspoint.com/java/java_loop_control.htm}}
\subsubsection*{Decision-Making }
These statements include,\\
 \textbf{if}\\

\noindent if (Condition) \\
\indent{statement} \\

\noindent \textbf{if-else}\\

\noindent if (Condition) \\
\indent{statement} \\
\noindent else\\
\indent{statement}\\

\noindent \textbf{switch}\\

\noindent switch(Expression)\\
 \{\\
   \indent case CaseLabel:\\
   \indent \indent Statement\\
  \indent ...\\
   \indent case CaseLabel:\\
   \indent  \indent Statement\\
   \indent default:\\
   \indent  \indent Statement\\
\}\\


\subsubsection*{Loop Statements}
In this section we are going to discuss several type of looping statements. \\

\begin{enumerate}
	\item  \textbf{while}: The while statement continually executes a block of statements  as long as a {\em{boolean expression}} is \textbf{true}.\\
	The syntax for a while loop is:
\begin{lstlisting}
	while(Boolean_expression)
	{
	      //One or more statements
	}
	
	public class Test
	{
		public static void main (String args[])
		{
			int x = 10;
		
			while( x < 20)
			{
				System.out.print("value of x : " + x);
				x++;
				System.out.print("\n");
			}
		}
	}

\end{lstlisting}

	\item \textbf{do ... while}: The do ... while loop is similar to the while loop, except it evaluates the expression at the bottom of the loop instead of the top which guarantees the loop is executed once before the Boolean is tested.
\begin{lstlisting}
	public class Test
	{
		public static void main (String args[])
		{
			int x = 10;
		
			do
			{
				System.out.print("value of x : " + x);
				x++;
				System.out.print("\n");
			}while( x < 20 );
		}
	}
\end{lstlisting}


	\item \textbf{for}: The `for' statement provides a way to iterate repeatedly until a particular condition is satisfied.
	
\begin{lstlisting}
	for(initialization; termination; increment or decrement)
	{
		//statements
	}
\end{lstlisting}

	\item \textbf{for-each or enhanced for loop:} This type of `for' loop is mainly used for iterating through an array.\\
	
\begin{lstlisting}
	for(declaration : expression)
	{
		//statements
	}
\end{lstlisting}
	
	
\begin{lstlisting}
	
	public class Test 
	{
		public static void main(String args[])
		{
      			int [] numbers = {10, 20, 30, 40, 50};

     			for(int x : numbers )
			{
         			System.out.print( x );
         			System.out.print(",");
     			}
			
     			System.out.print("\n");
      			String [] names ={"James", "Larry", "Tom", "Lacy"};
			
      			for( String name : names ) 
			{
         			System.out.print( name );
         			System.out.print(",");
      			}
		}
	}
	
\end{lstlisting}
	
	The output of this is: \\ 
	10,20,30,40,50,\\
	James,Larry,Tom,Lacy,\\	
\end{enumerate}


\subsubsection*{Branching Statements\footnote{This section is borrowed from notes provided by Catherine Hochrine} }
\begin{enumerate}

	\item The \textbf{break} statement has two forms:  labeled and unlabeled.  The unlabeled break is like the one used in a `switch' statement.  You can also use an unlabeled break to terminate a for, while, or do-while loop, although this practice is usually seen as sloppy programming and is discouraged by some.
	
\begin{lstlisting}
 //unlabeled break
	public class Test{
		public static void main(String args[])
		{
			int [] numbers = {10,20,30,40,50};
			
			for(int x : numbers)
			{
				if(x == 30)
				{
				   break;
				}
				System.out.print(x);
				System.out.print("\n");
			}
		}
	}
	
\end{lstlisting}

A labeled break statement terminates an outer statement that is labeled by an identifier.  For example, if you have nested `for' loops, labeled with the word ``search" right before the first for loop, you can put the following statement ``break search" inside the inner for loop to break out of both when the condition is met, and the control flow continues with the statement immediately following the labeled statement.

\begin{lstlisting}
	search:
		for(i=0; i < arrayOfInts.length; i++)
		{
			for(j = 0: j < arrayOfInts[i].length; j++)
			{
				if(arrayOfInts[i][j] == searchfor)
				{
				   foundIt = true;
				   break search;
				}
			}
		}
				
\end{lstlisting}
\vspace{2 in}

	\item The \textbf{continue} statement skips the current iteration of a loop.  The unlabeled form skips to the end of the innermost loop's body and evaluates the expression that controls the loop.
	
\begin{lstlisting}
	for(int i = 0; i < max; i++)
	{
		//interested only in p's
		if(searchMe.charAt(i) != p )
		{
			continue;
	        }
		//process p - only increments if it found a p
		numPs++;
	}
\end{lstlisting}

\noindent A labeled continue statement skips the current iteration of an outer loop marked with the given label.

\begin{lstlisting}
	test:
	   for(int i = 0; i <= max; i++)
	   {
	      int n = substring.length();
	      int j = i;
	      int k = 0;
	      while (n-- != 0)
	      {
	         if(searchMe.charAt(j++) != substring.charAt(k++))
	         {
	            continue test;
	         }
	      }
	      foundIt = true;
	         break test;
	  }
\end{lstlisting}	

	\item The \textbf{return} statement exits from the current method and returns control back to where the method was invoked from 
\end{enumerate}
\end{document}


