#include
using namespace std;
int main()
{
int x;
cout << "Enter a number between 1 and 10: ";
cin >> x;
if (x >= 1 && x <= 10)
{
cout << "inside the range" << endl;
}
else
{
cout << "outside the range" << endl;
}
if (x < 1 || x > 10)
{
cout << "outside the range" << endl;
}
else
{
cout << "inside the range" << endl;
}
// x is a variable with value 10
//int x = 10;
// The new value of x is the old value (10) + 1
// The new value of x is 11
x = x + 1;
// C++ has an "increment operator"
// This is the same as x = x + 1;
// After this x is 12
++x;
// After the next statement, x is 13 and
// y is 17
int y = 4 + ++x;
// When the ++ appears before the variable,
// the operation is pre-increment. That is we
// increment the variable and return the incremented
// value.
x = 10;
// This statement writes 11
cout << ++x << endl;
// This statement writes 11
cout << x << endl;
// When the ++ appears after the variable the
// the operation is post-increment. That is we
// increment the variable, but return the value the
// variable had before the increment.
x = 10;
// This statement will write 10!
cout << x++ << endl;
// This statement will write 11!
cout << x << endl;
// There is a language called c
int c = 1;
// The language that is the next increment of
// the c language is c++.
c++;
// A for-loop to do something n times
const int N = 10;
for (int i = 0; i < N; i = i + 1)
{
cout << "Hello world!" << endl;
}
for (int i = 0; i < N; ++i)
{
cout << "Hello world!" << endl;
}
// Write all the integers from 0 to N
// When we first enter the loop we declare
// an int called i and set its values 0
// Until the condition fails
// evaluate the boolean expression in the next field.
// If the expression is true, execute the body of the loop.
// After the body of the loop is executed, execute the
// statement in the third field of the for -- in our case,
// that is i++.
for (int i = 0; i <= N; i++)
{
cout << i << endl;
}
// Print all the even integers between 0 and N
for (int i = 0; i <= N; i++)
{
if (i % 2 == 0)
{
cout << i << endl;
}
}
// Print all the even integers between 0 and N
// A little more efficient
for (int i = 0; i <= N; i = i + 2)
{
cout << i << endl;
}
// Print all the odd integers between 0 and N
for (int odd = 1; odd <= N; odd = odd + 2)
{
cout << odd << endl;
}
// Compute the sum of all integers from x to y
// using a loop.
cout << "Enter the first integer in the sum: ";
int firstInt;
cin >> firstInt;
cout << "Enter the last integer in the sum: ";
int lastInt;
cin >> lastInt;
int sum = 0;
for (int i = firstInt; i <= lastInt; i++)
{
// Add the next number to the sum.
sum = sum + i;
}
cout << sum << endl;
system("pause");
}