Looping Statements:
It is used to execute an Instruction or set of Instructions Multiple times. Or Looping Statement will help
programmer to execute the statements again & again.
*) It will allow programmer to control the number of repetitions.
In Java We Have 4 Types of Looping Statements
1.) while loop
2.) do-while loop
3.) for loop
4.) for each loop/enhanced for loop/Advanced for loop
Note: Every loop statement we design must and should have three
important segments.
i) Initialization
ii) Condition
iii) Updation
1.) while loop: It is a keyword used as loop statement.
Syntax: while(condition)
// java stmts;
*) we go for while loop whenever we need to execute a stmt/ group of stmts repeatedly based on a
condition.
*) we go for while loop whenever we don’t know number of iterations
Basics Programs :
1.)wajp to print * 5 times vertically?
2.) wajp to print * 5 times Horizontally?
3.)wajp to print multiple of 3 upto 10 times ?
2.) do-while loop: It is a loop statement which uses 2 keywords.
i) do
ii) while
Syntax: do{
// java stmts;
while(condition);
*) we go for do-while stmt whenever we need to execute a statement or group of statements atleast
once irrespective whatever the situation is.
Basics Programs :
1.)wajp to print “HI” 3 times using do while loop.
2.) wajp to print “HI” 1 time using do while loop.
3.) wajp to print “HI” infinite times using do while loop.
3.) For Loop: It is a keyword used as loop statement.
Syntax: for(initialization ; Condition ; Updation)
// java Stmts;
*) we use for loop whenever we know number of iterations.
Note: The variable declared inside initialization segment of for loop will be a local variable to the for
loop, i.e, we cannot use that variable outside for loop, If we try to use we get Compile Time Error.
4.)Nested for loop: A Loop inside another loop is known as nested loop.
Syntax: for(initialization ; Condition ; Updation)
for(initialization ; Condition ; Updation)
// java Stmts;
METHODS / FUNCTIONS
Method is a set of instructions or Block of codes which is used to perform a specific task.
*Method gets executed only when they are called.
Syntax to create a method in java:
[modifiers] Returntype methodName ([formal arguments])
// java Stmts;
[ return [value/exp];]
Some Imp Method Terminologies:
1.)Method Signature: A methodName followed by formal arguments is known as Method Signature.
2.)Method Declaration: The Modifiers & Returntype followed by Method Signature is known as Method
Declaration.
3.)Method Definition: A Method declaration followed by a method block or body is known as Method
Definition.
Modifier:
*) These are the Keywords
*) They are responsible to change the behaviour of the members of the class.
Types of Modifiers:
*) Based on Access Modifiers: public , private, protected .
*) Based on Non-Access Modifiers: static, abstract, final …etc……
Return Type: It is a datatype which specifies (tells) what type of data is given back to the caller of the
method, once the execution is completed.
*) A Method Can Have 3 Possible return types.
i) Primitive type ( byte, short, int, long, float, double, char, boolean ).
ii) Non Primitive type (Any classname).
Iii) void
Types Of Methods
i) Based on Arguments:
a.) No-Argument Method
b.) Parameterized Method
ii) Based on modifiers:
a.) static method
b.) non-static method
c.) final method
d.) abstract method
e.) concrete method
*) Characteristics of Method
1.) Method gets executed only when it is called.
*) we can call method with the help of method signature.
Syntax: methodName();
2.) Methods can execute any number of times.
3.) It will Increase the code Reusability.
4.) If a method returns something, store it Or Print it.
What Happens when a method is called ?
*) Execution of current method is paused.
*) Control is transferred from calling method or caller to called method.
*) Execution of called method starts from beginning.
*) Once the execution is completed, control returns to the caller or calling method.
*) Execution of caller resumes.
No-Argument Method:
*) A method which doesn’t have a formal arguments declared in it is known as No-argument method.
Or
*) A method declared without formal arguments is known as No-argument method.
Syntax to call No-argument Method:
methodName();
Parameterized Method:
*) A method which is having formal arguments is known as Parameterized method.
Syntax to create parameterized Method:
Modifier returntype methodName (datatype v1, datatype v2){
Syntax to call parameterized Method:
methodName (actual arguments);
*) Formal Argument: The variable declared /created in the method declaration stmt is called as formal
arguments.
Note:
*) formal arguments are local variables of the method.
*) we can use v1 & v2 only in the block where it is declared.
*) Actual Arguments: The data / value which is passed in the method call statement is known as actual
arguments.
Note:
*) Actual arguments are in method calling stmt.
*) Formal arguments are in method definition.
Return data:
*) If the return type is void it will not return anything to the caller.
*) A method can return the data to the caller ,if the returntype of the method is anything other than
void.
Rules to call a method:
1.) The number of formal arguments & actual arguments should be same.
2.) The type of corresponding actual and formal argument should be same, else compiler tries for
widening , if it is not possible then it throws CTE.
Return Statement:
*) It is a keyword.
*) Return is a control transfer stmt, it stops the execution of current method and transfer the control
back to the caller.
Rules:
*) return stmt is mandatory if the returntype of the method is anything other than void.
*) If the returntype is void , return stmt is optional. ,but we should use return stmt without value.
*) return stmt in java can return only one value.
*) return stmt should be the last stmt of the block.