public class ControlStatements {
/**
* @param args
*/
public static void main(String[] args) {
String name4 = "Anusha";
String name1 = "Munna";
String name2 = "Krishna";
String name3 = "Chandu";
int i = 10;
int k = 3;
int a[] = new int[] { 100, 200, 300, 400 };
String c[] = new String[] { "Anusha", "Munna", "Krishna", "Bobby",
"Chandu" };
ControlStatements controlStatements = new ControlStatements();
// here i value is 10. So condition is true.
controlStatements.testIFandIFElse(i);
// here k value is 5. So condition is false.
controlStatements.testIFandIFElse(k);
// while loop in whileTest() is execute 5 times.
controlStatements.whileTest(i);
// statement executes 5 times.
controlStatements.doWhileTest(i);
// Here condition failed but statement executes 1 times because by
// default first time with out checking condition it is called.
controlStatements.doWhileTest1(k);
// for loop executed 4 times.
controlStatements.forLooptest(a);
// enhanced for loop executed 4 times.
controlStatements.enhancedForLooptest(a);
// enhanced for loop executed 5 times.
controlStatements.enhancedForLooptest(c);
controlStatements.switchCaseTest(name1);
controlStatements.switchCaseTest(name2);
controlStatements.switchCaseTest(name3);
controlStatements.switchCaseTest(name4);
controlStatements.switchCaseTest("Bobby");// default called
// returnedValueTest() method returns the string value. So here I have
// assigned to String as follows.
String fName = controlStatements.returnedValueTest(name4);
System.out.println(fName);
controlStatements.returnedValueTest1();
// breaks the for loop when match found.
controlStatements.breakTest(name4);
controlStatements.labeledBreakTest(name4);
controlStatements.continueTest();
controlStatements.labeledContinueTest();
}
// =======================================
public void testIFandIFElse(int j) {
if (j > 5) {
System.out.println("called when condition is true");
} else {
System.out.println("called when condition is failed");
}
}
// ========================================
public void whileTest(int m) {
while (m < 15) {
System.out.println("called while loop 5 times" + m);
m++;
}
}
// ===================================
public void doWhileTest(int m) {
do {
System.out.println("called do while loop 5 time" + m);
m++;
} while (m < 15);
}
// =====================================
public void doWhileTest1(int m) {
// statement is executed one time even while condition failes
do {
System.out.println("called do while loop 1 time" + m);
m++;
} while (m < 2);
}
// ======================================
public void forLooptest(int[] m) {
for (int i = 0; i < m.length; i++) {
System.out.println("called for loop 4 times" + m[i]);
}
}
// ====================================
public void enhancedForLooptest(int[] m) {
// no need to write initialization and increment in enhanced for loop.
// Here array is of type integers. so i have initialized int variable i.
for (int i : m) {
System.out.println("called enhanced for loop 4 times" + i);
}
}
// ====================================
public void enhancedForLooptest(String[] m) {
// no need to write initialization and increment in enhanced for loop.
// Here array is of type Strings. so i have initialized String variable i.
for (String i : m) {
System.out.println("called enhanced for loop 5 times" + i);
}
}
// =========================================
public void switchCaseTest(String name) {
switch (name) {
case "Anusha":
System.out.println("called when name is Anusha");
break;
case "Munna":
System.out.println("called when name is Munna");
break;
case "Krishna":
System.out.println("called when name is Krishna");
break;
case "Chandu":
System.out.println("called when name is Chandu");
break;
default:
System.out.println("default called when above cases failed");
break;
}
}
// =====================================
/**
* The unlabeled version of the break statement is used when we
* want to jump out of a single loop or single case in switch statement.
* Labeled version of the break statement is used when we want to jump
* out of nested or multiple loops.
*
* @param name
*/
public void breakTest(String name) {
String[] s = new String[] { "Test1", "Anusha", "Test3", "Test4" };
for (int i = 0; i < s.length; i++) {
if (name.equalsIgnoreCase(s[i])) {
System.out.println("match found at " + i);
// break the for loop if match found. 2nd iteration match found
// and breaks the for loop.
break;
}
}
System.out.println("called when for loop breaks");
}
// ============================================
/**
* The unlabeled version of the break statement is used when we
* want to jump out of a single loop or single case in switch statement.
* Labeled version of the break statement is used when we want to jump
* out of nested or multiple loops.
*
* @param name
*/
public void labeledBreakTest(String name) {
String[] s = new String[] { "Test1", "Anusha", "Test3", "Test4" };
int[] s1 = new int[] { 1, 2, 3, 4, 5 };
Outer: for (int j = 0; j < s1.length; j++) {
for (int i = 0; i < s.length; i++) {
if (name.equalsIgnoreCase(s[i])) {
System.out.println("match found at " + i + " and name is "
+ s[i]);
// break the for loop if match found. 2nd iteration match
// found and breaks the outer for loop.
break Outer;
}
}
}
System.out.println("this is called when outer for loop breaks");
}
// ============================================
public void continueTest() {
String name = "This is continue test string";
int length = name.length();
int nums = 0;
for (int i = 0; i < length; i++) {
if (name.charAt(i) != 's') {
// this will continue the for loop if character at ith position
// in name is not equals to 's'.
// IF condition is true then nums++ is not executed because it
// continues the for loop.
// if condition failes(means characther at ith position is 's')
// then nums++ is called.
continue;
}
nums++;
}
System.out.println("found" + nums + "number of s in name");
}
// ==========================================
public void labeledContinueTest() {
String name = "This is continue test string";
int length = name.length();
int nums = 0;
Outer: for (int j = 0; j < length; j++) {
while (nums < 4) {
if (name.charAt(j) != 's') {
// this will continue the for loop if character at ith
// position in name is not equals to 's'.
// IF condition is true then nums++ is not executed because
// it continues the outer for loop.
// if condition failes(means characther at ith position is 's')
// then nums++ is called.
continue Outer;
}
nums++;
}
}
System.out.println("found" + nums + "number of s in name");
}
// =========================================
public String returnedValueTest(String Fname) {
String name = "Krishna";
String FullName = name + Fname;
// It returns full name string value.
return FullName;
}
// =========================================
public void returnedValueTest1() {
// Example of return;
// It does not return any value as return type is void. In this case we
// can use return
System.out.println("It does not return any value.");
return;
}
// ==========================================
/**
* @param args
*/
public static void main(String[] args) {
String name4 = "Anusha";
String name1 = "Munna";
String name2 = "Krishna";
String name3 = "Chandu";
int i = 10;
int k = 3;
int a[] = new int[] { 100, 200, 300, 400 };
String c[] = new String[] { "Anusha", "Munna", "Krishna", "Bobby",
"Chandu" };
ControlStatements controlStatements = new ControlStatements();
// here i value is 10. So condition is true.
controlStatements.testIFandIFElse(i);
// here k value is 5. So condition is false.
controlStatements.testIFandIFElse(k);
// while loop in whileTest() is execute 5 times.
controlStatements.whileTest(i);
// statement executes 5 times.
controlStatements.doWhileTest(i);
// Here condition failed but statement executes 1 times because by
// default first time with out checking condition it is called.
controlStatements.doWhileTest1(k);
// for loop executed 4 times.
controlStatements.forLooptest(a);
// enhanced for loop executed 4 times.
controlStatements.enhancedForLooptest(a);
// enhanced for loop executed 5 times.
controlStatements.enhancedForLooptest(c);
controlStatements.switchCaseTest(name1);
controlStatements.switchCaseTest(name2);
controlStatements.switchCaseTest(name3);
controlStatements.switchCaseTest(name4);
controlStatements.switchCaseTest("Bobby");// default called
// returnedValueTest() method returns the string value. So here I have
// assigned to String as follows.
String fName = controlStatements.returnedValueTest(name4);
System.out.println(fName);
controlStatements.returnedValueTest1();
// breaks the for loop when match found.
controlStatements.breakTest(name4);
controlStatements.labeledBreakTest(name4);
controlStatements.continueTest();
controlStatements.labeledContinueTest();
}
// =======================================
public void testIFandIFElse(int j) {
if (j > 5) {
System.out.println("called when condition is true");
} else {
System.out.println("called when condition is failed");
}
}
// ========================================
public void whileTest(int m) {
while (m < 15) {
System.out.println("called while loop 5 times" + m);
m++;
}
}
// ===================================
public void doWhileTest(int m) {
do {
System.out.println("called do while loop 5 time" + m);
m++;
} while (m < 15);
}
// =====================================
public void doWhileTest1(int m) {
// statement is executed one time even while condition failes
do {
System.out.println("called do while loop 1 time" + m);
m++;
} while (m < 2);
}
// ======================================
public void forLooptest(int[] m) {
for (int i = 0; i < m.length; i++) {
System.out.println("called for loop 4 times" + m[i]);
}
}
// ====================================
public void enhancedForLooptest(int[] m) {
// no need to write initialization and increment in enhanced for loop.
// Here array is of type integers. so i have initialized int variable i.
for (int i : m) {
System.out.println("called enhanced for loop 4 times" + i);
}
}
// ====================================
public void enhancedForLooptest(String[] m) {
// no need to write initialization and increment in enhanced for loop.
// Here array is of type Strings. so i have initialized String variable i.
for (String i : m) {
System.out.println("called enhanced for loop 5 times" + i);
}
}
// =========================================
public void switchCaseTest(String name) {
switch (name) {
case "Anusha":
System.out.println("called when name is Anusha");
break;
case "Munna":
System.out.println("called when name is Munna");
break;
case "Krishna":
System.out.println("called when name is Krishna");
break;
case "Chandu":
System.out.println("called when name is Chandu");
break;
default:
System.out.println("default called when above cases failed");
break;
}
}
// =====================================
/**
* The unlabeled version of the break statement is used when we
* want to jump out of a single loop or single case in switch statement.
* Labeled version of the break statement is used when we want to jump
* out of nested or multiple loops.
*
* @param name
*/
public void breakTest(String name) {
String[] s = new String[] { "Test1", "Anusha", "Test3", "Test4" };
for (int i = 0; i < s.length; i++) {
if (name.equalsIgnoreCase(s[i])) {
System.out.println("match found at " + i);
// break the for loop if match found. 2nd iteration match found
// and breaks the for loop.
break;
}
}
System.out.println("called when for loop breaks");
}
// ============================================
/**
* The unlabeled version of the break statement is used when we
* want to jump out of a single loop or single case in switch statement.
* Labeled version of the break statement is used when we want to jump
* out of nested or multiple loops.
*
* @param name
*/
public void labeledBreakTest(String name) {
String[] s = new String[] { "Test1", "Anusha", "Test3", "Test4" };
int[] s1 = new int[] { 1, 2, 3, 4, 5 };
Outer: for (int j = 0; j < s1.length; j++) {
for (int i = 0; i < s.length; i++) {
if (name.equalsIgnoreCase(s[i])) {
System.out.println("match found at " + i + " and name is "
+ s[i]);
// break the for loop if match found. 2nd iteration match
// found and breaks the outer for loop.
break Outer;
}
}
}
System.out.println("this is called when outer for loop breaks");
}
// ============================================
public void continueTest() {
String name = "This is continue test string";
int length = name.length();
int nums = 0;
for (int i = 0; i < length; i++) {
if (name.charAt(i) != 's') {
// this will continue the for loop if character at ith position
// in name is not equals to 's'.
// IF condition is true then nums++ is not executed because it
// continues the for loop.
// if condition failes(means characther at ith position is 's')
// then nums++ is called.
continue;
}
nums++;
}
System.out.println("found" + nums + "number of s in name");
}
// ==========================================
public void labeledContinueTest() {
String name = "This is continue test string";
int length = name.length();
int nums = 0;
Outer: for (int j = 0; j < length; j++) {
while (nums < 4) {
if (name.charAt(j) != 's') {
// this will continue the for loop if character at ith
// position in name is not equals to 's'.
// IF condition is true then nums++ is not executed because
// it continues the outer for loop.
// if condition failes(means characther at ith position is 's')
// then nums++ is called.
continue Outer;
}
nums++;
}
}
System.out.println("found" + nums + "number of s in name");
}
// =========================================
public String returnedValueTest(String Fname) {
String name = "Krishna";
String FullName = name + Fname;
// It returns full name string value.
return FullName;
}
// =========================================
public void returnedValueTest1() {
// Example of return;
// It does not return any value as return type is void. In this case we
// can use return
System.out.println("It does not return any value.");
return;
}
// ==========================================
}
No comments:
Post a Comment