Tampilkan postingan dengan label statement. Tampilkan semua postingan
Tampilkan postingan dengan label statement. Tampilkan semua postingan

Kamis, 12 Januari 2012

Repetition Statement: The For Loop in c++



To understand this you must first go through The While loop in c++.
The below program, Program 1 does the same thing that the Program 1 of previous chapter The While Loop does. An integer i is declared and then three expression of the loop: initializer, loop-test and counting expression, all are passed as parameters in for loop. The variable i is initialized to 0 then it will test the condition whether the value of i is less than 10. If the condition is true, the body of loop will execute. The incrementation statement will be last statement to be executed by the for loop.
// Program 1

#include

using namespace std;

int main ()
{
int i;

    for (i=0; i<10; i++)
{
cout << i << endl;
}

    return 0;
}

If you are just going to use the variable within the body of loop, then better declare it in the for loop itself. This will make your program more reliable. The below programs declare variable in the for loop.


// Program 2

#include

using namespace std;

int main ()
{
for (int i=8; i<=80; i+=8)
cout << i << endl;

    return 0;
}


// Program 3

#include

using namespace std;

int main ()
{
for (int i=1; i<=20; i++)
{
if (i%2==0)
cout << i << "is an even number." << endl;

        else
cout << i << "is an odd number." << endl;
}

    return 0;
}

The operator "%" is called the remainder/modulo operator as already been described in Mathematical Operators. The statement i%2==0 simply means that the remainder of i divided by 2 equals to 0.


// Program 4

#include
#include

using namespace std;

int main ()
{
cout << "Number " << "Square " << "Cube" << endl;

    for (long i=1; i<=10; i++)
{
cout << setw (2) << i << setw (8)
<< i*i << setw (8) << i*i*i << endl;
}

    return 0;
}


// Program 5

#include
#include

using namespace std;

int main ()
{
int number;

    cout << "Enter a number to get its multiplication table: ";
cin >> number;
cout << endl;
cout << "-------------" << endl;

    for (int i=1; i<=10; i++)
{
cout << number << " x " << setw (2) << i << " = "
<< setw (3) << number*i << endl;
cout << "-------------" << endl;
}

    return 0;
}


Please do comment if you don't understand any part or want to know more or just want to say thanks. I love programming and love to teach my friends. Your suggestions and appreciation will make this blog much better.


View the original article here



Peliculas Online

Selection Statement (if-else if-else) in c++



To understand this chapter you should know about Comparison Operators which is explained in previous chapter. Selection statements are very important in programming because we make decisions using it.
// Program 1

#include
#include

using namespace std;

int main ()
{
int first, second;

    cout << "Enter two integers." << endl;

    cout << "First " << setw (3) << ": ";
cin >> first;

    cout << "Second "<< setw (2) << ": ";
cin >> second;

    if (first > second)
cout << "first is greater than second." << endl;

    return 0;
}


// Program 2

#include
#include

using namespace std;

int main ()
{
int first, second;

    cout << "Enter two integers." << endl;

    cout << "First " << setw (3) << ": ";
cin >> first;

    cout << "Second "<< setw (2) << ": ";
cin >> second;

    if (first > second)
cout << "first is greater than second." << endl;

    else
cout << "first is less than or equal to second." << endl;

    return 0;
}


// Program 3

#include
#include

using namespace std;

int main ()
{
int first, second;

    cout << "Enter two integers." << endl;

    cout << "First " << setw (3) << ": ";
cin >> first;

    cout << "Second "<< setw (2) << ": ";
cin >> second;

    if (first > second)
cout << "first is greater than second." << endl;

    else if (first < second)
cout << "first is less than second" << endl;

    else
cout << "first and second are equal." << endl;

    return 0;
}

Note: All the above three programs satisfies the condition that if first value
is greater then display the message "first is greater than second".

Question: When to use if, if-else and if-else if-else ?
Answer: It depends upon your program. The number of possibilities your program has. For example, in the above program there are three possibilities. The value could either be greater, smaller or equal. All these possibilities are covered using if-else if-else statements. Depending upon the possibilities you can add more "else if" statement before the final else statement. It is good programming to cover all the possibilities to make your program perfect.

In the next chapter we will learn about nested if statements.


Please do comment if you don't understand any part or want to know more or just want to say thanks. I love programming and love to teach my friends. Your suggestions and appreciation will make this blog much better.


View the original article here



Peliculas Online

Repetition Statement: The Do-While Loop in c++



We have already gone through The While Loop and The For Loop. Now its time for The Do-While Loop.

// Program 1

#include

using namespace std;

int main ()
{
int i = 0;
do
{
cout << i << endl;
i++;
} while (i < 10);
return 0;
}

The do-while loop works just like the for loop and while loop but with one exception. Unlike the for loop and while loop, the do-while loop will execute at least once.
The for loop and the while loop checks the condition and then the body of loop executes but in case of do-while, the body is executed first and then it checks the condition.


// Program 2

#include

using namespace std;

int main ()
{

    // The while loop
int i = -1;
while (i != -1)
{
cout << "Inside the while loop. " << endl;
cout << "Please enter a number or -1 to quit: ";
cin >> i;
}

    // The for loop
int j = -1;
for (; j != -1; )
{
cout << "Inside the for loop. " << endl;
cout << "Please enter a number or -1 to quit: ";
cin >> j;
}

    // The do-while loop
int k = -1;
do
{
cout << "Inside the do-while loop. " << endl;
cout << "Please enter a number or -1 to quit: ";
cin >> k;
} while (k != -1);

    return 0;
}

