In this article,Looping Control Structures are explained in detail with examples.
These control structures are used to execute a block of statements continuously/iteratively based on a condition.
1.While loopThese control structures are used to execute a block of statements continuously/iteratively based on a condition.
It is also known as pre-test loop i.e. first, we check the condition and if it is true, then it executes
the body.
Syntaxinitialization;
while(condition)
{
-----
body;
----
increment/decrement;
}
//program related to Looping Control Structures--while loop
class whileloop
{
public static void main(String args[])
{
int i=0;
while(i<10) // here condition is checked.If true, it enters into the loop and prints the result.
{
System.out.println("Number is: "+i);
i++;
}
System.out.println("End of WhileLoop"); // if condition fails, loop terminates and prints End of loop.
}
}
OutputIt is a post-test loop i.e,Condition is checked after executing the body at least once.
Syntax
Initialization;
do
{
-------
body;
-----
increment/decrement;
}
while(condition);
Ex
//program related to Looping Control Structures--Do While loop
class DoWhileLoop
{
public static void main(String args[])
{
int i=0;
do
{
System.out.println("Number is: "+i);
i++;
}
while(i<10);
System.out.println("End of DoWhileLoop");
}
}
Output
To get the code file use me.
3.For loop
It is also pre test loop.It is very efficient looping structure because initialization,condition checking,increment/decrement is done in a single line. (From topic Looping Control Structures)
Syntax
for(initialization;condition;increment/decrement)
{
------
body;
------
}
Ex
//program related to Looping Control Structures--for loop
class ForLoop
{
public static void main(String args[])
{
for(int i=0;i<10;i++)
{
System.out.println("Numbers are: "+i);
}
System.out.println("End of for loop");
}
}
Output
To get the code file use me.
Break Statement using loops.
This statement is used in two situations i.e, switch case control structure and looping control structure.
In switch-case,It is used with case-value statements. (From topic Looping Control Structures)
In looping structures,it is used with a condition when encountered,it takes the control of execution out of looping block,by skipping current iteration remaining statements and also remaining iterations.
Ex
//program related to Looping Control Structures--Forloops break.
class ForLoopBreak
{
public static void main(String args[])
{
{
if(i==6)
break; // here when i=6,loop breaks and proceed to print.
System.out.println("Number is: "+i);
}
System.out.println("Hello world");
}
}
Output
Continue Statement using loops.
This statement is used only in looping structures when encountered in a loop, it skips remaining statements of the current iteration and continues with next iteration.
It is also used with a condition in a loop. (From topic Looping Control Structures)
Ex
//program related to Looping Control Structures--For loop Continue.
class ForLoopContinue
{
public static void main(String args[])
{
for(int i=0;i<15;i++)
{
if(i==2) //here when i=2 then loop continue to next iteration.
continue;
if(i==8)
continue;
if(i==12);
break;
System.out.println("Number is: "+i);
}
System.out.println("End of the program");
}
}
Output
To get the code file use me.
It is also used with a condition in a loop. (From topic Looping Control Structures)
Ex
//program related to Looping Control Structures--For loop Continue.
class ForLoopContinue
{
public static void main(String args[])
{
for(int i=0;i<15;i++)
{
if(i==2) //here when i=2 then loop continue to next iteration.
continue;
if(i==8)
continue;
if(i==12);
break;
System.out.println("Number is: "+i);
}
System.out.println("End of the program");
}
}
Output
To get the code file use me.
Continue to the next topic Nested Loop and labeled break and labeled continue.
Begin your career in Digital Marketing,What is digital marketing? Digital Marketing online course. It's an current evolving technology which can give support to establish your own startup through Digital Marketing
Do check my new startup Surprise Planners in Hyderabad- Lavish Surprises our services are surprise party planners in Hyderabad, surprise gifts, surprise birthday party planners Hyderabad, Wedding anniversary surprises, surprise planners Hyderabad.
Hi Friends, Please comment down your views in the comment section below. Thank you...
No comments:
Post a Comment