Probably, you are familiar with C programming language. The basic programming constructs are almost similar to C programming language. If you are not familiar, don't worry these are not hard to understand.
Control Statements
Java allows you to control the flow of your program's execution based on conditions at the time of execution. Different types of control statements will be discussed in this section.
If-Else
Using if
statement, you can execute a block of code if certain conditions are true. You can also create two blocks of code, one of which will execute if the condition is true and another block will execute if the condition is false. For this type of scenario, you can use the if-else
statement.
Nested If-Else
In this programming language, you can also create a brunch of code blocks, which will be executed based on conditions. If you are taking some decisions based on certain conditions and you want to check some other conditions further, you can use nested blocks. You can use if-else
blocks within if
or else
as shown below.
If-Else-If block
This is also known as an if-else-if
ladder. These statements are executed in a top-down approach. If a condition is true, the respective block will be executed. Other blocks will not be executed. The following program depicts it clearly.
Switch Case
In Java, the switch
statement is used for multi-way branching. It is a good alternative to the if-else-if
ladder, though it has selective uses. It can not be used as an alternative to if-else-if
in every case.
This statement can not be used for conditions like x > 10
. It can not be used for a range of values. This statement works only for integer int
) and character (char
) data types.
Loop Control
As a programmer, sometimes you have to perform iterative tasks. Just for an example, if you want to calculate factorial of a number (say 10), you have to multiply all the integers starting from 1 to 10.
Java provides three types of iteration statements: for
, while
and do-while
. These are illustrated with sample code below.
For loop
A for loop contains, three segments separated by a semi-colon: initialization, condition checking, and increment or decrement operation. When a loop starts, initialization segment is executed. Generally, initialization segment contains loop control variable. The conditions are checked in every iteration including the first iteration. Generally, the last segment of the loop contains increment or decrement operation on loop control variable.
In this following example, we are going to print some integers within the range of 0 to 9.
Now, we are writing a program to calculate factorial of a number.
Another syntax of for loop will be discussed later.
While loop
This loop repeats a block of code while controlling expression is true. If the condition is false, the program control moves to the immediate next line of the loop. Let us try to write a code to print integers from 1 to 10.
Now, we are writing a program to calculate factorial of a number using while loop.
Do-While loop
This is a different form of while loop, where conditional expression is checked at the end of each iteration. The while
loop is entry controlled whereas do-while
is exit controlled.
Even if the condition is false at the beginning of the loop, the block will be executed once. Though the block of while
loop will not execute.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.