0% found this document useful (0 votes)
2 views8 pages

Control Structures

The document explains control structures in Java, which allow programmers to control the flow of execution in a program. It categorizes control statements into conditional statements (if-else, switch), iterating statements (for, while, do-while), and transfer statements (break, continue, return). Each type of statement is accompanied by syntax, examples, and rules for proper usage.

Uploaded by

murali krishna
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views8 pages

Control Structures

The document explains control structures in Java, which allow programmers to control the flow of execution in a program. It categorizes control statements into conditional statements (if-else, switch), iterating statements (for, while, do-while), and transfer statements (break, continue, return). Each type of statement is accompanied by syntax, examples, and rules for proper usage.

Uploaded by

murali krishna
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Control structures

The statement of a java program will be executed in sequential order. If we don’t want to execute
the statements in sequential order i.e. if we want to execute the statements in random order
according to programmer choice, then we take the help of control statements.
The control statements will help the programmer to control the flow of execution in a java program.
The control statements are classified as following
1. Conditional statements
a. if else statement
b. switch statement
2. Iterating statements
a. for loop
b. while loop
c. do-while loop
d. for each loop
3. Transfer statements
a. break
b. continue
c. return

Conditional Statements:
If-else statement: if else statement can be used for executing a group of statements based on a
condition.
Syntax:
if (condition)
{
statements1;
}
else
{
statements2;
}
If the condition is satisfied then it will be executing if block and if the condition is not satisfied then
it will be executing else block.
Example:
class IfElseDemo
{
public static void main (String[] args)
{
int n = 98;
if (n%2 == 0)
{
[Link](“Even Number”);
}
else
{
[Link](“Odd Number”);
}
}
}
Rule 1: Specifying the condition to if else statement is mandatory and it should be of boolean type
(either a condition or boolean variable or boolean value).
Rule 2: Specifying the else block is optional.
Rule 3: Specifying the { } is optional. If we don’t specify the { } then it will consider only one
statement, and that one statement is mandatory. If we want to consider multiple statements then
specifying the { } is mandatory. Within the { } we can specify any number of statements.
Rule 4: In if else statement we cannot execute both the blocks and we cannot skip both the blocks,
it will always execute exactly one block.

Switch statement: The switch statement can be used for executing a group of statements based on
a value.
Syntax:
switch (argument)
{
case label1: statements1;
break;
case label2: statements2;
break;
case label3: statements3;
break;

:
default: default statements;
}
Example:
class SwitchDemo
{
public static void main (String[] args)
{
int ch = 2;
switch(ch)
{
case 1: [Link](“First Choice”);
break;
case 2: [Link](“Second Choice”);
break;
case 3: [Link](“Third Choice”);
break;
case 4: [Link](“Fourth Choice”);
break;
default: [Link](“Wrong Choice”);
}
}
}
Rule 1: Specifying the argument to switch statement is mandatory and it should be of either byte,
short, int or char only.
Rule 2: Specifying the flower braces to switch statement is mandatory.
Rule 3: Specifying the case and default statements in switch is optional.
Rule 4: A switch statement can contain any number of cases and it can contain at most one default.
Rule 5: The default statement can be specified anywhere in the switch statement.
Rule 6: The case labels must be unique i.e. they should not be empty and they should not be
duplicated.
Rule 7: Specifying the break statement is optional. The break is a transfer statement and it will
transfer the control from inside the switch to outside the switch, so that it skips the execution of
remaining cases.
When a switch statement is executed, switch argument is compared with case labels and execution
will begin from that case onwards whose label is matching with the argument and continues until it
encounters a break statement or until it encounters the end of switch.
For Loop: This loop can be used for executing the statements multiple times. A for loop has to be
used when we know the exact number of iterations.
Syntax:

for (initialization; condition; increment/decrement)


