Basic Programming Constructs in Java - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Community

Basic Programming Constructs in Java

Share This

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.


public class TestMain { public static void main(String[] args) { int x = 50; if (x < 10) { System.out.println("x is less than 10"); } else { System.out.println("x is greater than 10"); } } }

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.


public class TestMain { public static void main(String[] args) { int x = 50; int y = 100; int z = 20; if (z < x && z < y) { System.out.println("z is lesser than x and y"); } else { if (z > x && z > y) System.out.println("z is greater than x and y"); else if (z > x && z < y) { System.out.println("z is greater than x and lesser than y"); } } } }

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.


public class TestMain { public static void main(String[] args) { int x = 50; int y = 100; int z = 20; if (z < x && z < y) { System.out.println("z is lesser than x and y"); } else if (z > x && z > y) System.out.println("z is greater than x and y"); else if (z > x && z < y) { System.out.println("z is greater than x and lesser than y"); } } }

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.



public class TestMain { public static void main(String[] args) { char grade = 'c'; switch (grade) { case 'a': System.out.println("Range of marks is 90 to 100"); break; case 'b': System.out.println("Range of marks is 80 to 89"); break; case 'c': System.out.println("Range of marks is 70 to 79"); break; case 'd': System.out.println("Range of marks is 60 to 69"); break; case 'e': System.out.println("Range of marks is 50 to 59"); break; default: System.out.println("Wrong choice"); } } }

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.



public class TestMain { public static void main(String[] args) { for (int i = 0; i < 10; i++) { System.out.println(i); } } }

Now, we are writing a program to calculate factorial of a number.



public class TestMain { public static void main(String[] args) { int fact = 1; int n = 5; for (int i = 2; i <= n; i++) { fact = fact * i; } System.out.println("The factorial of " + n + " is " + fact); } }

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.



public class TestMain { public static void main(String[] args) { int i = 1; while (i <= 10) { System.out.println(i); i++; } } }

Now, we are writing a program to calculate factorial of a number using while loop.


public class TestMain { public static void main(String[] args) { int fact = 1; int n = 5; int i = 2; while (i <= n) { fact = fact * i; i++; } System.out.println("The factorial of " + n + " is " + fact); } }

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.


public class TestMain { public static void main(String[] args) { int i = 0; do { System.out.println(i); i++; } while (i < 10); } }


Happy Exploring!

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.