Free Programming Tutorials, Hints, Tips, and Source Codes in JAVA and C++.
Thursday, October 18, 2012
Code Doppler Programming Tutorials: Welcome to Codedoppler
Code Doppler Programming Tutorials: Welcome to Codedoppler: Code Doppler is a newborn community for newbie to professional developers in JAVA and C++ programming languages. Our purpose is to prov...
C++ Loops
The purpose of loops is to repeat the execution of codes a number of time while the condition is still true.
while loop:
The form:
while(condition)
{
statement(s);
}
do while loop:
The form:
do
{
statement(s);
}while(condition);
The difference of the do while is that, it will first execute the statements first then check if the condition is still true then if it is still true, it will iterate or loop (executes the statement(s) again).
for loop:
The form:
for (initialization; condition; increment/decrement)
{
statement(s);
}
Examples:
int x=10;
int i = 0;
while (i<x)
{
cout<<i<<": Hi"<<endl;
i++;
}
do
{
cout<<i<<": Hi"<<endl;
i++;
}while(i<x)
for (int a=0;a<10;a++)
{
cout<<a<<": Hi"<<endl;
}
Try it and see the difference.
while loop:
The form:
while(condition)
{
statement(s);
}
do while loop:
The form:
do
{
statement(s);
}while(condition);
The difference of the do while is that, it will first execute the statements first then check if the condition is still true then if it is still true, it will iterate or loop (executes the statement(s) again).
for loop:
The form:
for (initialization; condition; increment/decrement)
{
statement(s);
}
Examples:
int x=10;
int i = 0;
while (i<x)
{
cout<<i<<": Hi"<<endl;
i++;
}
do
{
cout<<i<<": Hi"<<endl;
i++;
}while(i<x)
for (int a=0;a<10;a++)
{
cout<<a<<": Hi"<<endl;
}
Try it and see the difference.
switch case statements
Switch case statements do a similar job like if else statements. The form is:
switch (variable)
{
case value1:
statement(s);
break;
case value2:
statement(s);
break;
default:
break;
}
Example:
int x = 0;
switch (x)
{
case 0:
cout<<"X is 0"<<endl; //you can have like this code for newline
break;
case 1:
cout<<"X is 1";
cout<<endl; //or you can separate like this.
break;
default:
cout<<"X is neither 0 nor 1";
break;
}
Remember that you can have multiple numbers of case statements not just two like the above example.
Next: C++ Loops
switch (variable)
{
case value1:
statement(s);
break;
case value2:
statement(s);
break;
default:
break;
}
Example:
int x = 0;
switch (x)
{
case 0:
cout<<"X is 0"<<endl; //you can have like this code for newline
break;
case 1:
cout<<"X is 1";
cout<<endl; //or you can separate like this.
break;
default:
cout<<"X is neither 0 nor 1";
break;
}
Remember that you can have multiple numbers of case statements not just two like the above example.
Next: C++ Loops
if, if else
if:
if statement is used to execute a code or a series of codes if and only if the condition is met. The form is:
if (condition)
statement;
The condition is the expression that is being evaluated. If it is true, statement is executed. If it is false, statement is not executed.
If you want to execute a series of statements/codes, you should use curly braces { }. The form is:
if (condition)
{
statements;
statements;
}
Examples:
int x=0;
if (x==0)
cout<<"X is "<<x<<endl;
if (x==1)
cout<<"X is 1";
cout <<"X is not 1";
if (x==0)
{
cout<<"X is 0";
cout<<"X is not 1";
}
if else:
if else control structure is very similar to if statements. We will just add the else keyword if the condition is false. The form is:
if (condition)
statement;
else
statement;
If the condition is false, the statement in the else will be executed. Again, if you want to execute a series of statements/codes, you should use curly braces { }.
Example:
int x = 0;
if ( x==1)
cout<<"X is 1";
else
cout<<"X is not 1";
Next: switch case statements
if statement is used to execute a code or a series of codes if and only if the condition is met. The form is:
if (condition)
statement;
The condition is the expression that is being evaluated. If it is true, statement is executed. If it is false, statement is not executed.
If you want to execute a series of statements/codes, you should use curly braces { }. The form is:
if (condition)
{
statements;
statements;
}
Examples:
int x=0;
if (x==0)
cout<<"X is "<<x<<endl;
if (x==1)
cout<<"X is 1";
cout <<"X is not 1";
if (x==0)
{
cout<<"X is 0";
cout<<"X is not 1";
}
if else:
if else control structure is very similar to if statements. We will just add the else keyword if the condition is false. The form is:
if (condition)
statement;
else
statement;
If the condition is false, the statement in the else will be executed. Again, if you want to execute a series of statements/codes, you should use curly braces { }.
Example:
int x = 0;
if ( x==1)
cout<<"X is 1";
else
cout<<"X is not 1";
Next: switch case statements
Labels:
C++,
control,
control structure,
else,
flow,
if,
if else,
statements
Wednesday, October 3, 2012
C++ Variables
Variables are memory locations where you will store a data. There are several data types a variable can contain. Among the following in C++ are: int, char, float, double and bool.
int - are for integer data types. It is used to contain whole number integers.
char - are for characters. You will see examples later.
float - for integers with decimals.
double - for integers with decimals.
bool - for boolean. True or False;
To declare a variable in C++, this is the format: data_type name
int x;
float y;
bool tf;
You can also declare multiple variables with the same data type:
int x,y,z;
The name of the variable is case sensitive. Var1 is different from var1.
To Initialize a variable:
int x=0;
or declare then initialize:
int x;
x=0;
Next: if, if else statements
int - are for integer data types. It is used to contain whole number integers.
char - are for characters. You will see examples later.
float - for integers with decimals.
double - for integers with decimals.
bool - for boolean. True or False;
To declare a variable in C++, this is the format: data_type name
int x;
float y;
bool tf;
You can also declare multiple variables with the same data type:
int x,y,z;
The name of the variable is case sensitive. Var1 is different from var1.
To Initialize a variable:
int x=0;
or declare then initialize:
int x;
x=0;
Next: if, if else statements
Subscribe to:
Posts (Atom)