Login
Register

Home

Trainings

Fusion Blog

EBS Blog

Authors

CONTACT US

Fusion Blog
  • Register

Oracle Gold Partners, our very popular training packages, training schedule is listed here
Designed by Five Star Rated Oracle Press Authors & Oracle ACE's.

webinar new

Search Courses

Objective:

In the previous article Introduction to Java, we have learnt Introduction of java along with the link to Install   Eclipse. In this article we will learn the looping constructs and conditional statements in Java.

 

Let's begin with basic program:

bnew

In the above program, a simple statement “hello from varun” is written and println command is given so that the same statement is shown in the output.

How to Execute the Program the above program?

Click on the Execute option, which looks like a play button (Green in colour). OR We can use shortcut to execute the program by pressing “ctrl+F11”

 

Some of the examples of Java programs:

1. Addition of two numbers:

b3new

In the above program, 2 integer variables ‘a’ and ‘b’ are declared with values 10 and 30. Another integer variable ‘c’ is declared which is used to store the result of the addition of ‘a’ and ‘b’.In system.out.println(“result is “+c), + will act as a concatenation operator which will help ‘c’ to hold the result.

2. Short integer:

b4new

The range of short integer is – 32,768 to 32,767.The value entered in the above program is 32768. Therefore the result shows -32768. If the value of Short integer is entered between its range, then it shows the exact value.

3. Byte integer:

b5new

The range of byte integer is between -127 to 127.The value of the byte integer that is entered in the program is 123. Therefore the output value shows 123 as the input value is within the range of the byte integer.If the input value is not within the range of the byte integer then it will show the value in negative. For eg. If we put the input value as 147, then it will give the output value as -109.

4. Boolean variable:

b6new

In the above program, boolean variable 'b' is used to print 'true'.

b7new

NOTE: Conditions are evaluated in terms of TRUE or FALSE.

In the above program the condition was 10>20 and the result is false.

 

 If condition:

‘If’ condition is used to test the statement. If the conditions are found to be true then the set of statements will be executed.

For e.g.we are assigning the value true to variable withdraw. If the value of the balance is greater than 0.

if (balance>o)

Withdraw=true;

If-else condition:

If-else construct is use to control the flow of execution on the basis of condition.

For e.g. here we are assigning a value true to the variable withdraw if the value of balance is greater than 0 else it will be set to false.

            If (balance>0)

            {

            Withdraw=true;

            }   

            else

            {

            withdraw=false;

            }

            Example 1:           b8

The above program is an example of if-else condition.Two integer variables ‘a’ and ‘b’ are defined with the values 10 and 30.

 

LOOPS:

b10new

TYPES OF LOOPS:

  1. 1. for loop

  2. 2, while loop

  3. 3. do while loop

 

  1. 1. for loop:

b11new

Example 1:

b12

In the above program, for loop is used to increment the integer “i”.

for (initialization, condition, expression)

for (int i=1;i<=10;i++)

Here, int i=1 is the initialization.

        i<=10 is the condition

        i++ is the expression.

The value i=1 is checked in the condition i<=10 (1<=10). If the condition is true, the value of “i” is executed. Then again it goes in to the for loop and the value of “i” is incremented by 1 i.e. i+1. Once the value is incremented it is again checked by the condition i<=10 and so on. This will continue till the condition fails and the loop will stop.

Example 2:

b13

2. while loop:

b14            

while loop is also another looping statement. It is similar to the for loop except having initialization and expression section.

syntax of while loop is:

while(condition)

{

st-1;st-2;

}

If the condition is satisfied then the st-1 and st-2 will be executed else the looping statement will not be executed.

3. do while loop:

b15new

b16            

do while loop is also another looping statement.

Syntax of do while loop is:

do

{

st-1;st-2;

}

while(condition);

st-1 and st-2 will be executed first and then the condition in the while loop is executed at the end.

If the condition is found true then the looping statements(st-1 and st-2) will be executed again.

This process is repeated till the condition is found false.

 

 


Varun Kapila

Add comment


Security code
Refresh

About the Author

Varun Kapila

Search Trainings

Fully verifiable testimonials

Apps2Fusion - Event List

<<  May 2024  >>
 Mon  Tue  Wed  Thu  Fri  Sat  Sun 
    1  2  3  4  5
  6  7  8  9101112
13141516171819
20212223242526
2728293031  

Enquire For Training

Fusion Training Packages

Get Email Updates


Powered by Google FeedBurner