For Loop - JAVA
It is an another control statement. all the statement will execute repitatively until the condition became false. it is an entry control loop like while.
Syntax:-
for(initialization; condition ; Updation)
{
Statements;
}
WRITE A PROGRAM TO PRINT THE NUMBERS FROM 15 TO 1.
class Num
{
public static void main(String args[])
{
System.out.println(“Numbers: “);
for(int n=15;n>=1;n--)
{
System.out.println(n);
}
}
}
Continue:-
It is used to continue the loop.
Syntax:-
To terminate the control structure.
WRITE A PROGRAM TO PRINT THE EVEN NUMBERS FROM 0 TO 10 BY MISSING 4,6,8.
class Even
{
public static void main(String args[])
{
int i;
for(i=2;i<=15;i+=2)
{
if(i==4||i==6||i==8)
{
continue;
}
System.out.println(i);
}
}
}
WRITE A PROGRAM TO PRINT THE NUMBRS FROM 50 TO 100 TERMINATE THE LOOP WHEN THE NUMBERS DIVISIBLE BY 9 USING BREAK.
class Divisible
{
public static void main(String args[])
{
for(int n=50;n<=100;n++)
{
if(n%9==0)
{
break;
}
System.out.println(n);
}
}
}
Nested For Loop
WRITE A PROGRAM TO PRINT THE TABLES FROM 1 TO 3.
class Table
{
public static void main(String args[])
{
int c;
for(int n=1;n<=3;n++)
{
for(int b=1;b<=5;b++)
{
c=n*b;
System.out.println(n+"*"+b+"="+c);
}
System.out.println("");
}
}
}
WRITE A PROGRAM TO PRINT THE NUMBERS IN THE FORM.
1
1 2
1 2 3
12 3 4
1 2 3 4 5
class Format1
{
public static void main(String args[])
{
for(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(j);
}
System.out.println();
}
}
}
WRITE A PROGRAM TO PRINT THE NUMBERS IN THE FORM.
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
class Format2
{
public static void main(String args[])
{
for(int i=5;i>=1;i--)
{
for(int j=i;j>=1;j--)
{
System.out.print(j);
}
System.out.println();
}
}
}
For
More Explanation
&
Online Classes
More Explanation
&
Online Classes
Contact Us:
+919885348743
+919885348743