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...
#include <iostream> #include "lib.hpp" using namespace std; int main() {    // declare the bool bool...
#include <iostream> #include "lib.hpp" using namespace std; int main() {    // declare the bool bool a = true; bool b= true;    //Print the Conjunction function cout<<"\n\nConjunction Truth Table -"<<endl; cout<< "\nP\tQ\t(P∧Q)" <<endl; cout<< a <<"\t"<< b <<"\t"<< conjunction(a,b) <<endl; cout<< a <<"\t"<< !b <<"\t"<< conjunction(a,!b) <<endl; cout<< !a <<"\t"<< b <<"\t"<< conjunction(!a,b) <<endl; cout<< !a <<"\t"<< !b <<"\t"<< conjunction(!a,!b)<<endl;    //Print the Disjunction function cout<<"\n\nDisjunction Truth Table -"<<endl; cout<< "\nP\tQ\t(PVQ)" <<endl; cout<< a <<"\t"<< b <<"\t"<< disjunction(a,b) <<endl;...
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; }
my code is not printing the output #include <stdio.h> #include<stdbool.h> int main(){ // variable declarations   bool...
my code is not printing the output #include <stdio.h> #include<stdbool.h> int main(){ // variable declarations   bool binary[12] = {false,false, false, false,false,false,false,false,false,false,false, false};   int powerTwo[12] = {2048.1028,576, 256, 128, 64, 32, 16, 8 , 4, 2, 1};   int oneZero[9]= {0,0,0,0,0,0,0,0,0};   int tempVal = 0;   double decimal = 0;   int i = 0;   int j = 0;   // Intialization   printf("Starting the CPSC 1011 Decimal to Binary Converter!\n");   printf("Please enter a positive whole number (or EOF to quit): \n");   scanf("%lf", &decimal);   printf("%lf", decimal);...
True or False: It's easy to loop through an array using a for loop in C++...
True or False: It's easy to loop through an array using a for loop in C++ because you can use the .size() function to get the total number of spaces in the array True or False: It's easy to loop through a vector using a for loop in C++ because you can use the .size() function to get the total number of spaces in the vector
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...
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; }
Please code C# Convert the following for loop into a while loop: for(int count = 8;...
Please code C# Convert the following for loop into a while loop: for(int count = 8; count > 0; count--) { Console.WriteLine(count); }
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; }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT