Vyoms OneStopTesting.com - Testing EBooks, Tutorials, Articles, Jobs, Training Institutes etc.
OneStopGate.com - Gate EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
OneStopMBA.com - MBA EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
OneStopIAS.com - IAS EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
OneStopSAP.com - SAP EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
OneStopGRE.com - of GRE EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
Bookmark and Share Rss Feeds

Is Java driving you loopy? | Articles | Recent Articles | News Article | Interesting Articles | Technology Articles | Articles On Education | Articles On Corporate | Company Articles | College Articles | Articles on Recession
Sponsored Ads
Hot Jobs
Fresher Jobs
Experienced Jobs
Government Jobs
Walkin Jobs
Placement Section
Company Profiles
Interview Questions
Placement Papers
Resources @ VYOMS
Companies In India
Consultants In India
Colleges In India
Exams In India
Latest Results
Notifications In India
Call Centers In India
Training Institutes In India
Job Communities In India
Courses In India
Jobs by Keyskills
Jobs by Functional Areas
Learn @ VYOMS
GATE Preparation
GRE Preparation
GMAT Preparation
IAS Preparation
SAP Preparation
Testing Preparation
MBA Preparation
News @ VYOMS
Freshers News
Job Articles
Latest News
India News Network
Interview Ebook
Get 30,000+ Interview Questions & Answers in an eBook.
Interview Success Kit - Get Success in Job Interviews
  • 30,000+ Interview Questions
  • Most Questions Answered
  • 5 FREE Bonuses
  • Free Upgrades

VYOMS TOP EMPLOYERS

Wipro Technologies
Tata Consultancy Services
Accenture
IBM
Satyam
Genpact
Cognizant Technologies

Home » Articles » Is Java driving you loopy?

Is Java driving you loopy?








Article Posted On Date : Thursday, March 25, 2010


Is Java driving you loopy?
Advertisements

There are three basic building blocks of programming :-

  • sequence

  • selection

  • iteration

Most programmers will be familiar with sequence. A sequence of programming statements are executed one after the other. For example, the following two lines can be executed sequentially

System.out.println ("hello");
System.out.println ("goodbye");

Selection is also fairly simple. By using if / switch statements, you can control the execution flow of code. Most programmers are already familiar with the concept behind if statements - it is virtually impossible to write any application without using some form of selection.

The final concept, iteration, is the focus of this article. This one is a little more tricky, as there are quite a few different times of iteration statements in Java. The basic concept behind iteration, is that a sequence of statements is repeated until a certain condition is met. When this condition is met, the iteration terminates, and the loop is over.

'for' loops

The simplest type of loop uses the for statement, to iterate through a sequence of statements. Usually, a for loop will use a counter, with a precise starting point and a precise ending point.

// for loop, from 1 to 5
for (int i = 1; i <= 5; i++)
{
System.out.println ("count : " + i);
}

There are several components to a for loop

for ( init    condition   ;   statement ) 

A check to see if the condition equates to true is made at the end of the for loop. When the condition fails to be met, the loop will terminate. In our previous example, the loop terminates when our counter (i) is not less than or equal to a value of five.

It is also worth noting that each of the components of a for loop is entirely optional. For example, our loop could be rewritten as the following: -

// This may be more useful, as the variable
// i is created outside the scope of the
// loop.
int i = 1;
// for loop, from i to 5
for (; i <= 5; i++)
{
System.out.println ("count : " + i);
}

'while' loops

A while loop differs from the for loop, in that it only contains a condition, and that the condition is tested at the beginning of the loop. This means that if the condition evaluates to false, it will not be executed at all. We call this a pre-tested loop, because the test is made before executing the sequence of statements contained within.

int i = 1;

while (i <= 5)
{
System.out.println ("count : " + i);
i++;
}

'do...while' loops

A variation on the while loop is the post-tested version. Two keywords are used for this loop, the do and the while keywords.

int i = 1;

do
{
System.out.println ("count : " + i);
i++;
}
while (i <= 5)

There is no difference between a while, and a do...while loop other than pre-testing and post-testing.

Terminating loops abruptly

Sometimes it is necessary to stop a loop before its terminating condition is reached. For example, a loop that searched through an array for a particular entry should not continue once that entry is found - to do so would only waste CPU time and make for a slower program. Other times, you may want to skip ahead to the next iteration of the loop, rather than performing additional processing. Java provides two keywords for this purpose : break and continue

break

The break keyword is used to terminate a loop early. Consider the following example, which searches through an array for a particular value.

// Check to see if "yes" string is stored
boolean foundYes = false;

for (int i = 0; i < array.length; i++)
{
if (array[i].equals("yes"))
{
foundYes = true;
break;
}
}

continue

Unlike the break keyword, continue does not terminate a loop. Rather, it skips to the next iteration of the loop, and stops executing any further statements in this iteration. This allows us to bypass the rest of the statements in the current sequence, without stopping the next iteration through the loop.

for (int i = 0; i < array.length; i++)
{
if (array[i] == null)
// skip this one, goto next loop
continue;
else
{
// do something with array[i]
.......
}
}

While this example was unnecessary, since we had an if statement to separate program flow, there are some complex algorithms where continue and break are extremely useful.

Summary

