Why do we have to assign a value while using while loop

closed account (jNhkoG1T)
I saw a program in my CS book whose source code is:-

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;
 
int main()
{   char choice=‘y’; //why is this required?
   int marks, classx, test;
 
	while (choice=='y'||choice==‘Y')
	{
		 cout<<"Enter marks:";
		 cin>>marks;
       		 cout<<"\nEnter class:";
		  cin>>classx;
      		  cout<<"\nWant to Enter more? (y/n)";
		  cin>>choice; //Can't understand why choice is assigned 'y' before while loop?
 
	}
return 0;
}


In this code, I can't understand why have we assigned the character 'y' before while loop. I've omitted the value of assigning 'y' in the line 5, but it doesn't runs without showing any error.

Please explain me why have we assinged 'y' to character choice before while loop.

Thanks, in advance!
Last edited on
closed account (Dy7SLyTq)
lets say you didnt. what value would it have? certainly not y. so if it didnt have Y or y it wouldnt go into the loop. you made a mistake in the condition btw. it should be choice=='y'||choice=='Y'
closed account (jNhkoG1T)
In the line 14, the user must enter any variable, then where will it store? and why do we need to include character 'y' only inside while loop (i.e., filled in choice)?
Topic archived. No new replies allowed.