Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Control Statements

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 35

CONTROL STATEMENTS

CONTROL STATEMENTS
• FLOW OF CONTROL : CONTROL THE ORDER OF EXECUTION
• CONTROL STRUCTURE : STATEMENT WHICH USED TO ALTER THE
NORMAL SEQUENTIAL FLOW
CATEGORIES OF CONTROL
STATEMENTS
• SELECTION OR DECISION MAKING STATEMENT :
1. if statement
2. switch statement
• ITERATION OR LOOPING STATEMENT
1. for loop
2. while loop
3. do-while loop
• JUMP STATEMENT : break,continue,return
The if and if-else Statements
(cont.)

Figure 4-1: Flowcharts for the if and if-else statements


Fundamentals of Java 4
The if and if-else Statements

• Principal forms:

5 Fundamentals of Java
The if and if-else Statements
(cont.)
• Additional forms:

6 Fundamentals of Java
The if and if-else Statements
(cont.)
• Better to over-use braces than under-use them
• Can help to eliminate logic errors
• Condition of an if statement must be a Boolean
expression
• Evaluates to true or false
• A flowchart can be used to illustrate the behavior
of if-else statements.

Fundamentals of Java 7
The if and if-else Statements
(cont.)
• Examples of if statements:

8 Fundamentals of Java
NESTED – IF STATEMENT
If (expression 1)
{
if (expression 2)
statement 1;
else
statement 2;
}
Else
{
if (expression 3)
statement 3;
else
statement 4;
}
If (expression 1)
{
statement 1;
}
Else if (expression 2)
{
statement 2;
}
Else if (expression 3)
{
statement 3;
}
The while Statement

• Provides a looping mechanism


• Executes statements repeatedly for as long as some
condition remains true

11 Fundamentals of Java
The while Statement (cont.)

Figure 4-2: Flowchart for a while statement

Fundamentals of Java 12
The while Statement (cont.)

• Common structure of a while loop:

13 Fundamentals of Java
The while Statement (cont.)

• Example:

 Counter-controlled loop:
– cntr is the counter
– Loop repeats a determined number of times
14 Fundamentals of Java
The while Statement (cont.)

• Counting backward:

15 Fundamentals of Java
The while Statement (cont.)

• Task-controlled loop: Continues to execute until


some task is accomplished

16 Fundamentals of Java
The Do/While Loop
A do...while loop is similar to a while loop, except that a do...while
loop is guaranteed to execute at least one time.

do
{ // code block to be executed }
while (condition);
int i = 0;
int i = 0; while (i < 5)
do {
{ System.out.println(i);
System.out.println(i); }
i++; i++;
} while (i < 5);
Java Switch Statement

• The Java switch statement executes one statement from multiple


conditions.
• It is like if-else-if ladder statement.
• The switch statement works with byte, short, int, long, enum types,
String and some wrapper types like Byte, Short, Int, and Long.
Java Switch Statement
• There can be one or N number of case values for a switch expression.
• The case value must be of switch expression type only. The case value must
be literal or constant. It doesn't allow variables.
• The case values must be unique. In case of duplicate value, it renders compile-
time error.
• The Java switch expression must be of byte, short, int, long (with its Wrapper
type), enums and string.
• Each case statement can have a break statement which is optional. When
control reaches to the break statement, it jumps the control after the switch
expression. If a break statement is not found, it executes the next case.
• The case value can have a default label which is optional.
SWITCH : SYNTAX
• switch(expression){
• case value1:
• //code to be executed;
• break; //optional
• case value2:
• //code to be executed;
• break; //optional
• ......

• default:
• code to be executed if all cases are not matched;
• }
class SwitchExample {
public static void main(String[] args) {
int number=20; //Declaring a variable for switch expression
switch(number){ //Switch expression
case 10: System.out.println("10"); //Case statements
break;
case 20: System.out.println("20");
break;
case 30: System.out.println("30");
break;
default:System.out.println("Not in 10, 20 or 30"); //Default case statement
}
}
• }
int day = 4;
switch (day)
{
case 6:
System.out.println("Today is Saturday");
break;
case 7:
System.out.println("Today is Sunday");
break;
default:
("Looking forward to the Weekend");
}
Java For Loop

• The Java for loop is used to iterate a part of the program several
times. If the number of iteration is fixed, it is recommended to use for
loop.
• There are three types of for loops in java.
1. Simple For Loop
2. For-each or Enhanced For Loop
3. Labeled For Loop
Java Simple For Loop

• A simple for loop is the same as C/C++. We can initialize the variable,
check condition and increment/decrement value.

for(initialization;condition;incr/decr){
//statement or code to be executed
}
EXAMPLE
public class ForExample {
public static void main(String[] args) {
//Code of Java for loop
for(int i=1;i<=10;i++){
System.out.println(i);
}
}
}
Java Nested For Loop
public class NestedForExample {
public static void main(String[] args) {
//loop of i
for(int i=1;i<=3;i++){
//loop of j
for(int j=1;j<=3;j++){
System.out.println(i+" "+j);
}//end of i
}//end of j
}
}
Java for-each Loop

• The for-each loop is used to traverse array or collection in java. It is


easier to use than simple for loop because we don't need to
increment value and use subscript notation.
for(Type var:array){
//code to be executed
}
EXAMPLE
public class ForEachExample {
public static void main(String[] args) {
//Declaring an array
int arr[]={12,23,44,56,78};
//Printing array using for-each loop
for(int i:arr){
System.out.println(i);
}
}
}
Java Labeled For Loop

labelname:
for(initialization;condition;incr/decr){
//code to be executed
}
EXAMPLE
public class LabeledForExample {
public static void main(String[] args) {
aa: //Using Label for outer and for loop
for(int i=1;i<=3;i++){
bb:
for(int j=1;j<=3;j++){
if(i==2&&j==2){
break aa;
}
System.out.println(i+" "+j);
}
}
}
}
Java Infinitive For Loop

for(;;){
//code to be executed
}
BREAK STATMENT
• The break statement in Java programming language has the following
two usages −
• When the break statement is encountered inside a loop, the loop is
immediately terminated and the program control resumes at the next
statement following the loop.
• It can be used to terminate a case in the switch statement
EXAMPLE

public class Test {


public static void main(String args[])
{
int [] numbers = {10, 20, 30, 40, 50};
for(int x : numbers )
{
if( x == 30 )
{

break;
}
System.out.print( x );
System.out.print("\n");
} } }
CONTINUE STATMENT
public class Test {
public static void main(String args[])
{
int [] numbers = {10, 20, 30, 40, 50};
for(int x : numbers )
{
if( x == 30 )
{

continue;
}
System.out.print( x );
System.out.print("\n");
} } }

You might also like