Question

In: Computer Science

(True or False) The following is an infinite loop. int main() { bool flag = false;...

(True or False) The following is an infinite loop.

int main()
{
bool flag = false;
int num = 0;
while(!flag);
{
cin >> num;
if(num == -1) {
flag = true;
}
}
return 0;
}

Solutions

Expert Solution

NOTE:If you are stuck somewhere feel free to ask in the comments but do not give negative rating to the question as it affects my answering rights.....I will try to help you out

ans=True

It is definately an infinite loop since the initial value of flag is equal to false,

Now the condition in the while loop states that is the flag is not true which means if flag=false then, iterate

Now some may say that the syntax is wrong since you have a semicolon after the condition in while loop.

When it is not the case we can use such syntax which simply means we dont have the body of the while loop but iterate till the condition becomes false

I know kind of weird but true. It makes no sense to use while loop when there is no body of the loop .But that is what we have in c++

Hence it will iterate till flag becomes true which is never hence infinite iterations.

Also you may think that

{

cin >> num;

if(num == -1) {

flag = true;

} THIS SURELY CORRESPONDS TO THE BODY OF THE LOOP. IT IS NOT!!!

IT IS JUST A BLOCK OF SCOPE SINCE WE ALREADY CONCLUDED THE WHILE LOOP BY PUTTING A SEMICOLON


Related Solutions

Considering the following algorithm: bool thread1 = false; bool thread2 = false; thread1 { While (true)...
Considering the following algorithm: bool thread1 = false; bool thread2 = false; thread1 { While (true) {        while(thread2 == true);           thread1 = true;                  /* start of critical section */          …   ...     /* end of critical section */       thread1 = false                  …   }    }   thread2 { While (true) {        while(thread1 == true);           thread2 = true;                  /* start of critical section */          …          /* end of critical section */       thread2 = false                  …   }    }   Does this...
True or False) The following code will output "I was true". bool isGreater(string s1, string s2)...
True or False) The following code will output "I was true". bool isGreater(string s1, string s2) { if(s1 > s2) { return true; } return false; } int main() { if(isGreater("before","back")) { cout << "I was true"; } else { cout << "I was false"; } return 0; }
C++ bool exists_trio(int*,int); (it must use this line here) I was not sure how to utilize...
C++ bool exists_trio(int*,int); (it must use this line here) I was not sure how to utilize this line because I made code that works but not with this line specifically. //Input:    //an integer array (param 1) and its size (param 2) //Output:    //True or false //Behavior:    //Returns true is there exists    //a sequence of 3 *consecutive* values in the array    //such that the sum of the first two elements    //is equal to the third...
What is the ouput of the following code? void loop(int num) { for(int i = 1;...
What is the ouput of the following code? void loop(int num) { for(int i = 1; i < num; ++i) { for(int j = 0; j < 5; ++j) { cout << j; } } } int main() { loop(3); return 0; }
REWRITE THE FOLLOWING CODES USING FOR LOOP. PLS USE C LANGUAGE FORMAT #include <stdio.h> int main(void)...
REWRITE THE FOLLOWING CODES USING FOR LOOP. PLS USE C LANGUAGE FORMAT #include <stdio.h> int main(void) {      int count ; scanf("%d",&count);           while(count--){                printf("\--------------------------------------------\ \n"); printf("\          BBBBB               A                   \ \n"); printf("\          B    B             A A                  \ \n"); printf("\          BBBB              A   A                 \ \n"); printf("\          B    B           AAAAAAA                \ \n"); printf("\          BBBBB           A       A               \ \n"); printf("\---------------------------------------------\ \n");             }                            return 0; }
REWRITE FOLLOWING CODES USING DO...WHILE LOOP. BY USING C LANGUAGE #include <stdio.h> int main(void) {     ...
REWRITE FOLLOWING CODES USING DO...WHILE LOOP. BY USING C LANGUAGE #include <stdio.h> int main(void) {      int count =2; // COUNT TAKEN 2 AS TO PRINT 2 TIMES           while(count--){                printf("\--------------------------------------------\ \n"); printf("\          BBBBB               A                   \ \n"); printf("\          B    B             A A                  \ \n"); printf("\          BBBB              A   A                 \ \n"); printf("\          B    B           AAAAAAA                \ \n"); printf("\          BBBBB           A       A               \ \n"); printf("\---------------------------------------------\ \n");             }                                 return 0; }
C++ Questions: 1. True/False: an int uses 4 bytes in memory on all systems. 2. True/False:...
C++ Questions: 1. True/False: an int uses 4 bytes in memory on all systems. 2. True/False: a double typically offers better precision than a float. 3. True/False: The following two statements are equivalent assuming isPair is a variable of type bool: if (isPair) if (isPair == true) 4. Provide a value we could put in the blank so that the value of x will be 20 after the code snippet. int x = _____; x *= 4; 5. Provide a...
implement this search function using CPP Coding language bool binarySearch(vector<int>vec,int low,int high, int search) Test your...
implement this search function using CPP Coding language bool binarySearch(vector<int>vec,int low,int high, int search) Test your code in main
Development is described in three domains. Group of answer choices True False Flag this Question Question...
Development is described in three domains. Group of answer choices True False Flag this Question Question 23 pts The definition of child development is the change in the child that occurs over time. Changes follow an orderly pattern that moves towards greater complexity and enhances survival. Group of answer choices True False Flag this Question Question 33 pts Albert Bandura believed behaviors are inherently attained. Group of answer choices True False Flag this Question Question 43 pts Learning disabilities are...
Which cohesion does this method belong to? Public void sampleMethod(int flag) { Switch (flag) { Case...
Which cohesion does this method belong to? Public void sampleMethod(int flag) { Switch (flag) { Case ON: // code Case OFF: // code Case CLOSE: //code } }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT