473,504 Members | 13,601 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Related to For loop in java

1 New Member
Please see the below code in java . I'm not able to understand it .Please explain clearly with the output ..

class JMM112 {
public static void main (String[] args) {
int j = 0;
for (int i = 0; i++ < 2;) do
System.out.print(i);
while (j++ < 2);
}
}

I'm waiting for the reply . Please respond quickly ...
Aug 30 '07 #1
7 1489
JosAH
11,448 Recognized Expert MVP
Please see the below code in java . I'm not able to understand it .Please explain clearly with the output ..

class JMM112 {
public static void main (String[] args) {
int j = 0;
for (int i = 0; i++ < 2;) do
System.out.print(i);
while (j++ < 2);
}
}

I'm waiting for the reply . Please respond quickly ...
Greetings, you should've asked this question in the Java Forum; they're able to
help you overthere. Here's a tip: change that print statement to:

Expand|Select|Wrap|Line Numbers
  1. System.out.println("i: "+i+" j: "+j);
  2.  
... and run your program; you'll see what's happening exactly.

kind regards,

Jos
Aug 30 '07 #2
Ganon11
3,652 Recognized Expert Specialist
Thread has been moved to the Java forum.
Aug 30 '07 #3
Nepomuk
3,112 Recognized Expert Specialist
Expand|Select|Wrap|Line Numbers
  1. class JMM112 {
  2.   public static void main (String[] args) {
  3.     int j = 0;
  4.     for (int i = 0; i++ < 2;) do
  5.       System.out.print(i);
  6.      while (j++ < 2);
  7.  }
  8. }
  9.  
Hello Sonal Kumar! Welcome to TSDN!

OK, let's go through it line after line.

