Search This Blog

UI/UX Design | Web Development | Mobile Application Development

Whether you need UI/ UX design, web development services, or mobile app development services, Qaabil Digitals will be more than happy to help you. We work with top software developers, designers, and project managers. We tailor our services to suit your exact needs. We closely look at your company, listen to your story, and bring about the best ideas and solutions that can help you drive solid results. We don’t just produce work that works. We produce work that matters. Get a quote now Or Talk to us

Saturday 16 July 2011

The do while loop

Do while:
                As you know if the conditional expression controlling a while loop initially false then the body of the loop will not be executed at all. However, sometimes it is desirable to execute the body of a while loop at least once, even if the conditional expression is false at the beginning. We can say that, there are times while programming with java when you would like to test the conditional expression at the end of the body of loop rather than at the beginning. Fortunately, java supplies a loop that does just that: the do while.

The do while loop always executes its body first (even if the conditional expression is false to begin with) and then checks the conditional expression. It does so because its conditional expression is at the bottom/end of the body of the loop. Its general form is:

do
{
//body of loop
}
while (condition);

Each iteration of this loop first executes body of the loop and then evaluates the conditional expression, controlling the loop. If this condition is true then loop will repeat otherwise, the loop terminates. Remember, a condition must be a Boolean expression.

Friday 15 July 2011

Write a program that read a number and check wheather it is prime or not using do while loop

import java.util.*;
class prime
{
public static void main (String arg[])
{
Scanner in = new Scanner (System.in);
int a,b=2,c=1;
System.out.print ("\n\t Enter number = ");
a=in.nextInt();

do
{
if (a%b==0)
{
c=0;
break;
}
++b;
}while (b>a/2);

if (c==1)
System.out.println ("\n\t Prime ");
else
System.out.println ("\n\t Not prime ");

}
}

Note:
          This program is working just like we have seen a program like this using while loop.

Write a program that prints all numbers from 1 to 100 which are divisible by 9 using do while loop

import java.util.*;
class numbers
{
public static void main (String arg[])
{
Scanner in = new Scanner (System.in);
int a=100;
do
{
if (a%9==0)
System.out.print ("\n\t "+a);
--a;
}while (a>0);

}
}

Note:
        This program is approximately same as that prints even or odd numbers.
Share