An exercise on for-loops and if-else: Video Lecture 10
In the previous video lecture, we learned how to repeat a segment of code using a for-loop. The segment we used for repetition was small. In the following video lecture, we incorporate an if-else structure inside the scope of a for-loop. We use a simple problem to develop this practice scenario.
Contents
The Problem used in the Video Lecture
Print all the integer numbers starting between 1 and 20 (including 1 and 20.) Mark each of them by “odd” or “even” based on whether the number is odd, or even. You must use a for-loop in your code to iterate over all the integers in the range.
Sample output is as follows.
1 odd 2 even 3 odd 4 even 5 odd 6 even 7 odd 8 even 9 odd 10 even 11 odd 12 even 13 odd 14 even 15 odd 16 even 17 odd 18 even 19 odd 20 even
The Code used in the Video Lecture
We used the following code in the video lecture. If you decide to use it, please save the code in a file named OddEven.java
.
class OddEven{
public static void main(String[] args){
int i;
for (i=1; i<=20; i=i+1){
if (i%2==0){
System.out.println(i+" even");
}else{
System.out.println(i+" odd");
}
}
}
}
The Video Lecture
The YouTube video lecture is provided below.
Exercise
Print all the numbers between 1 and 25 (including 1 and 25.) Mark the numbers that are divisible by 5 with text “Penta”. A sample output is provided below.
1
2
3
4
5 Penta
6
7
8
9
10 Penta
11
12
13
14
15 Penta
16
17
18
19
20 Penta
21
22
23
24
25 Penta
Transcription of the Audio of the Video
Hi, this is Dr. Shahriar Hossain. I hope that you are enjoying the practice of programming using Java. In the previous video lecture, we learned looping. We now know how to use Java for-loops. Today, we will see how we can put if-else statements within a for-loop.
Let us start with a problem.
The problem
Print all the integer numbers from 1 to 20, and mark each of them by “odd” or “even” based on whether the number is odd, or even.
That is, we need an output like this one, where we have an integer and against every integer we have the information whether the integer is odd of even. We have twenty lines for twenty integers between 1 and 20.
[Show the output]
1 odd
2 even
……
20 even
[End of Show the output]
Components of the program
The program has a few components.
- We need to go over every integer number between 1 and 20.
- For each of the numbers, we have to check whether the number is odd or even.
- Based on whether the number is odd or even, print the number and print the text “odd” or “even”.
Let us write the main method in a class named OddEven. The file name is obviously OddEven.java.
The for-loop
Let us create an integer variable named “i”. This variable “i” will be used to hold all the integer values between 1 and 20. From the previous video lecture, we know how to write a for loop and change the value of a variable. Inside the parentheses of the for loop, we do an initialization. In this case, we want the variable “i” to vary between 1 to 20. Therefore, the initialization for “i” should be 1. We want the value of “i” to become each of the numbers between 1 to 20. Therefore, the condition is that the variable “i” should never be greater than 20. Hence, we write i<=20. As said, the value of the variable “i” should be each number. That is, the value of “i” should be incremented by 1 after each iteration of the for-loop.
Varying the value of i
Now that we have the structure of the for loop, we can do whatever we want inside the scope of the for loop. This for loop will go inside the scope 20 times. In each of these 20 iterations, the variable “i” will have a value between 1 and 20. Seeing is believing. Let us print the value of “i” inside the scope of the for loop. Let us save the file, compile it, and then run the class file.
Notice that the values of “i” are printed on the terminal. The values are 1, 2, 3, so and so forth, up to 20. Let us get back to the code.
How to find if a number is odd or even
Now, for a value of the variable i, we need to check whether the value is “odd” or “even”. How do we know if a number is odd or even? If you divide a number by 2, if there is a zero remainder, then the number is even. If the remainder is 1, then the number is odd.
For example, if you divide 5 by 2, the remainder is 1. Therefore, 5 is an odd number.
As another example, if you divide 8 by 2, the remainder is 0. Therefore, 8 is an even number.
That is, when you divide an odd number by 2, the remainder is 1. When you divide an even number by 2, the remainder is 0.
When you divide a number by 2, the remainder can be either 0 or 1, nothing else. Therefore, if you divide the value of the variable “i” by 2, and receive a remainder of 0, you can mark the value of “i” as “even”. Otherwise, the value is “odd”.
The remainder operation
In Java, the remainder operation is denoted by the percentage symbol (%). It is commonly called “mod”, which stands for modulus or remainder.
If-else
What we want to say here is the following, if (i%2 ==0) then we want to print the number and print the word “even”.
Otherwise, which is the else part, we want to print the number and the word “odd”.
Equality comparison
Notice that this double equals (“==”) symbol refers to equality comparison, as explained in one of the earlier videos. We are comparing if the left side and the right side of the “double equals” symbol have the same values. If yes, we execute the scope, otherwise we execute the scope of the else part.
Let us save the file. Compile it and run the class file. Notice that we have printed the numbers and side by the numbers, we have reported whether the number is odd or even.
Concluding remarks
One specialty of this program is that we have incorporated if-else statements inside the for-loop. In the upcoming videos, we will write more complex codes involving looping. In the previous video, I mentioned that there are three types of looping (1) For loops, (2) Wile loops, and (3) Do-while loops. I should mention that the use of “for loop” is sufficient. Still, we will go over while and do-while loops later. For now, I want to let you know that if you know “for loops”, you can practically do whatever things are done with while and do-while loops.
I hope that the videos we are making help you in your quest to learn Java programming. Please write comments below if you have any question. Please subscribe to Computing4All.com and to this YouTube channel if you have not already done so. You will receive notifications on our new posts when you subscribe. Have a wonderful experience in learning to program. See you soon in the next video.
1 Comment
I want learn java program