The Java for loop is used to iterate a part of the program several times. If the number of iteration is fixed, it is recommended to use for loop.
The syntax for a simple for loop is:
for (initial-action; loop-continuation-condition; action-after-each-iteration)
{
statements;
}
A for loop generally uses a variable to control how many times the loop body is executed and when the loop terminates. This variable is refereed to as a control variable. The initial-action often initializes a control variable, the action-after-each-iteration usually increments or decrements the control variable, and the loop-continuation-condition tests whether the control variable has reached a termination value.
Example 1:
The following code segment will print "Saya suka Java" 100 times.
int i = 0;
for (i=0; i<100; i++) {
System.out.println("Saya suka Java");
}
System.out.println("End");
And the flow chart for the above code segment is as follows.
In the above for loop, i is initializes to 0, then repeatedly executes the println statement and evaluates i++ while i is less than 100. The loop will terminates once i is equal or more than 100.
The expression (i<100) is evaluated at the beginning of each iteration. If it is true, execute the loop body. If it is false, the loop terminates and the program control turns to the line following the loop.
The action-after-each-iteration, i++ is a statement that adjusts the control variable. This statement is executed after each iteration. It increments the control variable. Eventually, the value of the control variable should force the loop-continuation-condition to become false. Otherwise the loop is infinite (the loop will not stop).
The loop control variable can be declared and initialized in the for loop (if and only if the loop control variable is used only in the loop, and not elsewhere).
Example 2: (declared and initialized in the loop)
for (int i=0; i<100; i++){
System.out.println("Saya suka Java");
}
System.out.println("End");
a. Draw a flow chart to calculates and display the total of 10 integers.
b. Write a program that reads 10 integers, and display the total of all numbers.
If the input is 2 3 4 4 5 3 2 9 1 7, the output will be 40.
Scanner sc = new Scanner(System.in);
int sum = 0;
for (int i=1; i<=10; i++){
int num = sc.nextInt();
sum += num;
}
System.out.println(sum);
a. Draw a flow chart to calculates and display the total of N integers.
b. Write a program that reads 10 integers, and display the total of all numbers.
If the input is 2 3 4 4 5 3 2 9 1 7, the output will be 40.
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int sum = 0;
for (int i=1; i<=N; i++){
int num = sc.nextInt();
sum += num;
}
System.out.println(sum);
}
If we have a for loop inside the another for loop, it is known as nested for loop. The inner loop executes completely whenever outer loop executes.
Example:
public class NestedForExample {
public static void main(String[] args) {
for(int i=1;i<=3;i++){
for(int j=1;j<=3;j++){
System.out.println(i+" "+j);
}//end of i
}//end of j
}
}
The output will be:
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
Problem Description
Write a program to determine the highest number.
Input Format
The first line of input is N (1 <= N <= 100) which is the number of cases. This is followed by N lines of input. Each of those lines of input represents a case. It starts with X (X <= 100) which is the number of input data followed by a list of X data.
Output Format
For each test case, output a line in the format "Case #x: M N" where x the case number (starting from 1), M is the maximum number of integers, and N is the number of its occurrences.
Sample Input
Sample Output
4
3 2 5 7
5 9 8 8 9 5
8 7 2 3 6 7 8 10 8
6 27 6 27 27 19 11
Case #1: 7
Case #2: 9
Case #3: 10
Case #4: 27
The objective is to find the max number.
Read carefully the input and output format. And look at the sample input and output.
In the sample input, the first number is integer 4. This number representing the number of cases. Each line is a case. Thus, 4 cases in 4 lines.
The second line of the input is the first case. The first number is the number of data. In this case, the first number is 3. There fore, there are 3 data which are 2, 5, and 7. And the maximum number of these three numbers is 7.
import java.util.Scanner;
public class FindMaxNum {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int caseN = sc.nextInt();
for (int i=1; i<=caseN; i++){
System.out.print("Case #" + i + ": ");
int bil = sc.nextInt();
int max = 0;
for (int j=0; j<bil; j++){
int num = sc.nextInt();
if (num > max)
max = num;
}
System.out.println(max);
}
}
}