Sie sind auf Seite 1von 28

UNIT II

DECISION MAKING AND BRANCHING


CONTROL STATEMENTS

• 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 (boolean condition)


{
loop statements...
}
// java program to illustrate while loop
class WhileDemo
{
public static void main(string args[])
{
int x = 1;

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

for (initialization condition; testing condition; increment/decrement)


{
statement(s);
}
• Initialization condition: here, we initialize the variable in use. it marks the start of a for loop. an
already declared variable can be used or a variable can be declared, local to loop only.
• Testing condition: it is used for testing the exit condition for a loop. it must return a boolean value.
it is also an entry control loop as the condition is checked prior to the execution of the loop
statements.
• Statement execution: once the condition is evaluated to true, the statements in the loop body are
executed.
• Increment/ Decrement: it is used for updating the variable for next iteration.
• Loop termination: when the condition becomes false, the loop terminates marking the end of its life
cycle.
// java program to illustrate for loop.
class ForloopDemo
{
public static void main(string args[])
{
for (int x = 2; x <= 4; x++)
system.out.println("value of x:" + x);
}
}
ENHANCED FOR LOOP

for (t element:collection obj/array)


{
statement(s)
}
// java program to illustrate enhanced for loop
public class EnhancedfFor
{
public static void main(string args[])
{
string array[] = {"ron", "harry", "hermoine"};
for (string x:array) /* for loop for same function */

{ for (int i = 0; i < array.length; i++)


{
system.out.println(x); system.out.println(array[i]);
} }

}
}
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: }
{

System.out.println("Before the break statement");


CONTINUE STATEMENT
class Continue
{
public static void main(String args[])
{
for (int i = 0; i < 10; i++)
{
if (i%2 == 0)
continue;

System.out.print(i + " ");


}
}
}
RETURN STATEMENT
class Return
{
public static void main(String args[])
{
boolean t = true;
System.out.println("Before the return.");

if (t)
return;

// Compiler will bypass every statement


// after return
System.out.println("This won't execute.");
}
}

Das könnte Ihnen auch gefallen