Unit 3 Control Statements (Tapashi) Edited
Unit 3 Control Statements (Tapashi) Edited
UNIT STRCUTURE
3.1 Learning Objectives
3.2 Introduction
3.3.2 Looping
3.2 INTRODUCTION
if (boolean_expression)
{
statement-block ;
}
statement;
if (percentage>=40)
{
System.out.println(“Pass“);
}
if ( Boolean_expression )
statement; //executes when the expression is true
else
statement; //executes when the expression is false
The switch statement : We have seen that when one of the many
alternatives is to be selected, we can design a program using if
statements to control the selection. However, the program becomes
difficult to read and follow when the number of alternatives increases.
Like C/C++, JAVA has a built-in multiway decision statement known
as a switch.
The switch statement provides variable entry points to a block.
It tests the value of a given variable or expression against a list of
case values and when a match is found, a block of statements
associated with that case is executed. The general form of switch
statement is as follows :
switch( expression )
{
case value : statements;
break;
case value : statements
break;
...
default : statements //optional default section
break;
}
The expression is evaluated and compared in turn with each
value prefaced by the case keyword. The values must be constants
(i.e., determinable at compile-time) and may be of type byte, char,
short, int, or long.
3.3.2 Looping
Ø while
Ø do-while
Ø for
while(expression)
{
statement(s);
}
do
{
statement(s);
}while (expression);
{
public static void main(String args[])
{
System.out.println("0\n1");
int n0=0,n1=1,n2=1;
while(n2<50)
{
System.out.println(n2);
n0=n1;
n1=n2;
n2=n1+n0;
}
System.out.println(n2);
}
}
The output of the above program will be the Fibonacci series
between 0 to 50 (i.e., 0 1 1 2 3 5 8 13 21 34).
The break statement : In Java, the break statements has two forms:
unlabelled and labelled. We have seen the unlabelled form of the
break statement used with switch earlier. As noted there, an
unlabelled break terminates the enclosing switch statement, and the
flow of control transfers to the statement immediately following the
switch. It can be used to terminate a for, while, or do-while loop. A
break (unlabelled form) statement, causes an immediate jump out
of a loop to the first statement after its end. When the break
statement is encountered inside a loop, the loop is immediately exited
and the program continues with the statement immediately following
the loop. When the loop is nested, the break would only exit from the
loop containing it. This means, the break will exit only a single loop.
continue ;
break;
for( int j=1;j<100;j++)
{
System.out.print("* ");
if(j==i)
continue outer;//labelled continue
}
}
}
}
The output of the above program will be like this:
return sum;
The data type of the value returned by return must match the
type of the method’s declared return value. When a method is
declared void, use the form of return that doesnot return a value:
return;