25-08-2017, 09:32 PM
JAVA PROGRAMMING CONSTRUCTS
Using Looping Statements
A looping statement causes a section of program to be executed a certain number of times.
The repetition continues while the condition set in the looping statement remains true.
When the condition becomes false, the loop ends and the control is passed to the statement following the loop.
The for Loop:
Is a looping statement iterating for a fixed number of times.
Consists of the for keyword followed by parentheses containing three expressions, each separated by a semicolon.
The three expressions in the for loop are:
Initialization expression
Test expression
IterationExpr expression
Is used when the number of iterations is known in advance.
For example, it can be used to determine the square of each of the first ten natural numbers.
Enhancing Methods of a Class
Methods are used to access the variables that are defined in a class.
A method is an interface to a class.
Parameterized methods need some extra information for its execution.
The extra information is passed to the method by using arguments.
Arguments are also known as parameters of the methods.
Defining Parameterized Methods:
Parameterized methods use parameters to process data and generate an output.
The output of a parameterized method is not static and depends on the value of the parameters passed.
Parameters in a parameterized method allow the method to be generalized.
Passing Arguments to a Method
Arguments can be passed to a method using two ways:
Call-by-value: Copies the value of the actual parameters to the formal parameters. Therefore, the changes made to the formal parameters have no effect on the actual parameters.
Call-by-reference: Passes the reference and not the value of the actual parameters to the formal parameters in a method. Therefore, the changes made to the formal parameters affect the actual parameters.
You can also pass arguments to the main() method at the run-time of a program.