Question

In: Computer Science

Fill in the blank for this common use of the for loop in processing each character...

Fill in the blank for this common use of the for loop in processing each character of a string.

for _____________________________________________________

{

    char ch = str.charAt(i);

    // process ch

}

Insert the missing statement in the following code fragment prompting the user to enter a positive number.

    double value;

    do

    {

    System.out.print("Please enter a positive number: ");

    value = in.nextDouble();

    }

    _____________________________________

The following for loop is executed how many times?

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

Consider the following for loop:

for (i = 0; i < str.length(); i++)

{ ... }

and the corresponding while loop:

i = 0;

while __________________________________

{

    . . .

    i++;

}

Fill in the blank for the while loop condition that is equivalent to the given for loop?

Write a for loop that is equivalent to the given while loop.

i = 1;

while (i <= 5)

{

    . . .

    i++;

}

Solutions

Expert Solution

1. for loop to process each character of a string:

For loop is used to traverse each character of the string where the process:

i. starts from charAt(0)

ii. end at charAt(last character of the string)

Below is the highlighted code for the loop to process the string

for(int I = 0; I < str.length(); i++)

{

    char ch = str.charAt(i);

    // process ch

}

EXAMPLE

Suppose the string str is “Example” which is to be printed by loop having a space in each character. Below is the full code with output:

String str = "Example";

          for(int i = 0; i < str.length(); i++) {

              char ch = str.charAt(i);

              System.out.print(ch+" ");

          }

Output:

-----------------------------------------

2. Please find added while condition with do loop:

double value;

    do

    {

    System.out.print("Please enter a positive number: ");

    value = in.nextDouble();

    }while(value<=0);

Example:

The above code will keep on asking the input until user enters a positive value as below, when 96 is entered the loop exists:

-----------------------------------------

3. The given loop has iterator variable x, which will be executed 11 times from 0 to 10

X’s starting value is = 0;

X’s ending value when the loop will be executed is 10

Please note 10 is included as condition is <=10

-----------------------------------------

4. Given for loop:

The for loop will traverse for each character of i from index 0 to index at its length-1.

Corresponding while loop is as below:

int i =0;

while (i< str.length())

{

    . . .

    i++;

}

EXAMPLE:

The sample code to print each character of string with while loop is as below:

int i =0;

          while (i< str.length())

          {

              char ch = str.charAt(i);

              System.out.print(ch+" ");

              i++;

          }

          Scanner in = new Scanner(System.in);

-----------------------------------------

5. The given while loop will start from int i = 1 to i <=5 and increment i for each iteration.

Corresponding for loop is as below:

for(int i = 1; i <=5; i++) {

                                                ....;

                                }

**Kindly comment if you need any clarification.


Related Solutions

In the blank to the left of each question, fill in the letter from the following...
In the blank to the left of each question, fill in the letter from the following list which best describes the presentation of the item on the financial statements of Helton Corporation for 2018. W. Change in accounting principle X. Change in accounting estimate Y. Correction of an error Z. None of these choices _____ 1. Raiders manufacture heavy equipment to customer specifications on a contract basis. On the basis that it is preferable, accounting for these long term contracts...
PART 1: Fill-In Answers – select the appropriate term to fill in each blank. TERMS: Capacity...
PART 1: Fill-In Answers – select the appropriate term to fill in each blank. TERMS: Capacity Capacity Utilization Diseconomies Economies Federalist Fixed Marginal Needs Scale Semi-fixed Service User Population Spending threshold Tastes and preferences Unit Variable A. [#1] ______________ costs are a type of expense an organization must pay for regardless of the number of clients served, for example, a mortgage on a facility. In contrast, additional expense associated with serving one client, for example tongue depressors in a health...
To begin, write a program to loop through a string character by character. If the character...
To begin, write a program to loop through a string character by character. If the character is a letter, print a question mark, otherwise print the character. Use the code below for the message string. This will be the first string that you will decode. Use the String class method .charAt(index) and the Character class method .isLetter(char). (You can cut and paste this line into your program.) String msg = "FIG PKWC OIE GJJCDVKLC MCVDFJEHIY BIDRHYO.\n"; String decryptKey = "QWERTYUIOPASDFGHJKLZXCVBNM";...
Three types of gametes found in algae are: (blank) (blank) and (blank) fill in blanks:
Three types of gametes found in algae are: (blank) (blank) and (blank) fill in blanks:
Fill in the blank with the answer. Each answer in the list may be used more...
Fill in the blank with the answer. Each answer in the list may be used more than once or not at all. a.Separate and proportionate b.Racketeer Influenced and Corrupt Organization Act (RICO) c.Foreign Corrupt Practices Act d.Fraud e.Negligence f.Reasonable professional care g.Gross negligence h.Securities Act of 1933 i.Compensatory damages j.Damages k.Criminal victim compensation l.Punitive damages m.Hochfelder n.Ultramares o.Privity p.Near privity q.Standing r.Deposition s.Constructive fraud t.Breach of contract u.Joint and severally v.Securities Exchange Act of 1934 _____1.A federal statute used for...
FILL IN THE BLANK: (The size of the blank has nothing to do with the answer)...
FILL IN THE BLANK: (The size of the blank has nothing to do with the answer) 1. The _____ propensity to consume is the change in _____ from a one unit increase in aggregate income or output 2. When a country has a stabilization________ it will use Keynesian fiscal stimulus whenever real GDP was expected to fall short of the full ___________ output level. 3. The _________ out effect occurs when government borrowing leads to higher _________ rates and lower...
Fill in each blank with the correct answer/output. Assume each statement happens in order and that...
Fill in each blank with the correct answer/output. Assume each statement happens in order and that one statement may affect the next statement. Hand trace this code instead of using your IDE. String s = "abcdefghijklmnop"; ArrayList r = new ArrayList(); r.add("abc"); r.add("cde"); r.set(1,"789"); r.add("xyz"); r.add("123"); Collections.sort(r); r.remove(2); The first index position in an array is __________. System.out.print( s.substring(0,1) ); // LINE 2 System.out.print( s.substring(2,3) ); // LINE 3 System.out.print( s.substring(5,6) ); // LINE 4 System.out.print( r.get(0) ); // LINE...
Fill in the blanks. A country is a net ​[fill in blank one] of the goods...
Fill in the blanks. A country is a net ​[fill in blank one] of the goods for which its relative price under Autarky is lower than under free trade and a net ​[fill in blank two] of the other good. A exporter, importer B importer, exporter C exporter, exporter D importer, importer E not enough information to know either direction of trade
QUESTION 1 This goes for ALL fill in the blank questions- Do not use Commas or...
QUESTION 1 This goes for ALL fill in the blank questions- Do not use Commas or Periods in your answers, just the letter. If not, your answer will be marked wrong. The following events took place in June 2016. Senior Frogg’s prepares monthly financial statements. Senior Frogg’s. is in the business of making Mexican Food. Match each event with the choice that correctly describes the effect of the transaction on the accounting equation: Increase (+), Decrease (-), No effect (NE)....
Fill in the first blank for each question. 1.  (________) Data not organized or easily interpreted by...
Fill in the first blank for each question. 1.  (________) Data not organized or easily interpreted by traditional databases or data models. 2.  (________) A database management system such as MongoDB that uses a document model made up of collections and documents to store data. 3.  (________) is a characteristic or property of an entity 4.  (________) a quality that allows you to change the structure of a database 5.  (________) A relational database concept that sets rules called integrity constraints on table relationships
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT