Hey guys can anyone answer this.....
what will happen if u execute the following statement in JAVA:
for(int i=1;i<=100;i++) ;
{
System.out.println("Hello....!")
}
Answers
Answered by
3
Hello Friend,
Here, the code shows an implementation of 'for' loop in JAVA.
→ 'for' loop in JAVA is used to execute a specific set of statements a fixed number of times.
→ Here, IF it had been given as:
for (int i=1; i<=100;i++)
{
System.out.println("Hello....!");
}
Then the output would be:
Hello....!
Hello....!
Hello....!
(100 times)
(Here, i=1 initializes variable i as 1. The loop will execute till i<=100. The increment condition: i++ increments value of i by 1 after each loop.)
→ But NOTE that in your question, the code is this:
for(int i=1;i<=100;i++) ; [Note this ';' ]
{
System.out.println("Hello....!")
}
There is a semicolon after 'for' loop line.
This means that the statements given in the block will not be executed. The semicolon breaks each iteration of loop before it goes into the block to print "Hello....!"
In other words , the loop will execute 100 times, but nothing will be printed.
→ The output is a blank screen.
Hope it helps.
Purva
@Purvaparmar1405
Brainly.in
Here, the code shows an implementation of 'for' loop in JAVA.
→ 'for' loop in JAVA is used to execute a specific set of statements a fixed number of times.
→ Here, IF it had been given as:
for (int i=1; i<=100;i++)
{
System.out.println("Hello....!");
}
Then the output would be:
Hello....!
Hello....!
Hello....!
(100 times)
(Here, i=1 initializes variable i as 1. The loop will execute till i<=100. The increment condition: i++ increments value of i by 1 after each loop.)
→ But NOTE that in your question, the code is this:
for(int i=1;i<=100;i++) ; [Note this ';' ]
{
System.out.println("Hello....!")
}
There is a semicolon after 'for' loop line.
This means that the statements given in the block will not be executed. The semicolon breaks each iteration of loop before it goes into the block to print "Hello....!"
In other words , the loop will execute 100 times, but nothing will be printed.
→ The output is a blank screen.
Hope it helps.
Purva
@Purvaparmar1405
Brainly.in
Similar questions