Computer Science, asked by tiwi06, 6 months ago

Consider the following code and predict the output if the input given is : 13579
Scanner sc = new Scanner(System.in);
int sum = 0;
for (int i = 0; i <5; i++) {
System.out.print("Enter an integer: ");
sum = sum + sc.nextInt();
}
System.out.println("The sum is " + sum);​

Answers

Answered by ibolbam
1

Explanation:

for( ; ; ){

;

}

The initialization statement is executed before the loop starts. It is generally used to initialize the loop variable.

Condition statement is evaluated before each time the block of statements are executed. Block of statements are executed only if the boolean condition evaluates to true.

Statement is executed after the loop body is done. Generally it is being used to increment or decrement the loop variable.

Following example shows use of simple for loop.

for(int i=0 ; i < 5 ; i++)

{

System.out.println("i is : " + i);

}

It is possible to initialize multiple variable in the initialization block of the for loop by separating it by comma as given in the below example.

for(int i=0, j =5 ; i < 5 ; i++)

It is also possible to have more than one increment or decrement section as well as given below.

for(int i=0; i < 5 ; i++, j--)

However it is not possible to include declaration and initialization in the initialization block of the for loop.

Also, having multiple conditions separated by comma also generates the compiler error. However, we can include multiple condition with && and || logical operators.

Answered by 69topinub69
0

Answer:

25 because 1+3+5+7+9 = 25

Similar questions