Modulo

Take a look at line 26. The % operator is called modulo. Modulo computes a remainder. The modulo operator provides a way to execute code once in every several iterations of a loop. We are using modulo here to check whether a number variable can be perfectly divided by 2. If the number can be divided, the reminder will be equal to 0, so we will have an even number! Otherwise, the number must be odd.

Coming back to line 14, as a method is a substitute for a value and IsEvenMethod is a return type method, returning Boolean value inside if statement. In simpler words, when the value of i is passed to IsEvenNumber, the method returns a true or false value. If the value is true, line 15 will be executed and the message will be printed on the Console window.

Let's test the code. If all goes well, you should see lots of messages printed on the Console window, as follows:

Modulo

All went very well then! Go ahead and try to experiment with this code. Why not run the loop 1,000 times? Do it! Computers can make mathematical calculations at amazing speeds.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset