Showing posts with label c programming examples. Show all posts
Showing posts with label c programming examples. Show all posts

Thursday, 17 October 2013

Break And Continue Statements

Break And Continue Statements:

In this C tutorial for beginners, we are going to discuss a very useful facility of C++ which are break and continue statements.

Break statement in C:

Previously, we have seen that break statement was used in a switch structure. Similarly, you can use break statement in while, for and do…while loops. When a break statement executes in a repetition structure, it immediately exits from the repetition structure. There are times when our requirement is met but the loop still doesn’t finish so, we use this break statement condition. This make our code efficient and less time consuming.

It is used for two purposes:

  • To exit early from the loop.
  • To skip the remainder of the switch structure.
The use of break in c can eliminate the use of many (flags) variables. However, the break statements in c should be used very carefully as the excessive use of it will make your code an ugly code. Use it just when you’re sure that what you wanted from the code has been achieved.

Example of break in c:

 int sum=0, num=0;
cin >> num;
while(cin)
{
    if (num<0)
    {
        cout << “Negative number found\n”;
        cin>>num;
        break;
}
    sum+=num;
    cin>>num;
}

Continue statement in c:

Continue statement in c is used in while, for and do…while loop. When continue statement executes, it skips all remaining statements in loop for that certain iteration and proceeds with the next iteration of the loop. 

Let’s take an example of continue in c:


int sum=0, num=0;
cin >> num;
while(cin)
{
    if (num<0)
    {
        cout << “Negative number found\n”;
        cin>>num;
        continue;
}
    sum+=num;
    cin>>num;
}

In this program, if the num is negative then it doesn’t execute the further statements and proceeds with the further iteration else it does.

Final words:

 In this c tutorial for beginners of shapes in c we discussed about break and continue statements in detail.
These are very important and commonly used conditions in C++. You can understand and learn it here.

Sunday, 6 October 2013

Creating A C++ Program

How to Create a C++ program??

In this C tutorial for beginners, we are going to learn how to create C++ programs with output. After studying previous tutorials, we now have the basic and required concepts needed to create a C++ program. A C++ program is a collection of function, one of which is the function main. A function is a set of instructions designed to accomplish specific task. We’ll deal with functions in further tutorials in detail.

The syntax of the function main is:

int main()
{
    Statement 1
    Statement 2
    .
    .
    .
    Statement n

return 0;
}

This main function body contains two type of statements:

1. Declaration statement:

Declaration statements are used to declare things as    variable.

2. Executable statements:

Executable statements perform calculations, manipulate data, create output, accept input and so on. Some executable statements that we came across with are assignment, input and output statements.

 The return 0 function is necessary and is always the last statement. In C++, return is reserved keyword.
 
This main function should be saved in .cpp extension file. 

The file containing this main function or source code is called the source code file or source file.

Things stated step by step needed for creating C++ program examples:

  • Comments if needed to introduce some information about your program
  • Use preprocessor directives to include header filese.g: #include<iostream>
    using namespace std;
  • Then we introduce any global scope variables if necessary.
  • Then we introduce main body as explained above with syntax.


Now, we’ll show you  simple c program examples to explain everything that we mentioned above.

//******************************************************
// Made by shapesinc.blogspot.com
//******************************************************

#include<iostream>    //preprocessor directive
using namespace std;

int main()
{
    int a, b, c;    //declarative statement
    double x, y    //declarative statement

    a=4;        //assignment statement
    cin >> b;    //input statement
    cout << a << “ ” << b <<endl;    //output statement
    return 0;
}