// Lecture 5 Fall2020.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include
using namespace std;
int f3()
{
// if error return number
// i.e. return(-5)
//success
return(0);
}
int f2()
{
int ret = f3();
if (ret) // if checking whether the value is true or false
return(ret); //stop executing return error
}
int f1()
{
int ret = f2();
if (ret)
return(ret);
}
int main()
{
//int ret = f1();
//if (ret)
//{
// cout << "Program has error" << ret << endl;
// return(ret);
//}
//return(0); // normal convention for C++ programms that 0 equals success
int i=0, j=0, k=0, l=6;
i = j = k = (l == 10);
cout << i << j << k << l << endl;
i = j = k = l = 10;
cout << i << j << k << l << endl;
//i = i + 10;
i += 10; //20
i -= 10; // 10
i /= 10; // 1
i *= 10; // 10
i %= 10; // 0
cout << i << j << k << l << endl;
int o = 2;
cout << (o ^ 10) << endl; // actually a bit operation
cout << (o << 2) << endl; // actually a bit operation
// equality operator test the values
// most of them look at two values and compare them
// equality opertors always return true and false
if (1 > 2) // false
{
// this will not get exected
}
if (false) // form of commenting out code
{
}
if (1) // always true will always get executed
{
}
if (-1) // always true will always get executed
{
}
if (1<2) // always true will always get executed
{
}
if (1 == 1) // always true will always get executed
{
}
if (1 != 1) // always false will never get executed since they are not equal
{
}
if (!true) // always false will never get executed since opposite of of true is false
{
}
if (1>=1) // always true because 1 is equal to 1
{
}
if (1 <= 1) // always true because 1 is equal to 1
{
}
if (1 <= 0) // always false
{
cout << " always false " << endl;
}
// what is true/false? non-zero or 0
/*
Example
+ (Addition)
A+B
– (Subtraction)
A-B
* (multiplication)
A*B
/ (Division)
A/B
% (Modulus)
A%B = returns remainder of integer division
=
== (equality) and = (asignment)
sum = 10;
10 is assigned to variable sum
+=
sum += 10;
This is same as sum = sum + 10
-=
sum -= 10;
This is same as sum = sum – 10
*=
sum *= 10;
This is same as sum = sum * 10
/=
sum /= 10;
This is same as sum = sum / 10
%=
sum %= 10;
This is same as sum = sum % 10
&=
sum&=10;
This is same as sum = sum & 10
^=
sum ^= 10;
This is same as sum = sum ^ 10
Operators
Example/Description
>
x > y (x is greater than y)
<
x < y (x is less than y)
>=
x >= y (x is greater than or equal to y)
<=
x <= y (x is less than or equal to y)
==
x == y (x is equal to y)
!=
x != y (x is not equal to y)
&& (logical AND)
(x>5)&&(y<5)
It returns true when both conditions are true
|| (logical OR)
(x>=10)||(y>=10)
It returns true when at-least one of the condition is true
! (logical NOT)
!((x>5)&&(y<5))
It reverses the state of the operand “((x>5) && (y<5))”
If “((x>5) && (y<5))” is true, logical NOT operator makes it false
& – Bitwise AND
| – Bitwise OR
~ – Bitwise NOT
^ – XOR
<< – Left Shift
>> – Right Shift
(Condition? true_value: false_value);
Example : (A > 100 ? 0 : 1);
Increment operator : ++ i ; i ++ ;
Decrement operator : – – i ; i – – ;
&
This is used to get the address of the variable.
Example : &a will give address of a.
*
This is used as pointer to a variable.
Example : * a where, * is pointer to the variable a.
Sizeof ()
This gives the size of the variable.
Example : size of (char) will give us 1.
*/
return(0);
}
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu
// Tips for Getting Started:
// 1. Use the Solution Explorer window to add/manage files
// 2. Use the Team Explorer window to connect to source control
// 3. Use the Output window to see build output and other messages
// 4. Use the Error List window to view errors
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file