by Dinesh Thakur Category: Control Structures

In this example, we input the interval values a and b. Each time loop is executed the value of fx is calculated and displayed on the screen for the value of x which ranges from a to b. On each iteration, x is incremented by 0.05. This continues until x is less than equal to the value of b that user inputs.

 
by Dinesh Thakur Category: Control Structures

A common programming construct that is based upon a sequence of nested ifs is the if-else-if ladder.

 
by Dinesh Thakur Category: Control Structures

The Java Example to check if a Number is a palindrome or not.  A Number is palindrome if it remains unchanged when it is reversed, for example "151" is a palindrome as its reverse is "151".

 
by Dinesh Thakur Category: Control Structures

There will be times when we will want to include more than one statement in the initialization and iteration sections of the For loop. For example, consider the loop in the following program:

 
by Dinesh Thakur Category: Control Structures

Nested loop means one loop inside the other. When a loop is written inside the other loop, a condition is essential to maintain: the inner loop will not cross the boundary of the outer loop. That is, the inner loop will begin and end inside the outer loop. We can have any number of loops one inside the other.

 
by Dinesh Thakur Category: Control Structures

step 1: Read n, x

step 2: Set r= 1

step 3: If value of n is 1 then print n and goto step - 8

step 4: Repeat through step-6 until j > 0

step 5: r=r*i

step 6: Update j as j=j-1

step 7: Print r

step 8: Exit

 
by Dinesh Thakur Category: Control Structures

In "PigLatin" a word such as KING is replaced by INGKAYand TROUBLE is replaced by OUBLETRAY and so on. The first vowel of the original word becomes the start of the translation, any proceeding letters being shifted towards the end and followed by AYwords that begin with vowels are left on changed.

 
by Dinesh Thakur Category: Control Structures

we have seen how to test whether a given number is prime or not. The program segment given below uses a for loop to test each number in a given range and print it if it is prime.

 
by Dinesh Thakur Category: Control Structures

Length():-This method is a predefined method of String class present in java. Lang package. It is used to determine the length of string and return type of this method is int type.



 
by Dinesh Thakur Category: Control Structures

The do loop enables us to repeatedly execute a block of code until a boolean expression evaluates to false. It is almost identical to the while loop with the difference that here the logical expression is evaluated at the bottom of the loop rather than the top. This means that the contents of the loop will execute at least once.

 
by Dinesh Thakur Category: Control Structures

When we have if or if else statement within another if construct or else construct, we call it nested if.

 
by Dinesh Thakur Category: Control Structures

Algorithm for Armstrong Number:

step 1:Set s=0

step 2: Read num

step 3: Set b=num

step 4: Repeat through step-7 while num greater than 0

step 5: r=num%10

step 6: s=s+(r*r*r)

 
by Dinesh Thakur Category: Control Structures

In this example we use BufferedReader class for Reading text from a character-input stream. After Enter any 5 Number we check if number ==0 then is zero if number greater than 0 then it is positive other wise negative.

 
by Dinesh Thakur Category: Control Structures

The Random class provides a template for the creation of random number generators. The Math.random() method provides a static function for the generation of random double values.

Two constructors are provided for creating Random objects. The default constructor initializes the seed of the random number generator using the current system time. The other constructor allows the seed to be set to an initial long value.

 
by Dinesh Thakur Category: Control Structures

Algorithm for Digits Are Present in the Number:

step 1: Set c=0

step 2: Read num

step 3: Repeat through step-5 while num greater than 0

step 4: c=c+1

 
by Dinesh Thakur Category: Control Structures

Algorithm for Swap Two Variables Using XOR Operator:

step 1: Read a, b

step 2: a=a^b

step 3: b=b^a

step 4: a=a^b

step 5: Print a,b

step 6: Exit

 
by Dinesh Thakur Category: Control Structures

The currentTimeMillis() method returns the current time in milliseconds since 00:00:00,January I, 1970.This method is used to know the time interval a process or job takes to perform. We record the time before and after the operation and the difference of two timings tells us the total time the operation took.

 
by Dinesh Thakur Category: Control Structures

In this example, the continue statement is placed in the body of inner for loop. While executing inner for loop, if the condition (m==n) evaluates to true then the continue statement is executed and the remaining statement for displaying values of m and n is skipped and control is transferred to the increment expression (n++) of inner for loop. This increments the value of n by 1 and test condition (n<=2) is evaluated again for this incremented value of n. This process continues. We have used continue statement in this program so that the same value of m and n should not be displayed.

 
by Dinesh Thakur Category: Control Structures

The while loop is a construct that repeatedly executes a block of code as long as a boolean condition remains true. The logical expression in while loop is evaluated first. If the logical expression evaluates to false, the body of while loop will not execute even once. If the logical expression in while evaluates to true, the statements in the body of while loop are executed. After executing the body, control jumps again at the top to recheck whether the boolean expression is still true or not. The body of the loop will continue to execute until the expression evaluates to false. When the logical expression becomes false, control continues the execution with the statements following while loop.

 

Page 2 of 4



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles