Conditions and Loop
Conditions.....
Java has 3 types of conditional statements.
- If
- else
- else if
If the same condition is false, we use "else" to specify a block of code to be executed.
And then we can use "else if" to specify a new condition to test if the 1st condition is false.
The above conditions return only boolean values.(true/false)
eg :- class A{
if(true){
sout ("A"); }
else {
sout ("B"); }
}
eg :- class A{
if(false){
sout ("A"); }
else if{
sout ("B"); }
else {
sout ("C"); }
}
Loops.....
There are mainly 2 types of loops in Java.
- While
- For
1) For loop
syntax - for (statement 1 ; statement 2 ; statement 3) {
//code to be executed
}
eg :- print numbers 1 to 5
sout ( x );
}
2) While loop
we can use only one condition inside the parameter list of while.
syntax - while ( condition ) {
//code block to be executed
}
eg :- while (true) {
sout (A") ;
}
Incremental/Decremental Operators
- x++ (post increment)
- x-- (post decrement)
- ++x (pre increment)
- --x (pre decrement)
Comments
Post a Comment