+ 1

Is this while condition is valid?? :-

#include<iostream> using namespace std; int main() {int a; while(a<7) {} return 0; }

21st May 2017, 1:47 PM
Vivek Mishra
Vivek Mishra - avatar
5 Réponses
+ 7
Not valid. a does not have any value. #include<iostream> using namespace std; int main() {int a=5; //initializing a while(a<7) {} return 0; } //Now the code would run in an infinite loop #include<iostream> using namespace std; int main() {int a=5; while(a<7) {a++;} return 0; } //Now the code would run but give no output #include<iostream> using namespace std; int main() {int a=5; while(a<7) {a++; cout <<a;} return 0; } //Now codes runs and prints 67
21st May 2017, 1:55 PM
Pixie
Pixie - avatar
+ 2
it is not returning anything.
21st May 2017, 1:50 PM
Rahul Siloniya
Rahul Siloniya - avatar
+ 2
it is valid condition, but because there is nothing in the while block you get an infinite loop. -try increasing a in the while loop -also print something so that you see some output eg: cout<<a++; [edit] also 'a' needs bring initialized otherwise will be a random number, maybe greater than 7.
21st May 2017, 1:54 PM
ifl
ifl - avatar
+ 1
the condition is valid buy it is nonsense because a is not initialised and count of loops is not defined.The curly brackets are wasted on other hand the semicolon is missing and there is no defined incresing statement.
21st May 2017, 1:57 PM
Highman
Highman - avatar