The document discusses various ways to convert data types to strings in Java, as well as different types of flow control including sequential, conditional, and iterative. It provides examples of if/else statements, switch statements, and for, while, and do-while loops. Conversion methods covered include byte, short, int, long, float, double, and boolean to string.
2. • 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);
3. • 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);
4. In computer science, control flow (or alternatively, flow of control) refers to the
specification of the order in which the individual statements, instructions or function calls
of 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
5. • 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
6. • if else statement
Syntax:
if(condition)
{
// statements
}
else{
// statements
}
When condition evaluates to true then its corresponding statements of if block are
executed, otherwise statements of else block are executed
For example:
int a=7;
7. if(a%2==0)
System.out.print(“Number is even”);
else
System.out.print(“Number is odd”);
It will print: Number is odd as condition evaluates to false, so else part is executed
• else if statement
Syntax:
if(condition1)
{
// statements
}
else if(condition2){
// statements
}
…
else if(condition-n){
// statements
}
else{
//statements
}
9. • switch statement:
Syntax:
switch(variable) //variable can be int, char, string
{
case value1:
//statements
break;
case value2:
//statements
break;
case value-n:
//statements
break;
default:
//statements
}
10. 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
11. • for loop is an entry controlled loop
Syntax:
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 5
Here i is local to for loop only.
12. • while loop is an entry controlled loop
Syntax:
while(condition)
{
//statements
}
For example:
int i=1;
while(i<=5)
{
System.out.print(i+” “);
i++;
}
it will print: 1 2 3 4 5
13. • do while loop is an exit controlled loop
Syntax:
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