<< Chapter < Page Chapter >> Page >

Some important things to notice about this algorithm:

  • The empty case must be checked explicitly — no polymorphism to help you out here!
  • The desired result index cannot be declared inside the for loop because otherwise it won't be visible to the outside world.
  • Be careful about using the minIdx value if the array was indeed empty--it's an invalid value! It can't be set to a valid value because otherwise you can't tell the difference between a value that was never set and one that was.
  • The for loop has two initialization statements separated by a comma.
  • The loop does work correctly if the array only has one element, but only because the termination check is done before the loop body.
  • Notice that to prove that this algorithm works properly, one must make separate arguments about the empty case, the one element case and the n-element case. Contrast this to the much simpler list algorithm that only needs an empty and non-empty cases.

For convenience, Java 5.0 now offers a compact syntax used for traversing all the elements of an array or of anything that subclasses type Iterable :

MyType[] myArray; // array is initialized with data somewherefor(MyType x: myArray){ // code involving x, i.e. each element in the array}

It is important to remember that this syntax is used when one wants to process every element in an array (or an Iterable object) independent of order of processing because Java does not guarantee a traversal order.

Let's look at an algorithm where we might not want to process the entire array:

// Find the first index of a given value in an array int idx = -1; // initialize the index to an invalid value.for(int j=0; j<myArray.length; j++) { //no initialization ; end index at length-1; //increment index every time the loop is processed.if(desiredValue == myArray[j]) { // found match!idx = j; // save the index. break; // break out of the loop.} }

Notes:

  • The only way you can tell if the desired value was actually found or not is if the value of idx is -1 or not. Thus the value of idx must be checked before it is ever used.
  • The resultant idx variable cannot be used as the index inside the loop because one would not be able to tell if the desired value was found or not unless one also checked the length of the array. This is because if the desired value was never found, idx at the end of the loop would equal the length of the array, which is only an invalid value if you already know the length of the array.
  • The break statement stops the loop right away and execution resumes at the point right after end of the loop.

There is a counterpart to break called continue that causes the loop to immediately progress to the beginning of the next iteration. It is used much more rarely than break , usually in situations where there are convoluted and deeply nested if - else statements.

Can you see that the price of the compact syntax of for loops is a clear understandability of the process?

While loops

for loops are actually a specialized version of while loops. while loops have no special provisions for initialization or loop incrementing, just the termination condition.

Questions & Answers

what is phylogeny
Odigie Reply
evolutionary history and relationship of an organism or group of organisms
AI-Robot
ok
Deng
what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Principles of object-oriented programming. OpenStax CNX. May 10, 2013 Download for free at http://legacy.cnx.org/content/col10213/1.37
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Principles of object-oriented programming' conversation and receive update notifications?

Ask