Question

16) Describe what the break and continue statements do when executed in a loop. 17) What...

16) Describe what the break and continue statements do when executed in a loop.

17) What does a break statement do in a switch-case construct?

18) What method must be declared and implemented in any Java program? Provide the correct form of the first line of this method’s definition.

19) Describe the meanings of all the various parts of the definition from the prior problem.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

16. function of break statement-

when we run any loop (while loop , for loop, do while loop) and we want to stop the execution of loop in any time(while loop is running) then this can be achieve by break statement.

this will be clear more with this example.

let a variable count =0; //initial value of count is 0;

for(i=1 ; i <=10 ; i++) // this loop run 10 times if we not introduce any break statement.

{ // in that case loop run for i=1 ,2,3,4,5,6,7,8,9,10

count =count+1;

print(count);

}

printf ("execution is here");

so output is 1 2 3 4 5 6 7 8 9 10

but if we use break inside loop then see,

initialize count =0;

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

{

count=count+1; // in this case loop run for only i=1,2,3,4,5 after that loop is break

print(count);

if(count ==5)

{

break;

}

}

printf("execution is here");

in that case output is 1 2 3 4 5 and "execution is here" is printed;

it means when break statement is introduced in loop then it exit from loop and start execution outside of loop.

function of continue statement

when we run any loop (while loop , for loop, do while loop) and we want to stop the execution of any iteration of loop in any time(while loop is running) then this can be achieve by continue statement.

this will be clear more with this example.

let a variable count =0; //initial value of count is 0;

for(i=1 ; i <=10 ; i++) // this loop run 10 times if we not introduce any continue statement.

{ // in that case loop run for i=1 ,2,3,4,5,6,7,8,9,10

count =count+1;

print(count)

}

printf ("execution is here");

output will count=1 , 2 ,3 , 4, 5, 6, 7, 8, 9, 10 and  "execution is here" is printed.

but if we use continue statement inside loop then see,

initialize count =0;

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

{

count=count+1; // in this case loop run for only i=1,2,3,4,6,7,8,9,10 but not for i=5;

if(count ==5) //when i=5 then it skip i=5 iteration of loop and start execution from i=6(i e stop for i=5);

{

continue;

}

print(count) ;

}

printf("execution is here");

in that case output is 1 2 3 4 6 7 8 9 10 and "execution is here" is printed;

it means when continue statement is introduced in loop then it stop execution of iterations of loop where it is called(here for i=5) continue is called.

17. function of break in switch case statement.

lets start with code .

int n=1;

switch(n)

case 1:

print(" hello 1");

break;

case 2:

print("hello 2");

break;

case 3:

print("hello 3")

break;

in above case n=1 so case 1 is executed and break is called in case 1.

so output will be hello 1 only and it exit from switch statement.

now if we don't introduce break statement then ,

int n=1;

switch(n)

case 1:

print(" hello 1");

case 2:

print("hello 2");

case 3:

print("hello 3")

then output will be hello 1 , hello 2 ,  hello 3 and then exit,

it means if we does not introduce break statement in switch case then it start executing also the below case(note - only below case is executed )

if n=2 in above example(without break statement example ) then output will be  hello 2 ,  hello 3 and then exit, here hello 1 is not printed because ,it is above the case 2.

hence , if break statement is not present in any case then it will execute that case and also all the decendent case till break is not found or cases are end;

18 .

the method that must be declared and implemented in any java program is main method ,

it is defined as public static void main(String args[]).

19 the meaning of various parts is

1 . public - main method should always be public because it can be called from outside of its class.

2. static - main method is static because static allow main method to be called without creating object of class of main method.

3. void -- because return type of main method is void(because it is called by JVM)

5 main - main is keyword for main method.

6. String args[] - it is array of strings which stores arguments passed by command line.

