Control Flow
Previous | Home | Next |
A program is a group of statements that are executed to achieve a task.Statements are executed in sequential form inside a program which are called sequential execution or sequential flow. however the normal flow of program can be controlled by putting decision making statements in the program.Statements that controlled the flow of program are called control flow statements.
java control statements are categories in to three java selection,java iteration and java jump statements.
Selection statements are: java support two selection statements if and switch case.
Iteration statements are: java support two iteration statements for while and do-while
Jump statements are : java support three jump statements break,continue, and return.
Selection statements
1.IF
if(condition)
{ statement;}
Else if
if(condition)
{ statement;}
else if (condition)
{ statement;}
else if(condition)
{statement}
.......
2.Switch case
switch(expression){
case 1:value
statement sequence
break;
case 2
statement sequence
break;
default;
default statement sequence;
}
Iteration statements
1.For
for( initialization;condition;iteration)
{body}
2.While
while(condition)
{
body of loop}
3 do while
do
{
body of loop
}
while(condition);
Jump statements
1. Break
is used to exit the loop
if(i==9)
break; // loop terminates over here
2.continue
continue statement make continue flow of loop.
for(i=0;i<=18;i++)
{
system.out.println(i+"");
}
if(i%6==0)
continue;
if statement
public static void main(String arg[])
{
int x=5;
int y=7;
if(x>y)
{
System.out.println(x is greater than y);
}
}
if statement
public static void main(String arg[])
{
int x=5;
int y=7;
if(x>y)
{
System.out.println(x is greater than y);
}
}
If else statementint a= 6;
if(a%2==0)
{
System .out.println("This is a even number");
}
else
{
System .out.println ("This is not a even no or it is a odd number)
}
Switch Case
int month = 2;
switch(month)
{
Case 1:
System.out.println("january");
break;
Case 2:
System .out.println("february");
break;
Case:3
System.out.prinln("march");
break;
default:Syatem.out.println("invalid value");
break;
}
For statement
public static void main(String arg[])
for(a=0;a<=10;a++)
{
system .out.println("the number is:"+a);
}
While statementpublic Static void main(arg[])
while(j<=10)
{
System .out.println("the number is:"+j);
j++;
}
do while Statementpublic static void main(String arg[])
{
int i=0;
do
{
System.out.println("the number is :"+i);
}
while(i<=10);
Break statementsint num []={20,5,36,23,56};
int search=23;
for(int i=1 ;int i<= num.length;i++)
{
if(num[i]==search)
{
System.out.println( "the value is found");break;}
}
Continue statementint num []={20,5,36,23,56};
int search=23;
for(int i=1 ;int i<= num.length;i++)
{
if(num[i]1==search)
{
continue;
}
if (found==search)
{
System.out.println( "the value is found");break;}
}
Previous | Home | Next |