Loop don't need to be difficult, providing you keep in minds these simple concepts: -

  • loops are for repetition of statements

  • some loops are pre-tested (for, while), others are post-tested (do..while)

  • when you have a clearly defined start and finish, a for loop is preferable

  • when you have a more complex termination condition, while loops are preferable






Sponsored Ads



Interview Questions
HR Interview Questions
Testing Interview Questions
SAP Interview Questions
Business Intelligence Interview Questions
Call Center Interview Questions

Databases

Clipper Interview Questions
DBA Interview Questions
Firebird Interview Questions
Hierarchical Interview Questions
Informix Interview Questions
Microsoft Access Interview Questions
MS SqlServer Interview Questions
MYSQL Interview Questions
Network Interview Questions
Object Relational Interview Questions
PL/SQL Interview Questions
PostgreSQL Interview Questions
Progress Interview Questions
Relational Interview Questions
SQL Interview Questions
SQL Server Interview Questions
Stored Procedures Interview Questions
Sybase Interview Questions
Teradata Interview Questions

Microsof Technologies

.Net Database Interview Questions
.Net Deployement Interview Questions
ADO.NET Interview Questions
ADO.NET 2.0 Interview Questions
Architecture Interview Questions
ASP Interview Questions
ASP.NET Interview Questions
ASP.NET 2.0 Interview Questions
C# Interview Questions
Csharp Interview Questions
DataGrid Interview Questions
DotNet Interview Questions
Microsoft Basics Interview Questions
Microsoft.NET Interview Questions
Microsoft.NET 2.0 Interview Questions
Share Point Interview Questions
Silverlight Interview Questions
VB.NET Interview Questions
VC++ Interview Questions
Visual Basic Interview Questions

Java / J2EE

Applet Interview Questions
Core Java Interview Questions
Eclipse Interview Questions
EJB Interview Questions
Hibernate Interview Questions
J2ME Interview Questions
J2SE Interview Questions
Java Interview Questions
Java Beans Interview Questions
Java Patterns Interview Questions
Java Security Interview Questions
Java Swing Interview Questions
JBOSS Interview Questions
JDBC Interview Questions
JMS Interview Questions
JSF Interview Questions
JSP Interview Questions
RMI Interview Questions
Servlet Interview Questions
Socket Programming Interview Questions
Springs Interview Questions
Struts Interview Questions
Web Sphere Interview Questions

Programming Languages

C Interview Questions
C++ Interview Questions
CGI Interview Questions
Delphi Interview Questions
Fortran Interview Questions
ILU Interview Questions
LISP Interview Questions
Pascal Interview Questions
Perl Interview Questions
PHP Interview Questions
Ruby Interview Questions
Signature Interview Questions
UML Interview Questions
VBA Interview Questions
Windows Interview Questions
Mainframe Interview Questions


Copyright © 2001-2024 Vyoms.com. All Rights Reserved. Home | About Us | Advertise With Vyoms.com | Jobs | Contact Us | Feedback | Link to Us | Privacy Policy | Terms & Conditions
Placement Papers | Get Your Free Website | IAS Preparation | C++ Interview Questions | C Interview Questions | Report a Bug | Romantic Shayari | CAT 2024

Fresher Jobs | Experienced Jobs | Government Jobs | Walkin Jobs | Company Profiles | Interview Questions | Placement Papers | Companies In India | Consultants In India | Colleges In India | Exams In India | Latest Results | Notifications In India | Call Centers In India | Training Institutes In India | Job Communities In India | Courses In India | Jobs by Keyskills | Jobs by Functional Areas

Testing Articles | Testing Books | Testing Certifications | Testing FAQs | Testing Downloads | Testing Interview Questions | Testing Jobs | Testing Training Institutes

Gate Articles | Gate Books | Gate Colleges | Gate Downloads | Gate Faqs | Gate Jobs | Gate News | Gate Sample Papers | Gate Training Institutes

MBA Articles | MBA Books | MBA Case Studies | MBA Business Schools | MBA Current Affairs | MBA Downloads | MBA Events | MBA Notifications | MBA FAQs | MBA Jobs
MBA Job Consultants | MBA News | MBA Results | MBA Courses | MBA Sample Papers | MBA Interview Questions | MBA Training Institutes

GRE Articles | GRE Books | GRE Colleges | GRE Downloads | GRE Events | GRE FAQs | GRE News | GRE Training Institutes | GRE Sample Papers

IAS Articles | IAS Books | IAS Current Affairs | IAS Downloads | IAS Events | IAS FAQs | IAS News | IAS Notifications | IAS UPSC Jobs | IAS Previous Question Papers
IAS Results | IAS Sample Papers | IAS Interview Questions | IAS Training Institutes | IAS Toppers Interview

SAP Articles | SAP Books | SAP Certifications | SAP Companies | SAP Study Materials | SAP Events | SAP FAQs | SAP Jobs | SAP Job Consultants
SAP Links | SAP News | SAP Sample Papers | SAP Interview Questions | SAP Training Institutes |


Copyright ©2001-2024 Vyoms.com, All Rights Reserved.
Disclaimer: VYOMS.com has taken all reasonable steps to ensure that information on this site is authentic. Applicants are advised to research bonafides of advertisers independently. VYOMS.com shall not have any responsibility in this regard.