When you run the above program, only the body of do-while gets executed and others do not. The initial value is set to -1 and the condition is such that the value should not be equal to -1. The for loop and while loop checks the condition first and hence their body is not executed, the do-while loop executes the body first and hence it gets executed even though the condition is false, as it checks the condition after executing the body.

Use Do-While when you want the body of the loop to execute at least once, even if the condition is false at the start or else you could make use of the for loop and while loop.


Please do comment if you don't understand any part or want to know more or just want to say thanks. I love programming and love to teach my friends. Your suggestions and appreciation will make this blog much better.


View the original article here



Peliculas Online

Repetition Statement: The While Loop in c++



The fundamental to programming are control statements. You must learn to have good command over control statement, if you want to program.
Sequence, selection and repetition are three types of control statement. It specifies the order in which the statement are executed.

(1) Sequence Structure: The sequence structure is built into c++. The c++ statement are executed in sequence, that is one after the other if not directed otherwise. Hope you know this. If not then remember from now on.

(2) Selection Statement: Selection statement are of three types: if, if-else and switch statement. We have already learned these in previous chapters.

(3) Repetition Statement: Repetition statement are of three types: while loop, for loop and do-while loop. We have to learn about these in this and coming chapters.

In programming, often the situation arises in which you have to repeatedly execute a particular code or set of codes. Suppose you want to print your name to the console five times. What you will do? If you have no knowledge of repetition statement, you will write your name in cout 5 times. Something like below:


// Program 1

#include

using namespace std;

int main ()
{
cout << "Mohammed Homam" << endl;
cout << "Mohammed Homam" << endl;
cout << "Mohammed Homam" << endl;
cout << "Mohammed Homam" << endl;
cout << "Mohammed Homam" << endl;

    return 0;
}

But what if you have to print it 100 or 1000 times? It would be bothersome and also the code will be too lengthy if we use the above method. To simplify such task we use repetition statement.



// Program 2

#include

using namespace std;

int main ()
{
int i = 0;
while (i < 5)
{
cout << "Mohammed Homam" << endl;
i++;
}

    return 0;
}

Now lets analyse that what's happening in the above program. Look at the program while reading the each statement of explanation. We declared an integer i and intialised it to 0. After that while loop checks whether i is less than 5. Currently the value of i is 0 and hence the condition i<5 is true. Since the condition is true, the body of while is executed. The body of while loop contains two statement: one that displays the name and other that increments the value of i by 1. After executing the increment statement, now the new value of i is 1. The while loop will again check the condition and since 1 is less than 5 the body of while will execute again. This will go on until the value of i is incremented to 5. Once the value of i is 5, the condition will become false as a result of which the body of loop won't execute and will terminate.

What's the use of increment statement in the above program?
If you don't increment the value of i in the above program, then value of i will always be 0 and hence the condition i < 5 will also be always true. It means your loop will never stop, it will go on and on. This is called infinite loop. Try this by removing i++; from above program. Compile and run it again. Press ctrl+c to terminate the program.


// Program 3

#include

using namespace std;

int main ()
{
int i = 5;
while (i > 0)
{
cout << "Mohammed Homam" << endl;
i--;
}

    return 0;
}

The above program does the same thing as its above program. The logic used is different. Here we gave the intial value to i as 5. Now the condition is, that i must be greater than 0. As the body of while loop is executed each time, the value of i is decremented by 1. The loop terminates when the value of i becomes 0.


// Program 4

#include

using namespace std;

int main ()
{
int i = 0;
while (i < 10)
{
cout << i << endl;
i++;
}

    return 0;
}


// Program 5

#include

using namespace std;

int main ()
{
int i = 9;
while (i >= 0)
{
cout << i << endl;
i--;
}

    return 0;
}

The above program were simplest one's to get started into looping. In the next chapter we will learn about the for loop.


Please do comment if you don't understand any part or want to know more or just want to say thanks. I love programming and love to teach my friends. Your suggestions and appreciation will make this blog much better.


View the original article here



Peliculas Online

The goto statement in c++



We have gone through the while loop, for loop and do-while loop. The another way to do loop in c++ is through using the goto statement. The goto statement was used in olden days but it is not suitable for creating modern applications. But since c++ supports it, you should have knowledge about it, as you may encounter a c++ source code containing goto statements, in that case you will know what it is and how it works.
It consist of label and statements that comes under that label. A label is named by you and it is followed by a colon sign (:). During execution of program, when goto is encountered, it jumps to the statements that comes under the label specified by goto statement. To get a clear idea, analyze the below program example.

#include

using namespace std;


int main ()
{
int i=0;


loop:
cout << i << endl;
i++;


    if (i<10)
goto loop;


    return 0;
}


The use of goto should be avoided to make the program more readable and reliable. The goto statement can cause the program execution to jump to any location in source code and in any direction backward or forward. This makes the program hard to read and understand and also makes it difficult to find bugs.
Since now we have more tightly controlled and sophisticated loops like while loop, for loop and do-while loop, the use of obsolete statement like goto is not at all recommended in creating loops.
Please do comment if you don't understand any part or want to know more or just want to say thanks. I love programming and love to teach my friends. Your suggestions and appreciation will make this blog much better.