REWRITING THE FOR LOOP INTO A WHILE LOOP:
Given for loop:
Syntax of for loop
for(variable initialization; condition; increment/decrement)
{
STATEMENTS
}
int s=0;
for(int i=1;i<= 10; i++)
{
s+=i
}
CODING USING WHILE LOOP:
Syntax of while loop:
while(condition)
{
STATEMENTS
}
//Let sfirst initialize th erequired variables before the starting of the while.
int s=0,i=1;
//Next lets write the required condition in the while loop
while(i<=10)
{
STATEMENTS:
//s+=i is nothing but s=s+i
s=s+i;
//incremention
i++;
}
Hence option 2 is the correct option.
QUESTION40:
Given that the lloop uses ArrayLists to collect elements that match a condition.
When we trace the given code,
we see that ArrayList of integers named matches is made
In the for loop variable element is initialized and intakes the given values
In the for loop we have a if condition where the elements greater than 100 are added to the ArrayList matches.
When the ArrayList values contains the elements 17,410,134,151,28,56,100,167,840
The values that enter the matches list are 410,134,151,167,840
They are indexed from 0 to 4 as they are total of 5 integers .
Hence when matches.get(4) is called it returns 840.
If you are satisfied with the answer please give it a THUMBS UP!!!
Please answer both questions thank you. Rewrite the following for loop into a while loop: int...
2. Rewrite the following loop into a while loop statement: (4 Marks) (CLO2:PLO5:C2) int i, b; cout<<"Please enter the lower bound of the loop:\n"; cin>>b; for (i = 70; b = i; i--) cout << i << endl; CSCS1511/March2019 Page 4 of 6
Convert the following while loop into a for loop. int 1 - 50: int sum-07 while (sum < 1000) sum - sum + 1; Question 35 (2 points) Saved Given an int variable k that has already been declared, use a while loop to print a single line consisting of 80 dollar signs. Use no variables other than k. int sum = 0; for(int i = 100;sum < 10000;1-- sum = sum + i;
using C++ 1. Convert the following while loop into a for loop: int count = 0; while (count < 10) x = count + sin(x); y = count + cos(y); count++;
Please answer both questions thank you. Question 52 (1 point) What is the output of the code snippet given below? String s = "abcde"; int i = 1; while (i < 5) { System.out.print(s.substring(i, i + 1)); i++; Question 50 (1 point) How many times will the output line be printed in the following code snippet? Please enter your answer as an integer. for (int i = 0; i < 3; i++) { for (int j = 1; j <...
II. SHORT ANSWER QUESTIONS. (10, 2 POINTS FOR EACH QUESTION) 1. What will the following loop display? int x = 0; while (x <5) cout<<x << endl;
C++ 5.How many times will the following loop run? int i=5; while (i19) 6 . What is the output of the following code snippet? int time 1; int year2 int rate = 1; int principal = 5; int interest-0 cout <<i < endl while (year< 5) interest (principal * time rate) /1 ; cout << interest << endl; year++
Q. 07 (2 points) Convert the following while loop to a for loop: int count = 0; while (count<50) cout<<"count iscounts endl count+
What is the output of the following C++ code? int count = 1; int num = 25; while (count < 25) { num--; count++; } cout << count « " " « num << endl i 25 1 0 24 1 0 25 0 0 24 0 In this while loop statement, while (counter < 10), where counter is an int variable. Which statement below is an equivalent way to write this while statement? while (10 < counter) while (9...
Consider the following Java method: public static ArrayList<Integer nums ArrayList<Integer values new ArrayList<Integer0; for(int i=10; i<30; i-i+3) if(i%) values.add(i); return values: What is returned after the method call nums()? a. [12] b. [13] c. [14] d. [15] e. [16] O Which of the following represents the final output of the code segment below? int k: int[]A; A new int[3]: fork-0; k<A.length;k++) A[k]-A.length-k; forſk-0; k<A.length-1; k++) A[k+1]-A[k) for(int i-0;i<A.length;i++) System.out.print(A[i]+" "); a. 222 O b. 333 c. 444 d. 555 O...
Consider the following method. public static ArrayList<Integer mystery(int n) ArrayList<Integer seg - new ArrayList<IntegerO; for (int k = n; k > 0; k--) seq.add(new Integer(k+3)); return seq What is the final output of the following Java statement? System.out.println(mystery(3)); a. [1,2,4,5) b. [2,3,4,5) c. [6,5,4,3] d. [7, 7, 8, 8] e. [7,8,9, 8) O Consider the following method: public static int mystery(int[] arr, int k) ifk-0) return 0; }else{ return arr[k - 1] + mystery(arr, k-1):) The code segment below is...