c++ note
c++ note
Simple Program
Main Function
Main Function
Comments
Header
#include <iostream>
Allow the program to have input (cin) and output (cout) streams.
https://docs.microsoft.com/en-us/cpp/standard-
library/cmath?view=msvc-170
Namespaces
Namespace std;
Is called using a directive.
End of line manuipulator (endl)
variable = expression;
CH-3: Assignment Operations
sum += 96;
variable = expression; sum -= 97;
sum *= 7;
sum /= 9;
sum %= 4;
CH-3: Counting Operations (Counter)
variable = expression;
i = i + 1; i += 1;
n = n + 1; n += 1;
count = count + count += 1;
1; j += 2;
j = j + 2; m += 2;
m = m + 2; kk += 3;
kk = kk + 3;
i ++;
n ++;
count ++;
j ++;
m ++;
kk ++;
CH-3: Example Program
CH-3: Manipulators
Flags
The flag refers to an item, such as a variable or an argument, that sets
a condition usually considered active or nonactive.
Using Mathematical Library Functions
#include <cmath>
Degree to Radian
Persian calendar = 360 days (The sun follows the ecliptic path over the
year)
One degree each day
Degree to Radian
Persian calendar = 360 days (The sun follows the ecliptic path over the
year)
One degree each day
𝝅/180 radian
180/ 𝝅
1 degree = => radian = degree *
If else If else
if “I am hungry”
if “I am hungry”
{
“I will eat” ;
“I will go to restaurant.” ;
else
“I will eat.”;
“I won’t eat”; “I will go home after that.”
}
Else
{
“I won’t eat.”;
“I won’t drink.”;
}
CH4: Selection Structures
If else
if 5>4
“do something” ;
else
“do nothing”;
CH4: Logical Operators
Example
CH4: Selection Structures
Else if else
Else
If
If
else
else
Nested if
If - else if - else
CH4: Selection Structures
“switch” Statement
switch ( expression )
{
case value 1:
statement1;
etc…
break;
case value 2:
statement2;
etc…
break;
default:
statement;
}
If and Switch
y = 10x2 + 3x – 2
while (x <= 3)
for ( x = 1; x <= 3; x++)
{
{ y = (10 * x * x)
+ (3 * x) – 2;
y = (10 * x * x) + (3 * x) – 2;
x ++;
} }
Output:
x=1 => y = 10+3-2 = 11
x=2 => y = 40+6-2 = 44
x=3 => y = 90+9-2 = 97
X=4 (condition false)
Nested loop
Output
*****
*****
*****
Random number
do
{
statement 1;
etc….
}
while (count != 0);
CH6: Function
{ {
if (x < 0) if (x < 0)
X = -x; X = -x;
cout << “The absolute value is “ << cout << “The absolute value is “ <<
x << endl; x << endl;
} }