C++ basic part 2 06-18-2013, 03:13 AM
#1
before I begin i'd like to say this is 100% MINE i am just transferring from SF
If statements
an if statement is a comparison statement. the if statement is seen in 3 different ways:
if (condition){ codeblock}
else if(condition){codeblock}
else{codeblock}
The if statement is always the first type to be used, then the else if, then the else.
When comparing two items in an if statment, such as 'x' and 'y' one should use the '==' and '!=' operators:
== -- compares the 2, if x is_Exactly_equal_to y
!= -- not equal to, if x is_not_equal_to y
An example of the if statement would be:
Another example using strings could be:
______________________________________
Different functions
The main() function is not the only function that can be made in c++, in-fact, functions can easily be made by just writing:
You can also make integer functions, string functions, etc.
In addition to this, functions can take in variables such as strings or ints like so:
One use for this would be:
As you can see, using this has cut out lines of code from the calculator we made in part 1.
______________________________________
Loops
In c++, just as in any other language, there are multiple types of loop that will loop through lines of code and repeat them until a 'trigger' is set off. Some kinds of loops are:
while -- while(conditional){code block}
for - for(int i = NUMBER; i is < OR > OR = ETC. NUMBER; i++ (add 1 to i each time)/i--(subtract 1 to i each time)
do-while-- do{codeblock}while(conditional)
Examples of these loops are:
while loop:
This will print out 1-10
-----------------------------
For loop
Will print out 1-10
-----------------------------
do-while loop
Will print out 1-10
______________________________________
Advanced Calculator
______________________________________
I hope you guys liked this tutorial!
If statements
an if statement is a comparison statement. the if statement is seen in 3 different ways:
if (condition){ codeblock}
else if(condition){codeblock}
else{codeblock}
The if statement is always the first type to be used, then the else if, then the else.
When comparing two items in an if statment, such as 'x' and 'y' one should use the '==' and '!=' operators:
== -- compares the 2, if x is_Exactly_equal_to y
!= -- not equal to, if x is_not_equal_to y
An example of the if statement would be:
Code:
#include <iostream>
using namespace std;
int main()
{
int five = 5;
if(five > 5){// if 5 is greater than 5
cout <<"high";
}
else if (five == 5){// if 5 equals 5
cout<<"correct";
}
else{// if the above 23
cout << "too low"
}
}
Another example using strings could be:
Code:
#include <iostream>
using namespace std;
int main()
{
string password;
cout <<"enter password: ";
cin >> password;
if (password != "passss"){
cout << "access denied.\n";
}
else if (password == "passss"){
cout << "access granted."\n;
}
}
______________________________________
Different functions
The main() function is not the only function that can be made in c++, in-fact, functions can easily be made by just writing:
Code:
void FUNCTION_NAME(){
}
In addition to this, functions can take in variables such as strings or ints like so:
Code:
void FUNCTION_NAME(string name,int age){
}
One use for this would be:
Code:
#include <iostream>
using namespace std;
int add(int a, int b){
return a + b;//this will just return the value of the sum of integers a and b
}
int main()
{
int a;
int b;
cout <<"enter a number: ";
cin >> a;
cout <<"enter another number: ";
cin >> b;
cout<< "the sum of the two numbers you entered is: "<<add(a,b)<<'\n';
}
______________________________________
Loops
In c++, just as in any other language, there are multiple types of loop that will loop through lines of code and repeat them until a 'trigger' is set off. Some kinds of loops are:
while -- while(conditional){code block}
for - for(int i = NUMBER; i is < OR > OR = ETC. NUMBER; i++ (add 1 to i each time)/i--(subtract 1 to i each time)
do-while-- do{codeblock}while(conditional)
Examples of these loops are:
while loop:
Code:
#include <iostream>
using namespace std;
int main()
{
int i = 0;
while(i < 10){
cout << i<<endl;
i++;
}
}
-----------------------------
For loop
Code:
#include <iostream>
using namespace std;
int main()
{
for(int i = 0; i < 10; i++){
cout<<i<<endl;
}
}
-----------------------------
do-while loop
Code:
#include <iostream>
using namespace std;
int main()
{
int i = 0;
do{
cout<<i<<endl;
i++;
}while(i<10);
}
______________________________________
Advanced Calculator
Code:
#include <iostream>
using namespace std;
int divide(int g, int h)
{
return g / h;
}
int multiply(int e, int f)
{
return e * f;
}
int subtract(int c, int d)
{
return c - d;
}
int add(int a, int b)
{
return a + b;
}
int main()
{
string choice;
cout << "would you like to add,subtrace,multiply,or divide?(a,s,m,d): ";
cin >> choice;
if(choice == "a"){
int a,b;//creates 2 integers
cout <<"enter first number: ";
cin >> a;
cout <<"enter second number: ";
cin >> b;
cout <<"answer: "<<add(a,b)<<endl;
}
else if(choice == "s"){
int c,d;//creates 2 integers
cout <<"enter first number: ";
cin >> c;
cout <<"enter second number: ";
cin >> d;
cout <<"answer: "<<subtract(c,d)<<endl;
}
else if(choice == "m"){
int e,f;//creates 2 integers
cout <<"enter first number: ";
cin >> e;
cout <<"enter second number: ";
cin >> f;
cout <<"answer: "<<multiply(e,f)<<endl;
}
else if(choice == "d"){
int g,h;//creates 2 integers
cout <<"enter first number: ";
cin >> g;
cout <<"enter second number: ";
cin >> h;
cout <<"answer: "<<divide(g,h)<<endl;
}
else{
cout<<"unknown answer: shutting off.\n";
}
}
I hope you guys liked this tutorial!