{
statements;
}
Example:
class ForDemo
{
public static void main(String[] args)
{
int n = 6;
for(int i=1; i<=10 ; i++)
{
[Link] (n+” * ”+ i+” = ”+ (n*i));
}
}
}
Rule 1: All the three sections of for loop are optional. If we do not specify any initialization and if
do not specify any inc/dec, then the compiler will not specify any initialization and will not specify
any inc/dec, but if we do not specify any condition then the compile will automatically specify a
boolean value true.
Rule 2: Specifying the section separators (;) in a for loop are mandatory.
for (; ; ) will execute for infinite times.
Rule 3: In for loop initialization section and the inc/dec section can contain any number of valid
java statements, but the condition section must contain a value of only boolean type.
Rule 4: The initialization section can contain multiple initializations separated by a comma(,) all
should be of same time and the data type should be specified only one time.
Rule 5: The condition section can contain any number of conditions, but they must be joined by
using a logical operator(& && | || ^).
Rule 6: The inc/dec section can contain multiple increments or decrements but separated by a
comma (,)
Rule 7: If a variable is declared inside for loop, then it can be used only in that for loop.
Rule 8: Specifying the { } is optional. If we don’t specify the { } then it will consider only one
statement, and that one statement is mandatory. If we want to consider multiple statements then
specifying the { } is mandatory. Within the { } we can specify any number of statements.

While loop: This loop can be used for executing the statements multiple times. A while loop has to
be used when we do not know the exact number of iterations.
Syntax:
while(condition)
{
statements;
}
Example:
class WhileDemo
{
public static void main(String args[])
{
int n = 8, i = 1;
while(i <=10)
{
[Link](n+” * ”+i+” = ”+(n*i));
i++;
}
}
}
Rule 1: Specifying a condition to while loop is mandatory and it should be of boolean type.
Rule 2: Specifying the { } is optional. If we don’t specify the { } then it will consider only one
statement, and that one statement is mandatory. If we want to consider multiple statements then
specifying the { } is mandatory. Within the { } we can specify any number of statements.
Note: A java program should not contain any unreachable statements i.e. every statement has to be
executed at some point of time.
Do-While loop: This loop can be used for executing the statements multiple times. A do-while loop
has to be used when we do not know the exact number of iterations.
Syntax:
do
{
statements;
} while(condition);
Example:
class DoWhileDemo
{
public static void main(String args[])
{
int n = 9, i = 1;
do
{
[Link](n+” * ”+i+” = ”+(n*i));
i++;
} while(i <=10);
}
}
Rule 1: Specifying a condition to do-while loop is mandatory and it should be of boolean type.
Rule 2: Specifying the { } is optional. If we don’t specify the { } then we must specify exactly only
one statement. If we want to consider multiple statements then specifying the { } is mandatory.
Within the { } we can specify any number of statements.
Nested Loop: If we specify a loop inside another loop then it is called as nested loop. Any loop can
be specified inside any other loop any number of times.
Example:
class NestedDemo
{
public static void main(String[] args)
{
for(int i=1;i<=5;i++)
{
for(int j=1;j<=5;j++)
{
[Link](“* ”);
}
[Link]();
}
}
}
Transfer Statements:
Break: break is a transfer statement which can be used either inside switch statement or inside a
loop.
The break statement, when used in switch will transfer the control from inside the switch to outside
the switch, so that we skip the execution of removing cases.
The break statement, when used in loop, will transfer the control from inside the loop to outside the
loop, so that we will skip the execution of remaining iterations.
Note: When we are specifying the break statement in a loop we are recommended to specify the
break statement along with condition.
Example:
class BreakDemo
{
public static void main (String[] args)
{
int sum = 0, capacity = 15;
for(int i=1;i<=100;i++)
{
[Link](i);
sum = sum + i;
if(sum>=capacity)
break;
}
[Link](sum);
}
}
Continue: continue is a transfer statement which has to be used only in loops. The continue
statements will skip the current iteration and continues with the remaining iterations.
Note: When we are specifying a continue statement in a loop, then we are recommended to specify
the continue statement along with a condition.
Example:
class ContinueDemo
{
public static void main(String[] args)
{
for(int i=1;i<=20;i++)
{
if(i==7 | i==13)
continue;
[Link](i);
}
}
}

You might also like