logow

Java Loops and Control Statements

Java control Statements 



Java
forloop4.blogspot.com







what is control statements in Java Programming


Basically, A control statement is a statement that determines whether other statements will be executed. In other words control statements are those statements that change the Flow of execution based on some Logical  Conditions.

There are 5 types of Control Statements in Java.
  1. if
  2. if else
  3. if else ladder
  4. Nested if
  5. Switch

1.) if -- In this statement if the condition is true it's body execute otherwise does not execute.In the case of if in the place of condition always zero and non-zero value is checked in which zero means condition false and non-zero means condition true.

For Example :

IF Example .... 





Output is .... x is greater than y:


2.) if else -- In this statement condition is true if part executes and if the condition is false then else part executes. In the case of if in the place of condition always zero and non-zero value is checked in which zero means condition false and non-zero means condition true.

For Example :






Output is ...... x is not equal to y-


3.) if-else Ladder - - In Java if else ladder is a part of conditional statement that executes only one condition at a time. If all condition are false then else part executes.it executes that condition that becomes first true from the top.in the case of if in the place of condition always zero and non-zero is checked in which zero means condition false and non-zero means condition true.

For Example : 





Output is ..... x is greater than 5-:

3.) Nested if - - Nested means one side another so one inside another if is called nested nested if.In the case of if in the place of condition always zero and non-zero value checked in which zero means condition false and non-zero means condition true.Nested means If the Outer if condition true then inner condition will execute otherwise not Execute.

For Example : 






Output is ....... x is greater than 5 and less than 15- 


5.) Switch case - In Java in Switch statement allows us to execute one statement from many statement and that statements are called case.Actually in switch statement , inside the body of switch a number of cases are used and a parameter are passed and from which cases this parameter is matched,it's executed.

Things to be remember --------

  1. In the Switch Statement a value/number is passed in the Equal in the place of parameter and that case will execute which is equal to the value/number.
  2. If no case matched then default case will Execute.  
  3. break statement is used to go outside from the case or Loop. 

For Example : 


  

Output is....... it is case 10 

So Java Control statements Finished Here ...........


Java Loops  



what is Java Loops 

Basically,we use the Loops Because To run the body continuously until a required condition is fulfill is called Looping.It is used to perform Looping operation.when the condition will become true Loop will be executed Otherwise condition will become false and execution of Loop will be Executed.   

There are 3 types of Loops in Java.

  1. For Loop
  2. While Loop
  3. Do While Loop
1.) For Loop - In for loop there are three part Initialization part , Conditional part and Increment or Decrement.
Initialization part executes only once.All the three part of the Loop Optional.

For Example :




Output is ...... counting is..
1
2
3
4
5
6
7
8
9
10

In the above program i is a variable which is initialize with 1,condition goes to 10 and it is incremented by 1 so the output will be 1 to 10 .


2.) While Loop - In simple words while Loop body will Execute until the Given Condition is true.
In while Loop we initialize the condition variable first then we enter the condition in while Loop Body and Increment/Decrement in the body of the Loop.

For Example :



In the below program i is a variable which is initialize with 1 , condition goes to 10 and it is incremented by 1 so the output will be 1 to 10 . 




Output is ...... counting is.. 
1
2
3
4
5
6
7
8
9
10


3.) do-while Loop - In do-while loop in this loop it is similar to while loop but one thing is in do-while condition will execute at least one time without checking the condition.it's body will execute until the given condition is true.

Consider the Following Example -



Also Read..Queue in 15 Minutes Must Read


Output is ...... counting is..
1
2
3
4
5
6
7
8
9
10

In this Program condition will check at one time in do-while and when i goes to 11 then condition false and Loop will be Stopped .


Nested Loops in Java


A Loop inside another Loop is called Nested Loop . So on for loop inside another Loop is called For Loop Nested Loop . 

For Example : 





Output is ...... Top 5 Prime Number is ....

2
3
5
7
11



So Java Loop also Finished................








#StayHome









Explain By Amit 

Technical Content Writer and Owner of Justok! Website



Post a Comment

0 Comments