Control Statements
In Java, control statements are used to control the flow of a program. They allow you to make decisions, repeat actions, and perform various operations based on certain conditions. There are three main categories of control statements in Java:
Selection Statements: These are used for making decisions in your code.
if
statementif-else
statementswitch
statement
Iteration Statements: These are used for repeating a set of statements.
for
loopwhile
loopdo-while
loop
Jump Statements: These are used for transferring control within your code.
break
statementcontinue
statementreturn
statement
Selection Statements
if
statement
if
statementThe if
statement allows you to execute a block of code if a given condition is true.
if-else
statement
if-else
statementThe if-else
statement allows you to execute one block of code if a condition is true and another block if the condition is false.
switch
statement
switch
statementThe switch
statement allows you to select one of many code blocks to be executed.
Iteration Statements
for
loop
for
loopThe for
loop is used to iterate a specific number of times.
while
loop
while
loopThe while
loop is used when you don't know in advance how many times the loop will run.
do-while
loop
do-while
loopThe do-while
loop is similar to the while
loop but guarantees that the loop block is executed at least once.
Jump Statements
break
statement
break
statementThe break
statement is used to exit a loop or switch statement.
continue
statement
continue
statementThe continue
statement is used to skip the current iteration of a loop and continue with the next iteration.
return
statement
return
statementThe return
statement is used to exit a method and return a value to the caller.
These control statements are fundamental in Java programming and are essential for controlling the flow of your code and making your programs more flexible and responsive to different conditions.
Last updated