Control statements
Statement similar to smallest unit which completes instruction in itself. So, every statement will get end with semicolon(;) in java.In java there are majorly sequential and control statements are available. if, while, do-while, for, switch, break, continue, return statement will come under the Control statements.
1. sequential statements: These are the statements that executes one by one.
1 2 3 4 5 | System.out.println("Hello"); x = y+z; System.out.println(x); |
These statements can be executed by JVM one by one in a sequential manner, that are called sequential statements. But this type of sequential execution is useful only to write simple programs . If we want to write better and complex programs, we need better control on the flow of execution. This is possible by using conditional statements.
2. control statements: Control statements are the statements which alter the flow of execution and provide better control to the programmer on the flow of execution. They are useful to write better and complex programs. The control statements in java are,
if condition
This statement is
used to perform a task depending on whether a given condition true or
false. Here, task represents a single statement or group of statements.
Syntax:
Simple if condition:
1 2 3 | if(condition){ statements; } |
Here, above one represents the simple if condition. if condition is true then statement is executed. if condition is false statements will never executed.
if-else condition:
1 2 3 4 5 6 | if(condition1) statement1; else if(condition2) statement2; else statement3; |
Here,
condition1 is placed inside the small braces(), which will call as method braces in java. If condition1 is true
statement1 is executed otherwise it verifies the else if() condition. If condition2 is true statement2 is executed, otherwise finally else statement3 will get execute.
Ex: Write a program to test a number is negative or positive?
steps:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | package com.javatbrains.basic; public class IfCondition { public static void main(String... args) { int num = -3; if (num == 0) { System.out.println(num + " is equals to zero"); } else if (num > 0) { System.out.println(num + " is greater than zero"); } else { System.out.println(num + " is lessthan zero"); } } } Output: -3 is lessthan zero |
steps:
Above example represents one if condition with multiple else conditions. In these program we have taken one int type variable and we have initialised negative value to that variable.
int num = -3;
After we have created one if statement and we have checking that condition(num==0) into that statement. If that statement is true, control comes into next line System.out.println(num+" is zero") and print that output. That condition in wrong, control comes into the else(num>0) statement and checks that inside of the else condition. That is true, that executes the next System.out.println(num+" is positive").
If else statement also false, then control comes into the last statement to print.
System.out.println(num+" is negative");
The below one also one more scenario to use multiple if statements.
Nested if condition:
1 2 3 4 5 6 7 8 | if(condition1) statement1; else if(condition2) statement2; else if(condition3) statement3; else statement4; |
while loop
The functioning of
this loop is also similar to do..while loop. This loop repeats a group
of statements as long as a condition is true. Once the condition is
false, the loop is terminated.
Syntax:
1 2 3 | while(condition){ statements; } |
Ex: Let us write a previous program using while loop
1 2 3 4 5 6 7 8 9 | public class WhileLoop{ public static void main(String[] args){ int i=1; //Initializing i value as 1 while(i<=10){ System.out.println(i); //printing the i value i++; //Increment i value } //Closing the while loop } } |
Here you get a doubt, what is the difference between while and do..while loop?
do..while
loop executes statements before checking the condition, at first time.
From the second time only condition is observed. This means the
programmer does not have control right from the beginning of its
execution.
In a while loop, condition is checked first and then only the statements are executed. This means it provides better control right from the beginning. Hence, while loop is more efficient than do..while loop.
do-while loop
do-while loop is used to
when there is a need to repeatedly execute a group of statements as long
as a condition is true. If the condition is false, the repetition will
be stopped and the flow of execution comes out of do..while loop.
Syntax:
1 2 3 | do{ statement; }while(condition); |
While using the do-while() loop, when the loop is going to execute first time it will not check the condition. So, Simply it will execute loop first time without checking the condition. When it will come to second time execution the time only that will check the condition. If that condition is true, that will execute that many times, until loop get false.
Ex: Write a program to display numbers from 1 to 10 by using do.. while loop?
For loop
1 2 3 4 5 6 7 8 9 | public class DoWhile{ public static void main(String[] args){ int i=1; do{ System.out.println(i); i++; }while(i<=10); } } |
For loop
The for loop is also
same as do..while or while loop, but it is more compact syntactically.
The for loop executes a group of statements as long as condition is
true.
Syntax:
1 2 3 | for(declaration;condition;increment/decrement){ statements; } |
Note: declaration, condition and increment/decrements in for loop is separated by using semi colon(;), To understand the preceding example,
1 2 3 | for(int i=0;i<=10;i++){ System.out.println(i); } |
compare the above expression with below program for loop. The declaration represents an initialization (int i=0) in the for loop. Condition represents the conditional expression(i<=10), as long as this condition is true, the statements inside for loop are executed. The increment/decrements is a modifying expression(i++). It may increment(i++) or decrements(i--) the value of variable.
Ex: Write a program to display the numbers form 1 to 10?
for loop also write these ways, Let us write the same for loop with outside initialization.
Let us write the same for loop without declaring any increment or decrements inside the for loop
Let us write the same for loop without any condition
Observe here, there is no condition in the for loop that tells where to stop. So, the preceding code executes without stoppage. It is called an infinite loop. Infinite loops are drawbacks in a program because when the user is caught in an infinite loop, he would be perplexed and could not understand how to come out of it. So, it is the duty of the programmer to see not to form the infinite loops. By chance if the programmer got an infinite loop, he should break it when the condition is reached. For this purpose‘break’ statement can be used.
1 2 3 4 5 6 7 | public class ForLoop{ public static void main(String[] args){ for(int i=0;i<=10;i++){ System.out.println(i); } } } |
for loop also write these ways, Let us write the same for loop with outside initialization.
1 2 3 4 | int i=0; for(;i<=10;i++){ System.out.println(i); } |
Let us write the same for loop without declaring any increment or decrements inside the for loop
1 2 3 4 5 | int i=0; for(;i<=10;){ System.out.println(i); i++; } |
Let us write the same for loop without any condition
1 2 3 4 5 | int i =0; for(; ;){ System.out.println(i); i++; } |
Observe here, there is no condition in the for loop that tells where to stop. So, the preceding code executes without stoppage. It is called an infinite loop. Infinite loops are drawbacks in a program because when the user is caught in an infinite loop, he would be perplexed and could not understand how to come out of it. So, it is the duty of the programmer to see not to form the infinite loops. By chance if the programmer got an infinite loop, he should break it when the condition is reached. For this purpose‘break’ statement can be used.
Ex: Write a program to display numbers from 1 to 10 using infinite for loop?
//to display numbers from 1 to 10
Nested For loop:
1 2 3 4 5 6 7 8 9 10 | public class InfiniteForLoop{ public static void main(String[] args){ int i =0; for(; ;){ System.out.println(i); i++; if(i<=10) break; //if i value exceeds 10, then come out of loop } } } |
Nested For loop:
We can write a for loop with in another for loop, such loops are called as nested for loops.
The preceding for loop get executed for 3 times by changing the I value from 1 to 3. Let us take another for loop,
These statements can executes 4 times by changing the j value from 1 to 4. If we write this loop inside a preceding for loop.
Ex: Write a program to display stars in a triangle form?
For-each loop:
1 2 3 | for(int i=1;i<3;i++){ Statement1; // these are executed 3 times } |
The preceding for loop get executed for 3 times by changing the I value from 1 to 3. Let us take another for loop,
1 2 3 | for(int j=1;j<4;j++){ Statement2; // these are executed 4 times } |
These statements can executes 4 times by changing the j value from 1 to 4. If we write this loop inside a preceding for loop.
1 2 3 4 5 6 | for(int i=1;i<3;i++){ Statement1; // these are executed 3 times for(int j=1;j<4;j++){ Statement2; // these are executed 12 times } } |
Ex: Write a program to display stars in a triangle form?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | package com.javatbrains; public class Stars { public static void main(String[] args) { int i = 5; for(int j=0;j<i;j++){ for(int k=0;k<j;k++){ System.out.print("*"); } System.out.println(); } } } |
For-each loop:
for loop is specially designed to handle the elements of collection. Collection represents a group of elements.
For example, we can
take an array as a collection or any class in java.util package can be
considered as a collection. The reason is that an array stores a group
of elements like integer value or strings. Similarly, java.util package
classes are developed to handle a group of objects.
The for each loop
repeatedly executes a group of statements for each element of the
collection. It executes as many times as there are number of elements in
the collection.
Syntax:
1 2 3 | for(variable : collection ){ Statement; } |
Ex: write a program to see the use of for each loop and retrieve the elements one by one from an array and display it?
1 2 3 4 5 6 7 8 9 10 11 12 13 | package com.javatbrains; public class ForEach { public static void main(String[] args) { //declare an array with 5 values int array[] = {10,20,30,40,50}; for(int i: array){ System.out.println(i); } } } |
Comments
Post a Comment