package day08;
public class LoopingStatmentDemo {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
System.out.println("The value of i is : " + i);
}
}
}
package day08;
public class LoopingStatmentDemo2 {
public static void main(String[] args) {
int i = 0;
while (i <= 10) {
System.out.println("The value of i is: " + i);
i++;
}
}
}
package day08;
public class LoopingStatmentDemo3 {
public static void main(String[] args) {
int i = 0;
do {
System.out.println("The value of i is:" + i);
i++;
} while (i <= 10);
}
}
/**
* Class Created for demoing additional conditions and truthTable
* @author BharathwajSoundarara
*
*/
class Rat {
int roomInBelly = 5;
public void eatCheese(int bitesOfCheese) {
// Keep eating as long as there is roomInBelly and some cheese
// is left
while (bitesOfCheese > 0 && roomInBelly > 0) {
bitesOfCheese--;
roomInBelly--;
}
System.out.println(bitesOfCheese + " pieces of cheese left");
}
public void showRoomInBelly() {
System.out.println("Room in belly: " + roomInBelly);
}
}
/**
* Class for creating an instance of a Rat Class
* @author BharathwajSoundarara
*
*/
public class RatDemo {
public static void main(String[] args) {
Rat rat = new Rat();
rat.roomInBelly = 5;
rat.eatCheese(10);
// Calling Belly Capacity
rat.showRoomInBelly();
}
}
int n = 35;
int result = 1;
while (n > 0)
{
int d = n % 10;
result *= d;
n /= 10;
}
System.out.println(result);
int count = 0;
/* missing loop header */
{
if (count % 2 == 0)
{
System.out.println(count);
}
count++;
}
for loop.while loop.do-while loop.for loop: public class Test1
{
public static void main(String[] args)
{
int x = 5;
while (x > 0)
{
System.out.println(x);
x = x - 1;
}
}
}
7.Rewrite using while loop:
public class Test1
{
public static void main(String[] args)
{
for (int x = 1; x <= 10; x++)
System.out.println(x);
}
}
8.The following code should print the values from 1 to 10 (inclusive) but has errors. Fix the errors so that the code works as intended.
public class Test1
{
public static void main(String[] args)
{
int x = 1;
while (x < 10)
{
System.out.println(x);
}
}
}
Additional Practice: Refer here.