learn java part 5

13
Conversion to String, Flow of Control: Sequential, Conditional, Iterative

Upload: gurpreet-singh

Post on 28-Jul-2015

196 views

Category:

Engineering


3 download

TRANSCRIPT

Conversion to String, Flow of Control: Sequential, Conditional, Iterative

• Converting byte to String:byte a=100;String s=Byte.toString(a);

• Converting short to String:short a=1000;String s=Short.toString(a);

• Converting int to String:int a=10000;String s=Integer.toString(a);

• Converting long to String:long a=100000;String s=Long.toString(a);

• Converting float to String:float a=10.156F;String s=Float.toString(a);

• Converting double to String:double a=10.156;String s=Double.toString(a);

• Converting Boolean to String:boolean a=true;String s=Boolean.toString(a);

In computer science, control flow (or alternatively, flow of control) refers to thespecification of the order in which the individual statements, instructions or function callsof an imperative program are executed or evaluated.

There are three types of Flow of Control:• Sequential- Statements executes one after the other• Conditional- if condition satisfies, then only the statement executes• Iterative- statements execute more than once

• if statement:Syntax:if(condition){//statements

}

When condition evaluates to true then its corresponding statements are executed

For example:

int a=4;if(a%2==0)System.out.print(“Number is even”);

It will print: Number is even as condition is true

• if else statementSyntax:if(condition){// statements

}else{// statements}

When condition evaluates to true then its corresponding statements of if block areexecuted, otherwise statements of else block are executed

For example:

int a=7;

if(a%2==0)System.out.print(“Number is even”);

elseSystem.out.print(“Number is odd”);

It will print: Number is odd as condition evaluates to false, so else part is executed

• else if statementSyntax:if(condition1){// statements

}else if(condition2){// statements}…else if(condition-n){// statements}else{//statements}

For example:

int a=55;

if(a>=80&&a<=100)System.out.print(“Grade A”);

else if(a>=60&&a<80)System.out.print(“Grade B”);

else if(a>=40&&a<60)System.out.print(“Grade C”);

elseSystem.out.print(“Grade D”);

It will print: Grade C

• switch statement:Syntax:switch(variable) //variable can be int, char, string{case value1://statementsbreak;

case value2://statementsbreak;

case value-n://statementsbreak;

default://statements

}

For example:

int a=5;switch(a){case 1:System.out.print(“Hello”);break;

case 2:System.out.print(“Hi”);break;

default:System.out.print(“Default”);break;}

it will print: Default

• for loop is an entry controlled loopSyntax:for(initialisation;condition;increment/decrement){

//statements}

For example:

for(int i=1;i<=5;i++)System.out.print(i+” “);

it will print: 1 2 3 4 5Here i is local to for loop only.

• while loop is an entry controlled loopSyntax:while(condition){

//statements}

For example:int i=1;while(i<=5){System.out.print(i+” “);i++;

}it will print: 1 2 3 4 5

• do while loop is an exit controlled loopSyntax:do{

//statements} while(condition);

For example:int i=1;do{System.out.print(i+” “);i++;

} while(i<=5);

it will print: 1 2 3 4 5

do while will executes at least once even if the condition evaluates to false