control-structure-in-java-masterskill
Control structure
A Program is a set of Instruction or block instruction java provides "Control structure" that can change the path of execution and control the execution of instruction.
Three kind of control structure in java
1. control statement in java
A. if statement
B. Nested if statement
C. if else statement
2.Loops in java / looping
A. for loop
B. While loop
C. Do - While loop
3.Branching statement in java
A. Break
B. Continue
1. Control Statement in Java
Java control statement use to chose the path for execution, there are some type of control statement
A. If statement
Its is simple - decision making statement, it use to decide whether the statement or block of statement should be executed not.
" A statement execute only if they given condition return true otherwise the block of the statement skips" the if statement always return the " Boolean value "
B. Nested if statement
The Nested-if statement mean an if statement inside another if statement .
" the inner block of if statement execute only outer block conditions is true.
C. if- else statement
In the if statement , the block of statemen execute, when the given condition return true otherwise block of the statement skips .
2. Loop in java/ looping Statement
2. Loop in java/ looping Statement
The Loop in the Java use to iterate the instruction multiple times, when we want to execute the statement to execute the statement a number of time then we use loop in Java.
A. For loop
A for loop execute the block of code several time . it mean the number of iterations is fixed java provider a concise way of writing the loop structure .
A. For loop
For example// for loop creation
for(int i=1; i<=3; i++)
{
System.out.println("iteration number;"+i");
}
B. While loop
A while loop allows code to execute repeatedly until the given condition return true if the number of iteration doesn't fixed, it recommended using to a while loop. "its also know as- Entry control loops
For example
// while loop creation
int i =1; while(i<=3)
{
system. out.println("iteratin number:"+i)
i++
}
{
for(int i=0;i<=3;i++)
system.out.println("iteration number ;"+i);
if(i==2)
break;
}
}
C. do-While loop
It is like a while loop ,with the only difference being that its check for the condition after the execution of the body of the loops " Its also known as Exit control loop
For example// creating a while loop
int i=1;
do
{ system.out.println("iteration number="+i); i++; }
int i=1;
do
while (i<=3)
}
}
3. Branching Statement in java
These use to alter the flow of control in loos.
A. Break
If a break statement encounter inside loop , the loop immediately terminate , and the control of the program goes the next statement following the loopsFor example
Public static void main (string arg[] ){
for(int i=0;i<=3;i++)
system.out.println("iteration number ;"+i);
if(i==2)
break;
}
}
B. Continue
The continue statement use in a loop control structure , if you want to skip some code and need to jump to the next iteration of the loop immediately then you can use a continue statement , it can used in for loop or While loop.
For example
public static void main (string args [] )
{
for(int i=0; i<=3; i++)
{
if(i==1)
continue;
system.out.println("iteration number="+i);
}
}
Comments
Post a Comment