• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

For loop running on Booleans

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Please tell me that how this code is working.




the method returns boolean value, if do directly replace it, then it fails.
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

This example is working because the following program also works

int i = 0;
for( ; i<2; )
{
System.out.println(i);
i++;
}

OutPut :: 0
1

Even this will work:

For(;
{
System.out.println("Hi");
}

Output :: It will act as an infinite loop.

In for loop, it is not necessery to have initialization, boolean expression and condition expression.

Here i has been initialised before the for loop starts.

Here first the initialisation part will be checked so it will print 'A'.
Then boolean expression will be check so it will print 'B'.
Then since the boolean expression returns true, it will go inside the loop where i will get incremented and 'D' will be printed. Then the increment expresion will be done so it will print 'C'. For loop will continue to print untill the boolean expression becomes false.(when i becomes > 2).

I hope it will help you.

Regards,
Satabdi
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See also this:
http://www.coderanch.com/t/263755/java-programmer-SCJP/certification/loop



Bu.
 
Ranch Hand
Posts: 329
Oracle Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
However, the following variation won't compile:



Note that both expressions: "i < 2000" and "foo('C')" evaluate to a boolean.
Can someone explain the difference between both, please?
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Delta {
static boolean foo(char c) {
System.out.print(c);
return true;
}
static void foo2(char c) {
System.out.print(c);
}
static Object foo3(char c) {
System.out.print(c);
return null;
}
public static void main( String[] argv ) {
int i =0;
Delta d=new Delta();

for ( foo('A'); foo('B')&&(i<2); foo2('C'),foo3('C'),Math.ceil(5.4)){
i++ ;

foo('D');
}
}
}

The only difference is That the iteration part is just an execution part
when you compile
1.the compiler check whether the method argument list are correct
(not return signatures)(example above)
2.IF a operator is encountered it is evaluated
i<2000 is very different from a++ (or any iteration)
SO it wont compile

Just make it simple
for (intiliaze , evaulation(checks for return type), iteration(just execute not evaulate)


for ( foo('A'); foo2('B') // this wont compile becoz
you can figure this out
 
This tiny ad is wafer thin:
professionally read, modify and write PDF files from Java
https://products.aspose.com/pdf/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!