Line 1: class JMM112 {
With this line you do two things:
  1. You define a class named "JMM112"
  2. You open the declaration of that class (with the bracelet)
Line 2: public static void main (String[] args) {[/i]
This line defines a function/method (void) named main. This method will be accessible for any other class (public) and without creating an object from the class (static). Also, this method has parameters: an Array (which is a type of Collection) of Strings (which are any string of characters, e.g. "Hello").
Then you open the declaration of the method (with the bracelet).
The main method has a very special meaning: It is the method, which is called, when ever you start your program. (The main method - sound's reasonable, doesn't it?) Anything, that your program should do, has to be called (directly or indirectly) in this method.

Line 3: int j = 0;
This Line defines and declares a new variable called j with the type integer and the value 0. The semicolon closes this command.

Line 4: for (int i = 0; i++ < 2;) do
This line is a bit complicated, as it does several things.
  1. You start a so called for-loop.This loop defines a variable i of the type integer and with the value 0. The loop should run, as long as i < 2 is valid and every time it finishes a run, it should increment i (= add 1 to it). This is stated in the expression i++ which is, for the moment, identical to i = i + 1; or ++i. Then comes a semicolon and an empty expression (there could be something behind the semicolon and before the bracet).
    The normal way to declare this loop would be
    Expand|Select|Wrap|Line Numbers
    1. for (int i = 0; i < 2; i++)
    2.  
    however both declarations do the same.
  2. You start a so called do-while-loop. This loop will do something, then check, if the condition under which it should do so is still fulfilled and if so will repeat that.
Line 5: System.out.println(i);
For the first time in this short program, you call a function. System.out.println will print something to the console (e.g. commandline or console). In this case, it will print the current value of the variable i.

Line 6: while (j++ < 2);
This is the second half of the do-while-loop. It checks, if j < 2 and if so repeats whatever came after do, after it has incremented the variable j.

After this line, the brackets, which were opened previously are closed again. Every bracket, which you open within your program MUST be closed at some point.

OK, if you have more questions or have problems understanding something, just ask again! :-)

Greetings,
Nepomuk
Aug 30 '07 #4
r035198x
13,262 MVP
Hello Sonal Kumar! Welcome to TSDN!

OK, let's go through it line after line.

Line 1: class JMM112 {
With this line you do two things:

  1. You define a class named "JMM112"
  2. You open the declaration of that class (with the bracelet)
Line 2: public static void main (String[] args) {[/i]
This line defines a function/method (void) named main. This method will be accessible for any other class (public) and without creating an object from the class (static). Also, this method has parameters: an Array (which is a type of Collection) of Strings (which are any string of characters, e.g. "Hello").
Then you open the declaration of the method (with the bracelet).
The main method has a very special meaning: It is the method, which is called, when ever you start your program. (The main method - sound's reasonable, doesn't it?) Anything, that your program should do, has to be called (directly or indirectly) in this method.

Line 3: int j = 0;
This Line defines and declares a new variable called j with the type integer and the value 0. The semicolon closes this command.

Line 4: for (int i = 0; i++ < 2;) do
This line is a bit complicated, as it does several things.
  1. You start a so called for-loop.This loop defines a variable i of the type integer and with the value 0. The loop should run, as long as i < 2 is valid and every time it finishes a run, it should increment i (= add 1 to it). This is stated in the expression i++ which is, for the moment, identical to i = i + 1; or ++i. Then comes a semicolon and an empty expression (there could be something behind the semicolon and before the bracet).



    The normal way to declare this loop would be



    Expand|Select|Wrap|Line Numbers
    1.  
    2.  
    3.  
    4. for (int i = 0; i < 2; i++)
    5.  
    6.  
    7.  
    8.  
    however both declarations do the same.
  2. You start a so called do-while-loop. This loop will do something, then check, if the condition under which it should do so is still fulfilled and if so will repeat that.
Line 5: System.out.println(i);
For the first time in this short program, you call a function. System.out.println will print something to the console (e.g. commandline or console). In this case, it will print the current value of the variable i.

Line 6: while (j++ < 2);
This is the second half of the do-while-loop. It checks, if j < 2 and if so repeats whatever came after do, after it has incremented the variable j.

After this line, the brackets, which were opened previously are closed again. Every bracket, which you open within your program MUST be closed at some point.

OK, if you have more questions or have problems understanding something, just ask again! :-)

Greetings,
Nepomuk
Perhaps you should have given them a bit more time to try and work it out themselves.
Aug 30 '07 #5
Nepomuk
3,112 Recognized Expert Specialist
Perhaps you should have given them a bit more time to try and work it out themselves.
Well, maybe. On the other hand, I did give a very detailed description of what it does and therefore I hope, that it will be really understood.
Aug 30 '07 #6
JosAH
11,448 Recognized Expert MVP
Well, maybe. On the other hand, I did give a very detailed description of what it does and therefore I hope, that it will be really understood.
I consider 'very detailed' an understatement; you used a tunneling microscope
and disected every single bit to tiny pieces where even those quarcks can't
see the little pieces anymore.

kind regards,

Jos (<--- chocolate and vanilla, I don't want the strawberries ;-)
Aug 30 '07 #7
Nepomuk
3,112 Recognized Expert Specialist
I consider 'very detailed' an understatement; you used a tunneling microscope
and disected every single bit to tiny pieces where even those quarcks can't
see the little pieces anymore.

kind regards,

Jos (<--- chocolate and vanilla, I don't want the strawberries ;-)
In that case, I'll just say: "It's art! :-D
Aug 31 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

18
1716
by: Manohar S | last post by:
It is a problem related to printf, and buffering to the printf statements Look through this program. main() { int pid; int loop,max=3; for(loop = 0; loop <max;loop++) { pid = fork();
9
4158
by: JS | last post by:
#include <stdio.h> main(){ int c, i, nwhite, nother; int ndigit; nwhite = nother = 0; for (i = 0; i < 10; ++i)
2
1476
by: Jim in Arizona | last post by:
Usually, If i need special formatting, I don't use the datagrid control and use a loop that processes a table for each record read from the database (as in classic asp) like so: ...
17
4320
by: Allerdyce.John | last post by:
Hi, I am trying to compare the amount of work between using STL algorithm VS a plain Java loop. Let's say the class Rect has 2 attributes: area, and areaPerCent. In Java, I just write a...
14
1556
by: ToddLMorgan | last post by:
Summary: How should multiple (related) projects be arranged (structured) and configured so that the following is possible: o Sharing common code (one of the projects would be a "common" project...
5
3987
by: hprYeV | last post by:
I have done a reasonable amount of programming in C++, but the other day I was talking to someone after a lecture in a course on Java who said that they had not been used to the syntax of the Java...
1
1890
by: saytri | last post by:
I have a problem with this code. I'm, doing a quiz, and the questions are stored in a binary file. My problem is how am i going to compare the answers entered by the user to the correct answers. The...
1
3025
by: JavaJon | last post by:
Hello, I'm Jon. I've recently picked up Java after using a "gimmick" programming language called GML ( Game Maker Language ). I've read a lot of tutorials and even a Java for Dummies *.pdf book....
4
2529
by: crochunter | last post by:
Hi, I want to read values from a text files from specified fields and use them as values to fill my methods inside the paintComponent() method. I am using for loop to do that but not able to do it...
0
7213
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7098
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7366
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7017
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
4698
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3187
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3176
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1526
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
754
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.