Conditional structure: if and else
if (condition) statement:
if (x == 100)
cout << "x is 100";
if we want more than a single statement to be executed in case that the condition is true we can specify a block using braces {}:
if (x == 100){
cout << "x is ";
cout << x;
}
if (condition) statement1 else statement2
For example1:
if (x == 100)
cout << "x is 100";
else
cout << "x is not 100"
For example2:
if (x > 0)
cout << "x is positive";
else if (x < 0)
cout << "x is negative";
else
cout << "x is 0";
The while loop:
while (expression) statement
while.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | // custom countdown using while
#include <iostream>
using namespace std;
int main(void){
int n;
cout << "Enter the starting number > ";
cin >> n;
while(n > 0){
cout << n << ", ";
--n;
}
cout << "FIRE!\n";
return 0;
}
|
while.cpp の実行結果は:
[cactus:~/code_c++/cpp_tuts]% ./while
Enter the starting number > 10
10, 9, 8, 7, 6, 5, 4, 3, 2, 1, FIRE!
dowhile.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | // number echoer
#include <iostream>
using namespace std;
int main(void){
unsigned long n;
do{
cout << "Enter number (0 to end): ";
cin >> n;
cout << "You entered: " << n << "\n";
}while(n != 0);
return 0;
}
|
dowhile.cpp の実行結果は:
[cactus:~/code_c++/cpp_tuts]% ./dowhile
Enter number (0 to end): 10
You entered: 10
Enter number (0 to end): 20
You entered: 20
Enter number (0 to end): 30
You entered: 30
Enter number (0 to end): 0
You entered: 0
The for loop
Its format is:
for (initialization; condition; increase) statement;
forloop.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | // countdown using a for loop
#include <iostream>
using namespace std;
int main(void){
for(int n = 10; n > 0; n--){
cout << n << ", ";
}
cout << "FIRE!\n";
return 0;
}
|
forloop.cpp の実行結果は:
[cactus:~/code_c++/cpp_tuts]% ./forloop
10, 9, 8, 7, 6, 5, 4, 3, 2, 1, FIRE!
The break statement
break.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | // break loop example
#include <iostream>
using namespace std;
int main(void){
int n;
for(n = 10; n > 0; n--){
cout << n << ", ";
if(n == 3){
cout << "countdown aborted!\n";
break;
}
}
return 0;
}
|
break.cpp の実行結果は:
[cactus:~/code_c++/cpp_tuts]% ./break
10, 9, 8, 7, 6, 5, 4, 3, countdown aborted!
The continue statement
continue.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | // continue loop example
#include <iostream>
using namespace std;
int main(void){
for(int n = 10; n > 0; n--){
if(n == 5) continue;
cout << n << ", ";
}
cout << "FIRE!\n";
return 0;
}
|
continue.cpp の実行結果は:
[cactus:~/code_c++/cpp_tuts]% ./continue
10, 9, 8, 7, 6, 4, 3, 2, 1, FIRE!
The goto statement
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | // goto loop example
#include <iostream>
using namespace std;
int main(void){
int n = 10;
loop:
cout << n << ", ";
n--;
if (n > 0) goto loop;
cout << "FIRE!\n";
return 0;
}
|
goto.cpp の実行結果は:
[cactus:~/code_c++/cpp_tuts]% ./goto
10, 9, 8, 7, 6, 5, 4, 3, 2, 1, FIRE!
The exit function:
void exit(int exitcode);
The selective structure:switch
func.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | // function example
#include <iostream>
using namespace std;
int addition(int a, int b){
int r;
r = a + b;
return r;
}
int main(void){
int z;
z = addition(45, 55);
cout << "The result is " << z << endl;
return 0;
}
|
func.cpp の実行結果は:
[cactus:~/code_c++/cpp_tuts]% ./func
The result is 100
func2.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | // function2 example
#include <iostream>
using namespace std;
int subtraction(int a, int b){
int r;
r = a - b;
return r;
}
int main(void){
int x = 4;
int y = 3;
int z;
z = subtraction(7, 2);
cout << "The first result is " << z << '\n';
cout << "The second result is " << subtraction(7, 2) << '\n';
cout << "The third result is " << subtraction(x, y) << '\n';
z = 4 + subtraction(x, y);
cout << "The fourth result is " << z << '\n';
return 0;
}
|
func2.cpp の実行結果は:
[cactus:~/code_c++/cpp_tuts]% ./func2
The first result is 5
The second result is 5
The third result is 1
The fourth result is 5
voidfunc.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | // void function example
#include <iostream>
using namespace std;
void printmessage(){
cout << "I'm a function!\n";
}
int main(void){
printmessage();
return 0;
}
|
voidfunc.cpp の実行結果は:
[cactus:~/code_c++/cpp_tuts]% ./voidfunc
I'm a function!
Arguments passed by value and by reference
referfunc.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | // passing parameters by reference
#include <iostream>
using namespace std;
void duplicate(int& a, int& b, int& c){
a *= 2;
b *= 2;
c *= 2;
}
int main(void){
int x = 1;
int y = 3;
int z = 5;
duplicate(x, y, z);
cout << "x = " << x << endl;
cout << "y = " << y << endl;
cout << "z = " << z << endl;
return 0;
}
|
referfunc.cpp の実行結果は:
[cactus:~/code_c++/cpp_tuts]% ./referfunc.cpp
x = 2
y = 6
z = 10
referfunc1.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | // more than one returning value
#include <iostream>
using namespace std;
void prevnext(int x, int& prev, int& next){
prev = x - 1;
next = x + 1;
}
int main(void){
int x = 100;
int y;
int z;
prevnext(x, y, z);
cout << "Previous = " << y << endl;
cout << "x = " << x << endl;
cout << "Next = " << z << endl;
return 0;
}
|
referfunc1.cpp の実行結果は:
[cactus:~/code_c++/cpp_tuts]% ./referfunc1
Previous = 99
x = 100
Next = 101
Default values in parameters
deffunc.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | // default values in functions
#include <iostream>
using namespace std;
int divide(int a, int b = 2){
int r;
r = a / b;
return r;
}
int main(void){
cout << "divide(12) = " << divide(12) << endl;
cout << "divide(20, 4) = " << divide(20, 4) << endl;
return 0;
}
|
deffunc.cpp の実行結果は:
[cactus:~/code_c++/cpp_tuts]% ./deffunc
divide(12) = 6
divide(20, 4) = 5
Overloaded functions
overfunc.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | // overloaded function
#include <iostream>
using namespace std;
int operate(int a, int b){
return a * b;
}
float operate(float a, float b){
return a * b;
}
int main(void){
int x = 5, y = 2;
float m = 5.2, n = 2.2;
cout << "int operate(int a, int b) = " << operate(x, y) << endl;
cout << "float operate(float a, float b) = " << operate(m, n) << endl;
return 0;
}
|
overfunc.cpp の実行結果は:
[cactus:~/code_c++/cpp_tuts]% ./overfunc
int operate(int a, int b) = 10
float operate(float a, float b) = 11.44
Recursivity
factorial.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | // factoiral calculator
#include <iostream>
using namespace std;
long fac(long a){
if(a > 1)
return a * fac(a-1);
else
return 1;
}
int main(void){
long num;
cout << "Please type a number: ";
cin >> num;
cout << num << "! = " << fac(num) << endl;
return 0;
}
|
factorial.cpp の実行結果は:
[cactus:~/code_c++/cpp_tuts]% ./factorial
Please type a number: 5
5! = 120
[cactus:~/code_c++/cpp_tuts]% ./factorial
Please type a number: 10
10! = 3628800
[cactus:~/code_c++/cpp_tuts]% ./factorial
Please type a number: 25
25! = 7034535277573963776
Declaring functions
declare.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | // declaring functions prototypes
#include <iostream>
using namespace std;
void odd(int a);
void even(int a);
int main(void){
int i;
do{
cout << "Type a number (0 to exit): ";
cin >> i;
odd(i);
}while(i != 0);
return 0;
}
void odd(int a){
if( (a%2) != 0)
cout << "Number is odd.\n";
else
even(a);
}
void even(int a){
if( (a%2) == 0)
cout << "Number is even.\n";
else
odd(a);
}
|
declare.cpp の実行結果は:
[cactus:~/code_c++/cpp_tuts]% ./declare
Type a number (0 to exit): 20
Number is even.
Type a number (0 to exit): 21
Number is odd.
Type a number (0 to exit): 15
Number is odd.
Type a number (0 to exit): 57
Number is odd.
Type a number (0 to exit): 0
Number is even.