Showing posts with label while. Show all posts
Showing posts with label while. Show all posts

Friday, 30 May 2014

While in Java

While Loop

The while loop is Java's most fundamental loop statement.It repeats the statement or block of code while its controlling expression is true.In while loop, condition is given before the statement.  It can execute the statements 0 or more times.

Syntax of the while loop:

while(condition)
{
   //statements
}

The condition can be any boolean expression.The body of the loop will be executed as long as the conditional expression is true.
When condition becomes false , control passes to the next line of code immediately following the loop.
If loop have only one statement then , curly braces are not necessary.

Flow Chart of the while loop:

Simple program of the while loop:

public class Test { public static void main(String[] args) { int i=5; while(i>0) { System.out.println("Count " + i ); i--; } } }
OUTPUT
Count 5 Count 4 Count 3 Count 2 Count 1

Some important points about while loop:


1.Since the while loop evaluates its conditional expression at the beginning the loop , the body of the loop        will not execute if condition is false at the beginning.
public class Test
{
 public static void main(String[] args)
 {
  int i=5 , j=10;
  while(i>j)
  {
   System.out.println("While loop body");
  }
  System.out.println("Out of While loop");
 }
}
OUTPUT
Out of While loop
2. The body of the while loop can be empty.This is because a null statement is syntactically valid in java.

public class Test
{
 public static void main(String[] args)
 {
  int i=5 , j=10;
  while(++i<--j)
  System.out.println(i);
 }
}
OUTPUT
8
3.If condition will always true then it will become infinite loop.
while(true) //infinite loop


Thursday, 29 May 2014

Do-While Loop in C

While loops causes program to execute the certain block of code repeatedly until some conditions are satisfied, i.e., loops are used in performing repetitive work in programming.
Suppose you want to execute some code/s 10 times. You can perform it by writing that code/s only one time and repeat the execution 10 times using loop.
There are 3 types of while loops in C programming:
  1. while loop
  2. do...while loop
  3. infinite while loop.

1.WHILE LOOP:
               The ‘while’ loop allows execution of statements inside block of loop only if condition in loop succeeds.
Syntax:
            initialization
            while(condition)
            {
                   ..........
                   ..........
                   .......... 
                   condition change statement;
            }
In the pseudo code above :
  • Variable initialization is the initialization of counter of loop before start of ‘while’ loop
  • Condition is any logical condition that controls the number of times execution of loop statements
  • Condition Change statement is the increment/decrement of counter or which change the condition.


Ex:

#include<stdio.h>
#include<conio.h>
int main()
{
          int i;
          i=10;
          while(i)
          {
                  printf("Value of i :%d", i);
                  i++;
          }          return 0;
}

2. do...while loop:
It is another loop like ‘for’ loop in C. But do-while loop allows execution of statements inside block of loop for one time for sure even if condition in loop fails.

Syntax:
           initialization
            do
            {
                   Statement Block
                   ..........
                   .......... 
                   condition change statement;
            }while(condition)


Example:
#include
int main() 
{
   int j = -5;
   do
   {          
       printf("%d\n",j);           
       j = j + 1;     
   }while(j <= 0);        
   return 0;  
} 

Infinite While Loop:

 We can use infinite while loop in C to iterate loop for infinite times. We can Even use Infinite while loop for operation in which we cannot decide how many iteration does it take at compile time. There are many ways we declare infinite while loop:

Way 1 : Semicolon at the end of While
#include<stdio.h>

 void main()

{

        int num=300;

        while(num>255);  //Note it Carefully

        printf("Hello");

} 
Output:
It won't Print anything

Semicolon at the end of while indicated while without body. In the program variable num doesn’t get incremented , condition remains true forever. As Above program does not have Loop body , It won’t print anything.

Way 2 : Non-Zero Number as a Parameter

#include
void main()
{
        while(1)
        printf("Hello");
}
Output:
 Infinite Time "Hello" word 
Non Zero is specified in the While Loop means Loop will have always TRUE condition specified inside. As condition inside loop doesn’t  get changed, condition inside while remains true forever.

Way 3 : Subscript Variable Remains the same:
#include
void main()
{
      int num=20; 
      while(num>10)      
      {     
           printf("Hello");      
           printf(" C ");
      }
}
Output:
Infinite Time "Hello C" word

Way 4 : Character as a Parameter in While  Loop:

#include<stdio.h>
void main()
{
        while('A')
        printf("Hello");
}
Output:
Infinite Time "Hello" word
Explanation:
       Character is Represented in integer in the form of ASCII internally.Any Character is Converted into Non-zero Integer ASCII value Any Non-zero ASCII value is TRUE condition , that is why Loop executes forever

Decision Making In C

Decision making is about deciding the order of execution of statements based on certain conditions or repeat a group of statements until certain specified conditions are met. C language handles decision-making by supporting the following statements,

This also we known as control statement.It is also divided into following category:
1. Selection statements.(if..else and switch-case statement)
2. Iteration or looping statements.(for loop and do-while loop)
3. Jump statements.(break, continue and goto statement)

Friday, 23 May 2014

Java Control Statements

In this post we are going to discuss the control statements.
The control statements are used to control the flow of execution of the program.
Different type of control statements are:
1. Selection statements.
2. Iteration or looping statements.
3. Jump statements.

1.Selection statements:
 Java supports two selection statements if and switch. These  statements allows you to control the flow of your program's execution based upon conditions known only during run time.
For details information on control statements click on below links.

1. If
2. Switch

2. Iteration or Looping statements:
Java supports three Iteration or Looping statements for , while and do while.It executes a block of code or statements till the given condition is true. 
For details information on Iteration or Looping statements click on below links.

1. For loop
2. While loop
3. Do-While loop

3. Jump statements:
Java supports three Jump statements break , continue and return .These statements transfer control to another part of your program.
For details information on Jump statements click on below links.

1. break 
2. continue
3. return