-2

I saw a C++ program in my C.S. book whose source code is:-

#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 choice which assigns 'y' in the line 5, but after that it doesn't runs, even without showing any error!

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

Please note that I am a newbie to the programming world, and started off with C++

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
Subhadeep Dey
  • 31
  • 1
  • 3
  • 10
  • 1
    If you didn't assign it to y before the loop in this case then it would never enter the loop the first time it would fail immediately and your program would do nothing – csteifel Jul 31 '13 at 17:55
  • 1
    I suggest starting off with one of the fine books listed at http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Mark B Jul 31 '13 at 17:55
  • In C++, you have to declare variables before you use them unlike in some other languages (Matlab and Visual Basic to name a few). Additionally, you want to unconditionally enter the while-loop at least once to run the input code. If `choice` was left uninitialized, the while-loop may not actually execute because the conditional statement may not be true. You could also remove the `choice` initialization and use a `d{}while()` loop instead. – Suedocode Jul 31 '13 at 17:57

8 Answers8

3

Because the very beginning condition for the while loop is if choice=='y', if at the very beginning choice doesn't equal 'y' then it will never even enter the loop to begin with.

If you really don't want to initialize choice first, then change your while loop to a do~while instead:

do
{
     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?

} while (choice=='y'||choice==‘Y');

In this case, the do starts a loop with no condition, your first loop will always happen. At the very end of the loop, it only loops again if the while condition is true.

Lochemage
  • 3,974
  • 11
  • 11
1

Otherwise choice will have a garbage value that may not equals to either 'y' or 'Y' and your loop condition at first time becomes false. If you initialized with y then first time loop will execute for sure (initialized choice with 'y' or 'Y' means code behavior is well defined = always executes for first time).

If you wants to user to enter choice for first time then add extra cin before loop.

// here you may leave choice uninitialized, know behavior is defined 
cout<< "\nWant to Enter more? (y/n)";
cin>> choice; 
while (choice == 'y' || choice == 'Y'){
     cout<< "Enter marks:";
     cin>> marks;
     cout<< "\nEnter class:";
     cin>> classx;

     cout<< "\nWant to Enter more? (y/n)";
     cin>> choice; 
}

Remember: Garbage value derives undefined behavior.
Additionally, its always do practice to initialize a variable in your code with default value.

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
  • But why have we only used 'y' (i.e., the condition value of while looop) as the initial value for "choice"? – Subhadeep Dey Jul 31 '13 at 17:58
  • @SubhadeepDey because in `||` logical or operator is any value is true then condition became true. You can also initialized with `'Y'` your code will run equally well. – Grijesh Chauhan Jul 31 '13 at 17:59
  • 1
    @SubhadeepDey What other initial value would you suggest? If you pick a value other than 'y' or 'Y', then the while check will fail and you will not enter the loop. – Trenin Jul 31 '13 at 18:30
1

It's required here only because of how the while loop is constructed.

Note the loop conditionals:

while (choice=='y'||choice==‘Y')

If choice is not set to y or Y before the while is executed the first time, the condition will never be satisfied, and the loop will never begin.

Try it yourself under a debugger and you will see.

John Dibling
  • 99,718
  • 31
  • 186
  • 324
  • thanks! please rate my question as it annoys me each time while I see my ratings in -ve. It is very difficult for a newbie to tolerate with. I hope you understand what I am saying? – Subhadeep Dey Jul 31 '13 at 18:40
  • @SubhadeepDey: I find it somewhat ironic that you're asking me to upvote your question, but you haven't accepted an answer yet. Don't you? – John Dibling Jul 31 '13 at 19:21
1

The value is assigned in the beginning because only then it will enter the loop or else the condition

while (choice=='y'||choice==‘Y')

fails. So the choice variable is initialised

Keval Doshi
  • 738
  • 6
  • 25
  • then, the user must enter the value before the loop too! – Subhadeep Dey Jul 31 '13 at 18:08
  • We provide a provision for the user to go on adding values once he has entered one value, which happens when he enters the loop. But prompting the user to input a value(virtually asking him to continue even before he has started) is meaningless – Keval Doshi Jul 31 '13 at 18:14
  • thanks! please rate my question as it annoys me each time while I see my ratings in -ve. It is very difficult for a newbie to tolerate with. I hope you understand what I am saying? – Subhadeep Dey Jul 31 '13 at 18:19
1

choice = 'y' make sure that the code in the while loop is done at least 1 time. Else, the loop may be executed once or may not be executed at all, depending on platform/compiler.

Also, using a debugger will help you answer some questions more rapidly.

Iosif Murariu
  • 2,019
  • 1
  • 21
  • 27
1

You asked

But why have we only used 'y' (i.e., the condition value of while looop) as the initial value for "choice"?

The while loop while (choice=='y'||choice==‘Y') specifies 2 conditions with the || (logical OR operator) between them. That means, that whichever condition is true, the while loop executes.

We needed to set choice to an initial value, to be sure that the loop executes the first time. That initial value could have been either 'Y' or 'y', any of them would have allowed the while loop to run the first time.

Ionut Berus
  • 173
  • 2
  • 13
  • thanks! btw, can u please rate my question as it annoys me each time while I see my ratings in -ve. It is very difficult for a newbie to tolerate with. I hope you understand what I am saying? – Subhadeep Dey Jul 31 '13 at 18:42
0

If you don't assign it a value, it will have an indeterminate value, and the while(choice == 'y' || choice == 'Y') will see a value that is not y or Y, and thus not enter the loop the first time, not allowing the user to enter anything.

By the way, you're mixing backquote (`) and single quote (') in your code, the compiler will not accept that. Use only single quote.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • This has been typed by mistake. BTW, I dont know how to write backquote! :P – Subhadeep Dey Jul 31 '13 at 18:09
  • Backquote is the button to the left of the `1` key on most keyboards. Single quote is the key to the left of `Enter` on most keyboards. – Trenin Jul 31 '13 at 18:29
  • Obviously, where those keys are depends on what keyboard layout you have - different countries/languages have them in different places - and yes, it makes a big difference in programming. – Mats Petersson Jul 31 '13 at 22:10
0

In this program the the While loop fist check the value of 'choice' then the code get executed so it should be initialize first by 'y' or 'Y' to execute the code first time

vikas
  • 1
  • 1