1. Computing & Technology

Discuss in my forum

Literal Loop

By , About.com Guide

Literal Loop Solution

The task was to write a program that used hexadecimal and octal literals in the scope of a for loop declaration. The program needed to have a for loop iterate from the hexadecimal 0xEF to the octal 0647, a total of 185 times.

Here's my version:

 public class HexOctalLoop {
 
   public static void main(String[] args) {
 
     int count = 0;
     for (int i = 0x0EF; i <= 0647; i++)
     {
       count++;
     }
     System.out.println("The loop looped " + count + " times.");
   }
 } 

Note: This loop is really the same as giving the int variable i the starting value of 239 (the denary value of 0xEF) and iterating until it equals the value of 423 (the denary value of 0647).

©2012 About.com. All rights reserved.

A part of The New York Times Company.