1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
kondaur [170]
3 years ago
8

What is the difference between Counter Controlled iterations and Sentinel Controlled iterations? What is the purpose of the incr

ement and decrement operators? Give Examples!
Computers and Technology
1 answer:
malfutka [58]3 years ago
7 0

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);

  }

}

You might be interested in
Which technique is used to convert a color image to a black and white image?
Andrew [12]
Microscope
bc thats the answer
4 0
3 years ago
A cathedral hull allows the boat to do what
Hoochie [10]
It allows power boats to have stability while cruising. The cathedral hull  is like a trimaran in terehat it has one main  and two side hulls stuck together so that has a somewhat square to rectangular shape and therefore exhibits greater stability than a single hulled boat. It became more popular with the advent of fibreglass boats in the 1960's and '70s.
5 0
3 years ago
What is your favorite film and what makes it good?
Gala2k [10]

Answer:

My Favorite film is The Old Guard

Explanation:

I like The Old Guard because it has action an etc.

4 0
3 years ago
Read 2 more answers
What is the quickest option for adding internet images to a power point presentation
Alex777 [14]

Answer:copy and paste

Explanation:

5 0
3 years ago
Need help with these
kolbaska11 [484]
The first one is d the second one is true the third one is false
3 0
3 years ago
Other questions:
  • To say the internet has helped democratize knowledge means that
    11·1 answer
  • Which of the following attributes of a website indicates a more reliable source for information?
    8·1 answer
  • HELP 25 POINTS!!!!!
    6·2 answers
  • Which of the following is the component of the processor that directs and coordinates most of the operations in the computer?A.
    12·1 answer
  • Which technology concept uses computer resources from multiple locations to solve a common problem?
    11·2 answers
  • Find the total cost of a $125 coat purchased in Los Angeles County where the sales tax is 9%. Use the expression c + 0.09c
    10·1 answer
  • What is the best method to avoid getting spyware on a machine
    6·1 answer
  • The term used to describe whereby old and new media are available via the integration of personal computers and high speed satel
    11·1 answer
  • Multiple Select
    5·1 answer
  • and assuming main memory is initially unloaded, show the page faulting behavior using the following page replacement policies. h
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!