Control Flow, as the name implies, control the flow of your codes if
some specific conditions are met. *Note that the flow of execution of
the program code is from top to bottom so starting from main() to
bottom.We are now starting to learn Logic. If you are good in logic, it
is no problem with you.
if ( codition is true ) { //this curly braces indicates start point
//execute codes that are inside.
} //this is end point
Try the code below in your program:
String msg = "Hello ";
if (msg == "Hello") {
System.out.print("condition is true");
}
Run your app. Notice that System.out.print(“condition is true”) is not executed because “Hello ” is not equal to “Hello”, notice that there is a space in the other one. Please change “Hello ” to “Hello” and try it.
if else conditional statements:
if ( condition is true ) {
//execute codes here
}
else { //if the condition above is false
//execute codes here
}
if else if conditional statements:
if ( condition is true ) {
//execute codes here
}
else if ( condition is true ){ //if the condition above is false and the condition here is true
//execute codes here
}
else { //you can also add another else statement here or else if statement
//execute codes here
}
In the condition, you may try if ( x<10), this is read if x less than 10 execute code here
You may also try (x <= 10) this is read if x less than or equal to ten execute codes here
These are some of the operators used:
== equal
<= less than or equal
>= greater than or equal
!= not equal
< less than
> greater than
Try to experiment!
There is also another way to control the flow of your program. Using the switch case statements. It also works in similar with if and else statements. Switch case statements are programmed this way.
switch ( variable_here ) {
Try this code!
Unlike if and else, you cannot do case (age<=3) in case statements but instead you can do it this way:
Next: Java Loops
if ( codition is true ) { //this curly braces indicates start point
//execute codes that are inside.
} //this is end point
Try the code below in your program:
String msg = "Hello ";
if (msg == "Hello") {
System.out.print("condition is true");
}
Run your app. Notice that System.out.print(“condition is true”) is not executed because “Hello ” is not equal to “Hello”, notice that there is a space in the other one. Please change “Hello ” to “Hello” and try it.
if else conditional statements:
if ( condition is true ) {
//execute codes here
}
else { //if the condition above is false
//execute codes here
}
if else if conditional statements:
if ( condition is true ) {
//execute codes here
}
else if ( condition is true ){ //if the condition above is false and the condition here is true
//execute codes here
}
else { //you can also add another else statement here or else if statement
//execute codes here
}
In the condition, you may try if ( x<10), this is read if x less than 10 execute code here
You may also try (x <= 10) this is read if x less than or equal to ten execute codes here
These are some of the operators used:
== equal
<= less than or equal
>= greater than or equal
!= not equal
< less than
> greater than
Try to experiment!
There is also another way to control the flow of your program. Using the switch case statements. It also works in similar with if and else statements. Switch case statements are programmed this way.
switch ( variable_here ) {
case value_to_check:
//this works like: if ( variable_here == value_to_check) then execute codes here
break; //break indicates the end of a case to check
case another_value_to_check:
break;
//note that you can have a series of case statements here.
default:
//if none of the values above are equal to the variable_here, then execute codes here
break;
}Try this code!
Unlike if and else, you cannot do case (age<=3) in case statements but instead you can do it this way:
how can i insert data into my database using java
ReplyDelete