//The while loop loops through a block of code as long as a specified condition is true:
Original file line number
Diff line number
Diff line change
@@ -0,0 +1,16 @@
1
+
#include<iostream>
2
+
usingnamespacestd;
3
+
4
+
intmain()
5
+
{
6
+
int i = 0;
7
+
do
8
+
{
9
+
cout << i << "\n";
10
+
i++;
11
+
} while (i < 5);
12
+
13
+
return0;
14
+
}
15
+
16
+
//The do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.
Original file line number
Diff line number
Diff line change
@@ -0,0 +1,27 @@
1
+
#include<iostream>
2
+
usingnamespacestd;
3
+
4
+
intmain()
5
+
{
6
+
for (int i = 0; i < 5; i++)
7
+
{
8
+
cout << i << "\n";
9
+
}
10
+
11
+
cout<<"\n";
12
+
13
+
for (int i = 0; i <= 10; i = i + 2)
14
+
{
15
+
cout << i << "\n";
16
+
}
17
+
18
+
return0;
19
+
}
20
+
21
+
//When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop
22
+
// Syntax: for (statement 1; statement 2; statement 3) {
23
+
// code block to be executed
24
+
// }
25
+
//Statement 1 is executed (one time) before the execution of the code block.
26
+
//Statement 2 defines the condition for executing the code block.
27
+
//Statement 3 is executed (every time) after the code block has been executed.
Original file line number
Diff line number
Diff line change
@@ -0,0 +1,31 @@
1
+
#include<iostream>
2
+
usingnamespacestd;
3
+
4
+
intmain()
5
+
{
6
+
for (int i = 0; i < 10; i++)
7
+
{
8
+
if (i == 4)
9
+
{
10
+
break;
11
+
}
12
+
cout << i << "\n";
13
+
}
14
+
15
+
cout << "\n";
16
+
17
+
for (int i = 0; i < 10; i++)
18
+
{
19
+
if (i == 4)
20
+
{
21
+
continue;
22
+
}
23
+
cout << i << "\n";
24
+
}
25
+
26
+
return0;
27
+
}
28
+
29
+
// You have already seen the break statement used in an earlier program of 9switchstatement.
30
+
// It was used to "jump out" of a switch statement.
31
+
// The break statement can also be used to jump out of a loop.
Original file line number
Diff line number
Diff line change
@@ -0,0 +1,48 @@
1
+
// C++ has many functions that allows you to perform mathematical tasks on numbers.
2
+
// For that we have to include cmath libary (header file)
3
+
4
+
#include<iostream>
5
+
#include<cmath>//in other old compilers it is #include<math.h>
6
+
usingnamespacestd;
7
+
8
+
intmain()
9
+
{
10
+
cout << max(5, 10);
11
+
cout << min(5, 10);
12
+
cout << sqrt(64);
13
+
cout << round(2.6);
14
+
cout << log(2);
15
+
16
+
return0;
17
+
}
18
+
19
+
/*
20
+
21
+
A list of other popular Math functions (from the <cmath> library) can be found in the table below:
22
+
23
+
Function Description
24
+
abs(x) Returns the absolute value of x
25
+
acos(x) Returns the arccosine of x
26
+
asin(x) Returns the arcsine of x
27
+
atan(x) Returns the arctangent of x
28
+
cbrt(x) Returns the cube root of x
29
+
ceil(x) Returns the value of x rounded up to its nearest integer
30
+
cos(x) Returns the cosine of x
31
+
cosh(x) Returns the hyperbolic cosine of x
32
+
exp(x) Returns the value of Ex
33
+
expm1(x) Returns ex -1
34
+
fabs(x) Returns the absolute value of a floating x
35
+
fdim(x, y) Returns the positive difference between x and y
36
+
floor(x) Returns the value of x rounded down to its nearest integer
37
+
hypot(x, y) Returns sqrt(x2 +y2) without intermediate overflow or underflow
38
+
fma(x, y, z) Returns x*y+z without losing precision
39
+
fmax(x, y) Returns the highest value of a floating x and y
40
+
fmin(x, y) Returns the lowest value of a floating x and y
41
+
fmod(x, y) Returns the floating point remainder of x/y
42
+
pow(x, y) Returns the value of x to the power of y
43
+
sin(x) Returns the sine of x (x is in radians)
44
+
sinh(x) Returns the hyperbolic sine of a double value
45
+
tan(x) Returns the tangent of an angle
46
+
tanh(x) Returns the hyperbolic tangent of a double value
47
+
48
+
/*
Original file line number
Diff line number
Diff line change
@@ -0,0 +1,32 @@
1
+
//It is a program calculates sum, sub, mul, div, and modulus of 2 numbers
2
+
#include<iostream>
3
+
usingnamespacestd;
4
+
5
+
intmain()
6
+
{
7
+
8
+
int x, y;
9
+
int sum;
10
+
int sub;
11
+
int mul;
12
+
int div;
13
+
int mod;
14
+
15
+
cout << "Type a number:\n";
16
+
cin >> x;
17
+
cout << "Type another number:\n";
18
+
cin >> y;
19
+
sum = x + y;
20
+
sub = x - y;
21
+
mul = x * y;
22
+
div = x / y;
23
+
mod = x % y;
24
+
25
+
cout << "Addition is: " << sum << "\n";
26
+
cout << "Subtraction is: " << sub << "\n";
27
+
cout << "Multiplication is: " << mul << "\n";
28
+
cout << "Division is: " << div << "\n";
29
+
cout << "Modulus is: " << mod;
30
+
31
+
return0;
32
+
}
Original file line number
Diff line number
Diff line change
@@ -0,0 +1,34 @@
1
+
//Program to print Hello World
2
+
#include<iostream>
3
+
usingnamespacestd;
4
+
intsum(int,int);
5
+
intmain() {
6
+
cout <<sum(10,20);
7
+
}
8
+
intsum(int a, int b)
9
+
{
10
+
return a+b;
11
+
}
12
+
/* Example Explained below:
13
+
Line 1: #include <iostream> is a header file library that lets us work with input and output objects, such as cout (used in line 5). Header files add functionality to C++ programs.
14
+
15
+
Line 2: using namespace std means that we can use names for objects and variables from the standard library.
16
+
17
+
Don't worry if you don't understand how #include <iostream> and using namespace std works. Just think of it as something that (almost) always appears in your program.
18
+
19
+
Line 3: A blank line. C++ ignores white space.
20
+
21
+
Line 4: Another thing that always appear in a C++ program, is int main(). This is called a function. Any code inside its curly brackets {} will be executed.
22
+
23
+
Line 5: cout (pronounced "see-out") is an object used together with the insertion operator (<<) to output/print text. In our example it will output "Hello World".
24
+
*/
25
+
26
+
27
+
//In turbo C++ and other old compliers it is like this :
0 commit comments