declare c as binary-long = 0
perform 10 times
display "Again and "
end-perform
*>Pre-test loops:
perform until c >= 10
add 1 to c
end-perform
perform varying c from 2 by 2 until c > 10
display c
end-perform
perform varying c2 as binary-long from 2 by 2 until c2 > 10
display c2
end-perform
*>Post-test loops:
set c = 0
perform with test after until c >= 10
add 1 to c
end-perform
*> Varying
*>Array or collection looping
declare names as string occurs any
set content of names to ("Fred" "Sue" "Barney")
perform varying s as string through names
display s
end-perform
*>Breaking out of loops:
declare i as binary-long = 0
perform until exit
if i = 5
exit perform
end-if
display i
add 1 to i
end-perform
*>Continue to next iteration:
set i = 0
perform varying i from 0 by 1 until i >= 5
if i < 4
exit perform cycle
end-if
display i *>Only prints 4
end-perform
|
public class loops
{
public static void main(String[] args)
{
int c = 0 ;
// Java has no direct equivalent to perform n times
// pre test loops
// "while" means condition inverted from COBOL's "until"
while ( c < 10);
{
c++;
}
for (int c2 = 2 ; c2 > 10 ; c2 += 2)
{
System.out.println(c2);
}
// Post test loops
c = 0 ;
do
{
c++;
}
while (c < 10 );
// looping through arrays or lists
String names[] = {"Fred", "Sue", "Barney"};
for (String s : names)
{
System.out.println(s);
}
// break out of loops
int i = 0;
while (true)
{
if (i == 5)
break;
System.out.println(i);
i++;
}
// Continue to next iteration:
for (i = 0; i < 5 ; i++)
{
if (i < 4)
continue;
System.out.println(i);
}
}
}
|