Control Structure
Example:int a;
for (a=0; a<=10; a++){
System.out.println("a = " +a); }
while loop:in while loop the statement(s) will be executed as long as Expression evaluates to true. If there is only a single statement inside the braces, you may omit the braces.Example: class counting{
public static void main (String args[]){
int i = 0;
while (i < =5){
System.out.println("count");
i = i + 1; }
}
}
do-while loop:The do-while statement is like the while statement, except that the associated block always gets executed at least once.
Syntax:do {
statement (s)
} while (Expression);
Example:public class MyClass {
public static void main(String[] args) {
int i = 0;
do {
System.out.println(i);
i++;
} while (i < 10);
}
}
if statement: The if statement is the simple form of control flow statement. It directs the program to execute a certain section of code if and only if the test evaluates to true. That is the if statement in Java is a test of any boolean expression.
Syntax:if (Expression) {
statement (s)
} else {
statement (s)
}
Example:public class MainClass {
public static void main(String[] args) {
int a = 3;
if (a > 3)
a++;
else
a = 3;
}
}
Example: class IfElseexamp {
public static void main(String[] args) {
int testscore = 66;
char grade;
if (testscore >= 90) {
grade = 'A';
} else if (testscore >= 80) {
grade = 'B';
} else if (testscore >= 70) {
grade = 'C';
} else if (testscore >= 60) {
grade = 'D';
} else {
grade = 'F';
}
System.out.println("Grade = " + grade);
}
}
The output will be
D
The Switch Statement: The Switch Statement is an alternative to a series of else if is the switch statement. Unlike if-then and if-then-else, the switch statement allows for any number of possible execution paths. A switch works with the byte, short, char, and int primitive data types. It also works with enumerated types. The body of a switch statement is known as a switch block. Any statement immediately contained by the switch block may be labeled with one or more case or default labels. The switch statement evaluates its expression and executes the appropriate case.
Switch Statement Syntax:switch (expression) {
case value_1 :
statement (s);
break;
case value_2 :
statement (s);
break;
.
.
.
case value_n :
statement (s);
break;
default:
statement (s);
}
Example: class Switchexamp {
public static void main(String[] args) {
int month = 5;
switch (month) {
case 1: System.out.println("January");
break;
case 2: System.out.println("February");
break;
case 3: System.out.println("March");
break;
case 4: System.out.println("April");
break;
case 5: System.out.println("May");
break;
case 6: System.out.println("June");
break;
case 7: System.out.println("July");
break;
case 8: System.out.println("August");
break;
case 9: System.out.println("September");
break;
case 10: System.out.println("October");
break;
case 11: System.out.println("November");
break;
case 12: System.out.println("December");
break;
default: System.out.println("Wrong Input");
break;
}
}
}
The output will be
May
ไม่มีความคิดเห็น:
แสดงความคิดเห็น