Beruflich Dokumente
Kultur Dokumente
• A programming language uses control structures to control the flow of execution based on some
certain conditions
• Types
• Selection statements
• Iteration statements
• Jump statements
SELECTION STATEMENTS
• If statement
• If-else statement
• If – else – if Ladder statement
• Switch statement
IF STATEMENT
if (condition)
{
Statement block;
}
Statement X;
// java program to illustrate if statement
class Demo1
{
public static void main(string args[])
{
int i = 10;
if (i > 15)
system.out.println("10 is less than 15");
system.out.println(“EXIT");
}
}
IF – ELSE STATEMENT
if (condition)
{
if block;
}
else
{
else block;
}
Statement X;
// Java program to illustrate if-else statement
class Demo2
{
public static void main(String args[])
{
int i = 10;
if (i < 15)
System.out.println("i is smaller than 15");
else
System.out.println("i is greater than 15");
System.out.println(“EXIT");
}
}
NESTED IF – ELSE STATEMENT
if (condition 1) if (condition 1)
{ {
if (condition 2) block A;
{ }
block A; else
} {
else if (condition 3)
{ {
block B; block B;
} }
} else
else {
{ block C;
block C; }
} }
} Statement X;
Statement X;
// java program to illustrate nested-if statement
class Demo3
{
public static void main(string args[])
{
int i = 10;
if (i == 10)
{
if (i < 15)
system.out.println("i is smaller than 15");
if (i < 12)
system.out.println("i is smaller than 12 too");
else
system.out.println("i is greater than 15");
}
}
}
IF-ELSE-IF LADDER
if (condition 1)
statement 1;
else if (condition 2)
statement 2;
.
.
.
.
else
statement A;
Statement X;
// java program to illustrate if-else-if ladder
class Demo
{
public static void main(string args[])
{
int i = 20;
if (i == 10)
system.out.println("i is 10");
else if (i == 15)
system.out.println("i is 15");
else if (i == 20)
system.out.println("i is 20");
else
system.out.println("i is not present");
}
}
SWITCH CASE
switch (expression)
{
case value 1:
statement 1;
break;
case value 2:
statement 2;
break;
.
.
case value N:
statement N;
break;
default:
statement Default;
}
// java program to illustrate switch-case
class Demo5
{
public static void main(string args[])
{
int i = 9;
switch (i)
{
case 0:
system.out.println("i is zero.");
break;
case 1:
system.out.println("i is one.");
break;
case 2:
system.out.println("i is two.");
break;
default:
system.out.println("i is greater than 2.");
}
}
}
ITERATION STATEMENTS
• while loop
• do – while loop
• for loop
• nested for loop
WHILE LOOP
while (x <= 4)
{
system.out.println("value of x:" + x);
x++;
}
}
}
DO WHILE LOOP
do
{
statements..
}
while (condition);
// java program to illustrate do-while loop
class DowhileDemo
{
public static void main(string args[])
{
int x = 21;
do
{
system.out.println("value of x:" + x);
x++;
}
while (x < 20);
}
}
FOR LOOP
}
}
JUMP STATEMENTS
• break
• continue
• return
BREAK STATEMENT
class Break
{
public static void main(String args[])
{
for (int i = 0; i < 10; i++)
{
if (i == 5)
break;
System.out.println("i: " + i);
}
System.out.println("Loop complete.");
}
}
USING BREAK AS A FORM OF GOTO
if (t)
class BreakLabel break second;
{ System.out.println("This won't execute.");
public static void main(String args[]) }
{ System.out.println("This won't execute.");
boolean t = true; }
first:
{ System.out.println("This is after second block.");
second: }
{ }
third: }
{
if (t)
return;