23.11.2014 Views

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

if (a[i][j] == 0) {<br />

foundFlag = true;<br />

break zeroSearch;<br />

}<br />

}<br />

}<br />

}<br />

return foundFlag;<br />

The example above also uses arrays, which are covered <strong>in</strong> Section 3.1.<br />

The cont<strong>in</strong>ue Statement<br />

The other statement to explicitly change the flow of control <strong>in</strong> a <strong>Java</strong> program is<br />

the cont<strong>in</strong>ue statement, which has the follow<strong>in</strong>g syntax:<br />

cont<strong>in</strong>ue label;<br />

where label is an optional <strong>Java</strong> identifier that is used to label a loop. As<br />

mentioned above, there are no explicit "go to" statements <strong>in</strong> <strong>Java</strong>. Likewise, the<br />

cont<strong>in</strong>ue statement can only be used <strong>in</strong>side loops (for, while, <strong>and</strong> dowhile).<br />

The cont<strong>in</strong>ue statement causes the execution to skip over the<br />

rema<strong>in</strong><strong>in</strong>g steps of the loop body <strong>in</strong> the current iteration (but then cont<strong>in</strong>ue the<br />

loop if its condition is satisfied).<br />

1.5 Arrays<br />

A common programm<strong>in</strong>g task is to keep track of a numbered group of related objects.<br />

For example, we may want a video game to keep track of the top ten scores for that<br />

game. Rather than use ten different variables for this task, we would prefer to use a<br />

s<strong>in</strong>gle name for the group <strong>and</strong> use <strong>in</strong>dex numbers to refer to the high scores <strong>in</strong> that<br />

group. Similarly, we may want a medical <strong>in</strong>formation system to keep track of the<br />

patients currently assigned to beds <strong>in</strong> a certa<strong>in</strong> hospital. Aga<strong>in</strong>, we would rather not<br />

have to <strong>in</strong>troduce 200 variables <strong>in</strong> our program just because the hospital has 200 beds.<br />

In such cases, we can save programm<strong>in</strong>g effort by us<strong>in</strong>g an array, which is a<br />

numbered collection of variables all of the same type. Each variable, or cell, <strong>in</strong> an<br />

array has an <strong>in</strong>dex, which uniquely refers to the value stored <strong>in</strong> that cell. The cells of<br />

59

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!