Add a comment
Know the answer?
Add Answer to:
16) Describe what the break and continue statements do when executed in a loop. 17) What...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C and Java allows loop exits using the "break" and "continue" statements. A "break" statement leaves...

    C and Java allows loop exits using the "break" and "continue" statements. A "break" statement leaves the current loop entirely, and a "continue" statement jumps to the end of the current loop. A break statement can only appear inside a loop or a switch statement, and a continue can only appear inside a loop. If we add "break" and "continue" to VSL, what changes would have to be made to the semantic analyzer to ensure that these statements only appear...

  • please write code in java language and do not add any break, continue or goto statements...

    please write code in java language and do not add any break, continue or goto statements Write a program with a graphical interface that allows the user to convert an amount of money between U.S. dollars (USD), euros (EUR), and British pounds (GBP). The user interface should have the following elements: a text box to enter the amount to be converted, two combo boxes to allow the user to select the currencies, a button to make the conversion, and a...

  • Write a C program that does the following: • Displays a menu (similar to what you...

    Write a C program that does the following: • Displays a menu (similar to what you see in a Bank ATM machine) that prompts the user to enter a single character S or D or Q and then prints a shape of Square, Diamond (with selected height and selected symbol), or Quits if user entered Q. Apart from these 2 other shapes, add a new shape of your choice for any related character. • Program then prompts the user to...

  • Only part 1, makinf the 2nd and 3rd picture of code do the same thing using...

    Only part 1, makinf the 2nd and 3rd picture of code do the same thing using array Purpose This homework is to learn the concept and usage of one-dimensional (ID) arrays. Part 1 (30 pts) Modify your HW6' (i.e., digit frequency histogram) in a new program (DigitFrequencyArray.java) to use a count array instead of 10 count variables. Your new program should be much shorter than your original code since there is no need for branching statements (if or switch). For...

  • Fill the blank in each of the following using the terms at the bottom. IT. When...

    Fill the blank in each of the following using the terms at the bottom. IT. When an array is created, a value in the array is called a(n) 12. f and ) must always be used with an if) statement: True or False 13. Theoperator is used to check if two values are equal. 14. A command can only be used in a loop statement. 15. Acommand can be used in both a loop and switch statement. 16. In the...

  • The menu Here is what the menu should look like when running your program:                              &nbsp

    The menu Here is what the menu should look like when running your program:                                         ***********************************                                         *         FUN MENU             *                                         ***********************************                                                 <S>um of natural integers                                                 <L>eap year check                                                 <C>ount vowels                                                 <E>xit Menu tasks Sum of natural integers ask users to enter an integer (n), then use for loop to compute the sum of the first n integers and finally display the result. If users enter negative integer display an error message. Leap year check...

  • This should be programmed in C LANGUAGE! This lab requires you to use arrays, loop structures...

    This should be programmed in C LANGUAGE! This lab requires you to use arrays, loop structures and if statements/switch statements. You must use functions where indicated. Test your program often - make sure each bit of functionality works before you continue. In this problem, we want to create a song and play it as we build it. The way we do this is by using arrays to store different notes. Notes are just tones that run for a specific amount...

  • QUESTION 1 Which statement results in the value false? The value of count is 0; limit...

    QUESTION 1 Which statement results in the value false? The value of count is 0; limit is 10. (count != 0)&&(limit < 20) (count == 0)&&(limit < 20) (count != 0)||(limit < 20) (count == 0)&&(limit < 20) 10 points    QUESTION 2 If this code fragment were executed in an otherwise correct and complete program, what would the output be? int a = 3, b = 2, c = 5 if (a > b) a = 4; if (...

  • How do I get my output to print "What class of car do you want" after every loop? Here is my outp...

    How do I get my output to print "What class of car do you want" after every loop? Here is my output, it gets stuck on asking and calculating the cost of the specific class of car i entered previously. clear cic: cars-input ('How many cars do you want to rentln run input ('Would you like to run the program Y or N','S) while run'Y carclass-input ('What class of car do you want B, C, or DIn', 'S while carclassB...

  • Java Questions When creating a for loop, which statement will correctly initialize more than one variable?...

    Java Questions When creating a for loop, which statement will correctly initialize more than one variable? a. for a=1, b=2 c. for(a=1, b=2) b. for(a=1; b=2) d. for(a = 1&& b = 2) A method employee() is returning a double value. Which of the following is the correct way of defining this method? public double employee()                                    c. public int employee() public double employee(int t)                  d. public void employee() The ____ statement is useful when you need to test a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT