java loops
Java basic se basic loops Program
Basic
Program No:- 1
class Demo{
public static void main(String args[]){
for(int i=0 ;i<=10 ;i++)
{
System.out.println(i);
}
}
}
//output
// C:\java>javac Test.java
// C:\java>java Demo
// 0
// 1
// 2
// 3
// 4
// 5
// 6
// 7
// 8
// 9
// 10
Program NO :-2
Nested Loops
Pyramid
Program no:- 3
class PyramidEx{
public static void main(String[] args)
{
for(int i=1;i<=5;i++){
for(int j=1;j<=i;j++){
System.out.print("* ");
}
System.out.println();//new line
}
}
}
//Output
// C:\java>javac Test.java
// C:\java>java Demo
// *
// * *
// * * *
// * * * *
// * * * * *
Program no :- 4 PyramidEx2
class PyramidEx2 { public static void main(String[] args) { int term=6; for(int i=1;i<=term;i++){ for(int j=term;j>=i;j--){ System.out.print("* "); } System.out.println();//new line } } }
//output
// C:\java>javac Test.java
// C:\java>java PyramidEx2// * * * * * *// * * * * *// * * * *// * * *// * *// *
class PyramidEx2 {
public static void main(String[] args) {
int term=6;
for(int i=1;i<=term;i++){
for(int j=term;j>=i;j--){
System.out.print("* ");
}
System.out.println();//new line
}
}
}
//output
// C:\java>javac Test.java
// C:\java>java PyramidEx2
// * * * * * *
// * * * * *
// * * * *
// * * *
// * *
// *
Program No :-5
For each loops
//Java For-each loop example which prints the
//elements of the array
public class ForEachEx {
public static void main(String[] args) {
//Declaring an array
int arr[]={12,23,44,56,78};
//Printing array using for-each loop
for(int i:arr){
System.out.println(i);
}
}
}
//Output
// C:\java>javac Test.java
// C:\java>java ForEachEx
// 12
// 23
// 44
// 56
// 78
Program No :-6
Labeled For Example 1
//Java For-each loop example which prints the
//elements of the array
class Demo {
public static void main(String[] args) {
aa:
for(int i=1;i<=3;i++){
bb:
for(int j=1;j<=3;j++){
if(i==2&&j==2){
break aa;
}
System.out.println(i+" "+i);
}
}
}
}
//Output
// C:\java>javac Test.java
// C:\java>java Demo
// 1 1
// 1 1
// 1 1
// 2 2
Program NO :- 7
Labeled For Example 2
//Java For-each loop example which prints the
//elements of the array
class Demo {
public static void main(String[] args) {
aa:
for(int i=1;i<=3;i++){
bb:
for(int j=1;j<=3;j++){
if(i==2&&j==2){
break bb;
}
System.out.println(i+" "+i);
}
}
}
}
//Output
// C:\java>javac Test.java
// C:\java>java Demo
// 1 1
// 1 1
// 1 1
// 2 2
// 3 3
// 3 3
// 3 3
-----------------------------------------------------------------------------------------------------------
Q1.What is the output of below program?
class Demo {
public static void main(String[] args) {
for(;;)
for(;;)
System.out.println("Hello..");
}
}
//Output
//C:\java>javac Test.java (compile successful)
//C:\java>java Demo (Run successful)
But output is (Hello is printed infinite times)..
Q2:What is output of below program?
class Demo {
public static void main(String[] args) {
for(;;);
for(;;);
System.out.println("Hello..");
}
}
//output
//Nothing is printed
Q3:-What is the output of below program?
class Demo {
public static void main(String[] args) {
for(int i=0,i<5,i++)
{
System.out.println("Hello..");
}
}
}
//output
// compilar error
Q4:-What is output of below program?
class Demo {
public static void main(String[] args) {
for(int i=0,i<5,++i++)
{
System.out.println("Hello..");
}
}
}
//output
//compilar error
Q5:-What is output of below program?
class Demo {
public static void main(String[] args) {
for(int i=0,j=0;i<5;i++)
{
System.out.println(i+" "+j);
}
}
}
//output
//C:\java>javac Test.java
//C:\java>java Demo
//0 0
//1 0
//2 0
//3 0
//4 0
//C:\java>
Q6:-What is output of below program?
class Demo {
public static void main(String[] args) {
int i,j,k,count;
count=0;
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
count++;
}
}
System.out.println(count);
}
}
//output
//C:\java>javac Test.java
//C:\java>java Demo
//25
//C:\java>
class Demo {
public static void main(String[] args) {
int i,j,count;
count=0;
for(j=0;j<5;j++)
{
count++;
}
System.out.println(count);
}
}
// output
// C:\java>javac Test.java
// C:\java>java Demo
// 5
// C:\java>
Q6:-What is output of below program?
class Demo {
public static void main(String[] args) {
int j;
for(j=0;j<5;j++);
{
System.out.println("java");
}
}
}
// output
// C:\java>javac Test.java
// C:\java>java Demo
// java
// C:\java>
(ans:-java is printed 1 times)
Hint:
You might be thinking that it should print 5 times but it is printing 1 time only because for loop is terminated by ; which means for loop will run 5 times but printf() is not part of that loop.
You might be thinking that it should print 5 times but it is printing 1 time only because for loop is terminated by ; which means for loop will run 5 times but printf() is not part of that loop.
Q7:- What is output of below program?
class Demo {
public static void main(String[] args) {
int i,j,k,count;
count=0;
for(i=0;i<5;i++);
{
for(j=0;j<5;j++);
{
count++;
}
}
System.out.println(count);
}
}
//output
// C:\java>javac Test.java
// C:\java>java Demo
// 1
// C:\java>
Q8:-How many times Hello is printed?
class Demo {
public static void main(String[] args) {
while(1){
System.out.println("Hello");
}
}
}
//output
// C:\java>javac Test.java
// Test.java:5: error: incompatible types: int cannot be converted to boolean
// while(1){
// ^
// 1 error
// C:\java>
or
class Demo {
public static void main(String[] args) {
while(1){
System.out.println("Hello");
}
}
}
//output
Blank Screen in Infinite Loop
or
class Demo {
public static void main(String[] args) {
int a=0;
while(a){
System.out.println("Hello");
}
}
}
//output
// C:\java>javac Test.java
// Test.java:5: error: incompatible types: int cannot be converted to boolean
// while(1){
// ^
// 1 error
// C:\java>
Comments
Post a Comment