184k views
4 votes
What is the difference between Counter Controlled iterations and Sentinel Controlled iterations? What is the purpose of the increment and decrement operators? Give Examples!

User Phunehehe
by
3.7k points

1 Answer

4 votes

Answer and Explanation:

Counter-controlled loops:

In counter controlled loops, the user has an idea of how many times the loop iterates in advance.

The iterations can be counted.

so it is also called definite iterative loops.

Ex:

import java.util.Scanner;

public class Main

{

public static void main(String[] args)

{

//here i is a counter variable.

//The user knows that this for loop iterates 10 times(0 to 9 times) in advance.

int i;

for(i=0;i<10;i++)

{

System.out.println("Hi! Chegg Expert");

}

}

}

Sentinel-controlled loops:

Generally, loops are controlled within a definite count of executions.

But, sometimes, the user may not have a certain idea of how many times the loop needs to iterate.

then, the user needs to take the help of flag variable.

These type of loops iterate an infinite number of times.

By summarizing all the data above,

One cay says that Sentinel loops are the loops in which the user does not have any idea about how many times the body of the loop iterates in advance.

Ex:

import java.util.Scanner;

public class Main

{

public static void main(String[] args)

{

int i=0;

//here in this loop the user does not know how many times the while loop iterates.

while(true)

{

Scanner obj=new Scanner(System.in);

int n=obj.nextInt();

//it is a condition which halts the loop. 'n' is a sentinel variable.

if(n==2)

{

break;

}

System.out.println(n);

}

}

}

Increment and decrement operators:

++ are increment operators.

__ are decrement operators.

The purpose of increment and decrement operators:

When the user needs to add 1 to a variable, then the increment operator(++) is used.

When the user needs to subtract 1 from a variable, then the decrement operator(__) is used.

import java.util.Scanner;

public class Main

{

public static void main(String[] args)

{

int i=10,j=20;

i++;j--;

System.out.println(i);

System.out.println(j);

}

}

output:

11

19

Sometimes,

these operators behaves like assignment operates since they alter the values of the variables they assigned with.

int x=1;

int y=x++;//here y is assigned to x and paralley x is

//incremented by 1 and so y.

Ex:

import java.util.Scanner;

public class Main

{

public static void main(String[] args)

{

int i=1;

i++;

int j=i;

System.out.println(j);

}

}

output:

2

These operators can be placed before the variable or after the variable.

If they are placed before the variable,the values changes before the evaluation of the expression happens.

If they are placed after the variable,the values changes after the evaluation of the expression happens.

ex:

import java.util.Scanner;

public class Main

{

public static void main(String[] args)

{

int x=1;

System.out.println(x++);

int y=x;

System.out.println(++y);

}

}

User Henry He
by
3.6k points