\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{Bayan (Bayan.A)} \pdfinfo{ /Title (java-mastery-2.pdf) /Creator (Cheatography) /Author (Bayan (Bayan.A)) /Subject (Java Mastery 2 Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{7D8091} \definecolor{LightBackground}{HTML}{F6F7F8} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{Java Mastery 2 Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{Bayan (Bayan.A)} via \textcolor{DarkBackground}{\uline{cheatography.com/122738/cs/31553/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}Bayan (Bayan.A) \\ \uline{cheatography.com/bayan-a} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Not Yet Published.\\ Updated 7th April, 2022.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{3} \begin{tabularx}{5.377cm}{x{1.84149 cm} x{3.13551 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Java Structure}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Package}} & contains one or more classes. \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} {\bf{Class }} & contains one or more fields and methods. \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} {\bf{Method }} & contains declarations and statements. \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} {\bf{Statement }} & contains declarations, statements, and expressions. \tn % Row Count 9 (+ 3) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\emph{ {\bf{These may contain comments:}} }}} \tn % Row Count 10 (+ 1) % Row 5 \SetRowColor{white} {\bf{Single line: }} & // \tn % Row Count 12 (+ 2) % Row 6 \SetRowColor{LightBackground} {\bf{Multi-line }} & starts with /{\emph{ and ends with }}/ \tn % Row Count 14 (+ 2) % Row 7 \SetRowColor{white} {\bf{Documentation notes}} & starts with /{\emph{* and ends with }}/, put before the definition of a variable, method or class \tn % Row Count 18 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.94103 cm} x{3.03597 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Data Types and Variable Declaration}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{`int`}} & holds integer values (between −231 and 231 − 1). \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} {\bf{`double`}} & holds floating-point numbers (i.e., numbers containing a decimal point). \tn % Row Count 6 (+ 3) % Row 2 \SetRowColor{LightBackground} {\bf{`boolean` }} & holds a true or false value. \tn % Row Count 8 (+ 2) % Row 3 \SetRowColor{white} {\bf{`char`}} & holds single characters(can also be used as numbers) \tn % Row Count 11 (+ 3) % Row 4 \SetRowColor{LightBackground} {\bf{`float `}} & holds less accurate floating-point numbers. \tn % Row Count 13 (+ 2) % Row 5 \SetRowColor{white} {\bf{`byte, short and long`}} & holds integers with fewer or more digits. \tn % Row Count 15 (+ 2) % Row 6 \SetRowColor{LightBackground} {\bf{`String`}} & composed of zero or more chars, it is an object, not a primitive type. \tn % Row Count 18 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Variable declaration examples: \newline `int age; //without initial value` \newline `int count = 0; //with initial value of 0` \newline `double distance = 37.95;` \newline `boolean isReadOnly = true;` \newline `String greeting = "Welcome to SP2";` \newline `String outputLine;`} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.4977 cm} p{0.4977 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Enumeration types}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{An easy way to name a finite list of values that a variable can hold} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Like declaring a new type, with a list of possible values} \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Can have any number of values, but you must include them all in the enum declaration} \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Can declare variables of the enumeration type:} \tn % Row Count 7 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Can use the comparison operator with them:} \tn % Row Count 8 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Example: \newline \newline {\bf{Claring a new type with a list of possible values:}} \newline `public enum FilingStatus \{ ` \newline `SINGLE, MARRIED, \seqsplit{MARRIED\_FILING\_SEPARATELY} \}` \newline \newline {\bf{Enumeration type variable declaration:}} \newline `FilingStatus status = FilingStatus.SINGLE;` \newline \newline {\bf{With comparison operator:}} \newline `if (status == FilingStatus.SINGLE)`} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Reading input}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Structure}}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} Import Scanner class: & `import java.util.Scanner;` \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} Create a scanner, assign it to a variable: & `Scanner scanner = new \seqsplit{Scanner(System.in);`} \tn % Row Count 6 (+ 3) % Row 3 \SetRowColor{white} & `new Scanner(..)` creates a new one \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} & System.in says scanner is to take input from the keyboard \tn % Row Count 11 (+ 3) % Row 5 \SetRowColor{white} Request user to input number & \seqsplit{`System.out.print("Please} input data: ");` \tn % Row Count 14 (+ 3) % Row 6 \SetRowColor{LightBackground} Read in the number: & `myNumber = scanner.nextInt();` \tn % Row Count 16 (+ 2) % Row 7 \SetRowColor{white} Read in String: & String myString = scanner.nextLine(); \tn % Row Count 18 (+ 2) % Row 8 \SetRowColor{LightBackground} Read in double: & String myDouble = \seqsplit{scanner.nextDouble();} \tn % Row Count 20 (+ 2) % Row 9 \SetRowColor{white} Read in char: & char myChar = \seqsplit{scanner.next().charAt(0);} \tn % Row Count 22 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Printing}} \tn % Row 0 \SetRowColor{LightBackground} Print and end line & \seqsplit{System.out.println(something);} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} Print and doesn't end the line & \seqsplit{System.out.print(something);} \tn % Row Count 4 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.24425 cm} x{3.73275 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Assignment statements}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{alues can be assigned to variables by assignment statements.} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} Syntax: & variable = expression \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{The expression must be of the same type as the variable.} \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{The expression may be a simple value or it may involve computation} \tn % Row Count 7 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{When a variable is assigned a value, the old value is discarded and totally forgotten.}}} \tn % Row Count 9 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.69218 cm} x{3.28482 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Methods}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{This is a named group of declarations and statements} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{They are called or invoked by naming it in a statement} \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Every method definition must specify a return type} \tn % Row Count 5 (+ 1) % Row 3 \SetRowColor{white} Return type void: & used if nothing is to be returned \tn % Row Count 7 (+ 2) % Row 4 \SetRowColor{LightBackground} & plain `return` can be used \tn % Row Count 8 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{If not `void`, return statements that specify the value to be returned must be supplied} \tn % Row Count 10 (+ 2) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Method call:} \tn % Row Count 11 (+ 1) % Row 7 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Request to an object to do something, or compute value} \tn % Row Count 13 (+ 2) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{When calling a method, parameter types are not specified} \tn % Row Count 15 (+ 2) % Row 9 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Parameters of the type specified in the definition must be supplied} \tn % Row Count 17 (+ 2) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Method calls can be used as a statement} \tn % Row Count 18 (+ 1) % Row 11 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Methods that return a value may be used as part of an expression} \tn % Row Count 20 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.4977 cm} x{4.4793 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Arithmetic expressions}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{number literals (e.g., 42) and variables (e.g., x);} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} + & indicate addition; \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} - & subtraction \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} * & multiplication \tn % Row Count 5 (+ 1) % Row 4 \SetRowColor{LightBackground} / & division \tn % Row Count 6 (+ 1) % Row 5 \SetRowColor{white} \% & modulo(indicates remainder of an integer only division) \tn % Row Count 8 (+ 2) % Row 6 \SetRowColor{LightBackground} ( ) & indicate the order in which to do things. \tn % Row Count 10 (+ 2) % Row 7 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{An operation involving two ints results in an int.} \tn % Row Count 12 (+ 2) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{When dividing one int by another, the fractional part of the result is thrown away, e.g., 14 / 5 gives 2 (and 14 \% 5 gives 4).} \tn % Row Count 15 (+ 3) % Row 9 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Any operation involving a double results in a double, e.g., 3.7 + 1 gives 4.7 (int values are automatically converted to double where needed} \tn % Row Count 18 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.4977 cm} x{4.4793 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Boolean expressions}} \tn % Row 0 \SetRowColor{LightBackground} \textless{} & less than \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \textless{}= & less than or equals \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} == & equals \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} \textgreater{} & greater than \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} \textgreater{}= & \textgreater{}= \tn % Row Count 5 (+ 1) % Row 5 \SetRowColor{white} != & not equals \tn % Row Count 6 (+ 1) % Row 6 \SetRowColor{LightBackground} \&\& & "and" true if and only if both operands are true \tn % Row Count 8 (+ 2) % Row 7 \SetRowColor{white} || & "or" true if and only if at least one operand is true \tn % Row Count 10 (+ 2) % Row 8 \SetRowColor{LightBackground} ! & "not" reverses the truth value of its one operand \tn % Row Count 12 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Example: \newline `(x \textgreater{} 0) \&\& !(x \textgreater{} 99)` \newline "x is greater than zero and is not greater than 99"} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.9908 cm} x{2.9862 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Conditional expressions}} \tn % Row 0 \SetRowColor{LightBackground} `condition ? expr1 : expr2` & Becomes expr1 if condition is true, otherwise expr2. \tn % Row Count 3 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Example: \newline `x \textless{} 0 ? -1 : 1` \newline "if x is less than zero, then -1, otherwise 1"} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{String concatenation}} \tn % Row 0 \SetRowColor{LightBackground} You can concatenate (join together) Strings with the + operator & fullName = firstName + " " + lastName; \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} ou can concatenate any value with a String and that value will automatically be turned into a String. & \seqsplit{System.out.println("There} are " + count + " apples."); \tn % Row Count 10 (+ 6) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{0.84609 cm} x{4.13091 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{If statements}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{An if statement lets you choose whether or not to execute one statement, based on a boolean condition.} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Condition {\bf{must}} be boolean.} \tn % Row Count 4 (+ 1) % Row 2 \SetRowColor{LightBackground} \seqsplit{Syntax:} & if (boolean\_condition) statement; \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} \seqsplit{Example:} & `if (x \textless{} 100) x = x + 1;` // adds 1 to x, but only if x is less than 100 \tn % Row Count 9 (+ 3) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{An if statement may have an optional else part, to be executed if the boolean condition is false.} \tn % Row Count 11 (+ 2) % Row 5 \SetRowColor{white} \seqsplit{Syntax:} & if (boolean\_condition) statement else statement \tn % Row Count 13 (+ 2) % Row 6 \SetRowColor{LightBackground} \seqsplit{Example} & `if (x \textgreater{}= 0 \&\& x \textless{} limit) y = x / limit; else System.out.println("x is out of range: " + x);` \tn % Row Count 16 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.4977 cm} p{0.4977 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Compound statements}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Group multiple statements into a single statement by surrounding them with braces, `\{ \}`.} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{there is no semicolon after a compound statement} \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Braces can also be used around a single statement, or no statements at all (to form an "empty" statement).} \tn % Row Count 6 (+ 3) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{It is good style to always use braces in the if part and else part of an if statement, even if they surround only a single statement.} \tn % Row Count 9 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Example: \newline ` if (score \textgreater{} 100) \{` \newline ` score = 100;` \newline ` \seqsplit{System.out.println("score} has been adjusted");` \newline `\}`} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.79632 cm} x{4.18068 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{While Loops}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{A while loop will execute the enclosed statement as long as a boolean condition remains true.} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \seqsplit{Syntax:} & `while (boolean\_condition) statemen` \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{he condition must be boolean.} \tn % Row Count 5 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{If the condition never becomes false, the loop never exits, and the program never stops.} \tn % Row Count 7 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Example: \newline \newline n = 1; \newline while (n \textless{} 4) \{ \newline System.out.println(n + " squared is " + (n * n)); \newline n = n + 1; \newline \} \newline \newline Result: \newline 1 squared is 1 \newline 2 squared is 4 \newline 3 squared is 9} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.59264 cm} x{3.38436 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{for loop}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{The for loop looks complicated, but is very handy.} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} Syntax: & for (initialise ; test ; increment) statement \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{There is no semicolon after the increment.} \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{The initialise part is done first and only once} \tn % Row Count 5 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Then, the test is performed, and, as long as it is true,} \tn % Row Count 7 (+ 2) % Row 5 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{the statement is executed, and} \tn % Row Count 8 (+ 1) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{the increment is executed} \tn % Row Count 9 (+ 1) % Row 7 \SetRowColor{white} Initialise: & define the loop variable with an assignment statement, or with a declaration and initialisation. \tn % Row Count 13 (+ 4) % Row 8 \SetRowColor{LightBackground} Test, or condition: & A boolean condition. \tn % Row Count 15 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Example: \newline \newline Print the numbers 1 through 10 and their squares: \newline for (int i = 1; i \textless{} 11; i++) \{ \newline System.out.println(i + " " + (i {\emph{ i)); \newline \} \newline \newline Print the squares of the first 100 integers, ten per line: \newline for (int i = 1; i \textless{} 101; i++) \{ \newline System.out.print(" " + (i }} i)); \newline if (i \% 10 == 0) \newline System.out.println(); \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.24425 cm} x{3.73275 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{When do you use each loop}} \tn % Row 0 \SetRowColor{LightBackground} for loop & if you know ahead of time how many times you want to go through the loop. \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} while loop & in almost all other cases. \tn % Row Count 4 (+ 1) % Row 2 \SetRowColor{LightBackground} do-while loop & if you must go through the loop at least once before it makes sense to do the test. \tn % Row Count 7 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.4977 cm} p{0.4977 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{break statement}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Inside any loop, the break statement will immediately get you out of the loop.} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{If you are in nested loops, break gets you out of the innermost loop} \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{It doesn't make any sense to break out of a loop unconditionally; you should do it only as the result of an if test} \tn % Row Count 7 (+ 3) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{break should not be the normal way to leave a loop} \tn % Row Count 8 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Use it when necessary, but don't overuse it.} \tn % Row Count 9 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Example: \newline \newline for (int i = 1; i \textless{}= 12; i++) \{ \newline if (badEgg(i)) \newline break; \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.4977 cm} p{0.4977 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{continue statemen}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Inside any loop, the continue statement will jump right before the end of the loop body.} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{In a while or do-while loop, the continue statement will bring you to the test.} \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{In a for loop, the continue statement will bring you to the increment, then to the test} \tn % Row Count 6 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.4977 cm} p{0.4977 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Multiway decisions}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{The if-else statement chooses one of two statements, based on the value of a boolean expression} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{The switch statement chooses one of several statements, based on the value} \tn % Row Count 4 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.4977 cm} p{0.4977 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{switch statement}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{works with the byte, short, char, and int primitive data types} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{works with enumeration types, the String class, and a few special classes that wrap certain primitive types: Byte, Short, Character, and Integer.} \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Notice that colons ( : ) are used as well as semicolons.} \tn % Row Count 7 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{The last statement in every case should be a break;} \tn % Row Count 9 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{The default: case handles every value not otherwise handled.} \tn % Row Count 11 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{public static void printStatus(FilingStatus status) \{ \newline switch (status) \{ \newline case SINGLE: // SINGLE rather than FilingStatus.SINGLE \newline System.out.print("Single filing"); \newline break; \newline case MARRIED: \newline \seqsplit{System.out.print("Married} joint filing"); \newline break; \newline case \seqsplit{MARRIED\_FILING\_SEPARATELY:} \newline \seqsplit{System.out.print("Married} separate filing"); \newline break; \newline default: \newline \seqsplit{System.out.print("Unexpected} case");/ / better: throw an exception if code. needs to be updated to handle new case \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}