Showing posts with label continue. Show all posts
Showing posts with label continue. Show all posts

Friday, 30 May 2014

Continue Statement In Java

Continue Statement

The continue statement is used to continue the execution of loop.
In while and do-while loops , a continue statement transfer control to the conditional expression that controls the loop.
In for loop , first control goes to the increment/decrements portion and the goes to the expression.

Simple Program for Continue Statement

package org.modi;
public class Test
{
   public static void main(String[] args) 
   {
      for(int i=0;i<10;i++)
      {
         System.out.print( i+" " );
         if(i%2==0)
            continue;
         System.out.println("");
      }
  }
}
OUTPUT
0 1 
2 3 
4 5 
6 7 
8 9 
From program we can see that when continue statement encountered then first the control goes to the increment/decrements and then goes to expression.

Thursday, 29 May 2014

Break And Continue in C

Break:
The break statement allows you to exit a loop from any point within its body, bypassing its normal termination expression. When the break statement is encountered inside a loop, the loop is immediately terminated, and program control resumes at the next statement following the loop. The break statement can be used with all three of C's loops for, while and do...while loops.
Syntax:
break;

The figure below explains the working of break statement in all three type of loops.


Here's an example of a use for the break statement:
#include
int main()
{
 int t ;
 for ( ; ; ) 
 {
  scanf("Enter any no.\n%d" , &t) ;
  if ( t==10 ) 
                break ;
 }
 printf("End of an infinite loop...\n");
 return 0;
 }
Output:
Enter any no.
2
3
10
End of an infinite loop...
In above example, it takes no. until user enter 10 because t=10 then it will break.

Continue:
The continue statement is somewhat the opposite of the break statement. It forces the next iteration of the loop to take place, skipping any code in between itself and the test condition of the loop. In while and do-while loops, a continue statement will cause control to go directly to the test condition and then continue the looping process. In the case of the for loop, the increment part of the loop continues. One good use of continue is to restart a statement sequence when an error occurs.

Syntax:
continue; 

Here's an example of a use for the break statement:

#include
int main()
{
 int x ;
 for ( x=0 ; x<=100 ; x++) 
 {
  if (x%2) continue;
  printf("%d\n" , x);
 }
}

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