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]
4 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]4 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
What output is produced by
Natasha_Volkova [10]

Answer:

The answer to this question is given below in the explanation section. the correct option is C.

Explanation:

This is Java code statement:

System.out.print("Computing\nisInfun");

The output of this code statement is

Computing

isInfun

However, it is noted that the C option is not written correctly, but it is guessed that it will match to option C.

This Java code statement first prints "Computing" and then on the next line it will print "isInfun" because after the word "Computing" there is a line terminator i.e. \n. when \n will appear, the compiler prints the remaining text in the statement on the next line.

4 0
3 years ago
Write a program that displays the values in the list numbers in ascendingorder sorted by the sum of their digits.
melisa1 [442]

Answer:

Here is the Python program.

def DigitList(number):  

   return sum(map(int, str(number)))  

 

list = [18, 23, 35, 43, 51]  

ascendList = sorted(list, key = DigitList)  

print(ascendList)

Explanation:

The method DigitList() takes value of numbers of the list as parameter. So this parameter is basically the element of the list. return sum(map(int, str(number)))  statement in the DigitList() method consists of three methods i.e. str(), map() and sum(). First the str() method converts each element of the list to string. Then the map() function is used which converts every element of list to another list. That list will now contain digits as its elements. In short each number is converted to the string by str() and then the digit characters of each string number is mapped to integers. Now these digits are passed to sum() function which returns the sum. For example we have two numbers 12 and 31 in the list so each digit is 1 2 and 3 1 are added to get the sum 3 and 4. So now the list would have 3 4 as elements.

Now list = [18, 23, 35, 43, 51] is a list of 5 numbers. ascendList = sorted(list, key = DigitList)  statement has a sorted() method which takes two arguments i.e. the above list and a key which is equal to the DigitList which means that the list is sorted out using key=DigitList. DigitList simply converts each number of list to a another list with its digits as elements and then returns the sum of the digits. Now using DigitList method as key the element of the list = [18, 23, 35, 43, 51] are sorted using sorted() method. print(ascendList) statement prints the resultant list with values in the list in ascending order sorted by the sum of their digits.

So for the above list [18, 23, 35, 43, 51] the sum of each number is 9 ,5, 8, 7, 6 and then list is sorted according to the sum values in ascending order. So 5 is the smallest, then comes 6, 7, 8 and 9. So 5 is the sum of the number 23, 6 is the sum of 51, 7 is the sum of 43, 8 is the sum of 35 and 9 is the sum of 18. So now after sorting these numbers according to their sum the output list is:

[23, 51, 43, 35, 18]                                                                                                          

4 0
3 years ago
What is syntax?
Basile [38]

Answer:

a

(would really appreciate the brainliest)

5 0
3 years ago
____ are the computers that store network software and shared or private user files.
kodGreya [7K]
Servers <span>are the computers that store network software and shared or private user files.</span>
3 0
4 years ago
Refer to the exhibit. The web servers WS_1 and WS_2 need to be accessed by external and internal users. For security reasons, th
BigorU [14]

Answer:

c.Ports Fa3/1 and Fa3/2 on DSW1 will be defined as secondary VLAN isolated ports. Ports Fa3/34 and Fa3/35 will be defined as primary VLAN promiscuous ports.

Explanation:

Primary VLANs which can only be reached by using promiscuous port, comprises of the gateway and isolated VLANs for users to get out of a network.

The isolated ports can only communicate i.e send and receive data with the promiscuous ports; Fa3/34 and Fa3/35.

Also, WS_1 and WS_2 can neither send nor receive data with the data server, thus we isolate them.

3 0
4 years ago
Other questions:
  • You have created a number of different documents using several applications including word, excel, and powerpoint. these files a
    10·1 answer
  • What should you remember about typography while creating your résumé?​
    14·1 answer
  • Computer ________ involves identifying, extracting, preserving, and documenting computer evidence. a. inoculation b. forensics c
    8·1 answer
  • Question 14 (3 points)
    5·1 answer
  • You're the network administrator for a company that has just expanded from one floor to two floors of a large building, and the
    13·1 answer
  • Over a TCP connection, suppose host A sends two segments to host B, host B sends an acknowledgement for each segment, the first
    6·1 answer
  • If a driver who is under the age of 21 is stopped by a law enforcement officer and has a blood alcohol level of 0.02 or greater
    8·1 answer
  • In a system where Round Robin is used for CPU scheduling, the following is TRUE when a process cannot finish its computation dur
    13·1 answer
  • One subtask in the game is to ‘refuel a car’. Explain why ‘refuel a car’ is an abstraction.
    13·1 answer
  • Can yall help me pls
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!