what is ||?

Hi, may I know what is || inside this program? Is it means "or"? If yes, how to write for "and"? (mark<0 & mark>100) or (mark<0 && mark>100)? or both are acceptable? Thanks^^

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <conio>

int main()
{
	int mark;
   do
   {
   cout<<"\nEnter marks for test 1:";
   cin>>mark;
   }while (mark<0||mark>100);

   cout<<"\nYour mark for test 1 is "<<mark;
   cout<<"\n\nEnd Of Program";
   getch();
   return 0;
}
|| is logical or. It returns true if at least one of the sides are true.
&& is logical and. It returns true if both sides are true.

& and | are bitwise operations. Not what you want here.
I believe you have it correct with the &&. And yes, the || means or.
Or you can use something like
1
2
3
4
while(mark<=100 && mark>0)
     {
           //statements here;
     }
Last edited on
|| means or and && means and.
Thanks everyone for the help~

Matri X, I tried ur code, but then got problem here where if the number more then 100 and less then 0, it also give the output, I want them to ask for the input until the input is in between 0 and 100 then only shows the output...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <conio>

int main()
{
	int mark;

   cout<<"\nEnter marks for test 1:";
   cin>>mark;

   while (mark<=100&&mark>0)  {
   cout<<"\nYour mark for test 1 is "<<mark;
   break;

   }

   cout<<"\nYour mark for test 1 is "<<mark;
   cout<<"\n\nEnd Of Program";
   getch();
   return 0;
}
This is for input validation no?

Initialize mark to -1, then run the while loop making sure mark is not > 100 or < 0.

Like so:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

int main ()
{
	int mark = -1;
	while (mark > 100 || mark < 0)
	{
		std::cout << "Enter marks for test 1:\n >";
		std::cin >> mark;
	}

	std::cout << "\nYour mark for test 1 is " << mark;
	std::cout << "\n\nProgram End";

	return 0;
}


Last edited on
In your while looping condition,you ought to put some if...else statement,something like

1
2
3
4
5
6
7
8
9
10
11
while (variable_name>0 && variable_name<100) 
     { if (variable_name>0 &&variable_name<=35) cout<<"your message here";
if (variable_name>35 &&variable_name<=50) cout<<"another message here";
if (variable_name>50 &&variable_name<=75) cout<<"third msg here";
//like that till you finish the whole thing up
}
//to check for negative numbers or 0,include
while (variable_name<=0) {
       cout<<"Error! Enter number greater than Zero: ";
cin>>variable_name;
}
You can use andornot as operators.

oh, it's you... ¿did you fix your compiler? http://cplusplus.com/reference/clibrary/ciso646/ (it is not needed)
Last edited on
Yeah, thanks ne555 for the update, I do try in Borland C++ and Visual Studio 2010 C++, only Visual can use that library to fixed the problem but not in Borland C++, I have replied it here http://www.cplusplus.com/forum/beginner/61691/ (So I think it's actually needed is it?)

You can use andornot as operators.
Is that u mean this?
and = &&

or = ||

not = !

So just use the left one also correct? or u actually means that I can use in the above program here while (mark<0||mark>100); ?

====================================================================

Thanks Matri X, I used to try that code before, but for this program, I want to get the output
Your mark for test 1 is bla bla bla
if and only if the mark insert is between 0 and 100 but the program will be repeated to ask for the user input if and only if the mark insert is out of that range...

====================================================================

Thanks wolfgang, actually I'm trying to use && inside while loop but my system cannot really satisfied the condition where the system will give output no matter I give input out of range or inside the range...can u help me check? Thanks...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <conio>

int main()
{
	int mark;

   cout<<"\nEnter marks for test 1:";
   cin>>mark;

   while (mark<=100&&mark>0)  {
   cout<<"\nYour mark for test 1 is "<<mark;
   break;

   }

   cout<<"\nYour mark for test 1 is "<<mark;
   cout<<"\n\nEnd Of Program";
   getch();
   return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

int main ()
{
	int mark = -1;
	while (mark <= 100 && mark > 0) // Using &&
	{
		std::cout << "Enter marks for test 1:\n >";
		std::cin >> mark;
	}

	std::cout << "\nYour mark for test 1 is " << mark;
	std::cout << "\n\nProgram End";

	return 0;
}
Hi, wolfgang, ur code direct give output as -1 as soon as I run it because while loop(need mark in between 0 and 100) does not go through at all but then direct show the output...
Topic archived. No new replies allowed.