Control Statements in C and C++ programming languages
Every C or C++ program begins its execution from the main function main(). A function is a collection of commands or statements to perform an operation. The main function is always called first when a C / C++ program executes. We can call other functions, whether built-in functions or user defined functions in the main().
}
Using C++ language:
// preprocessor directives - header files
}
The printf() in C and cout in C++ are both used to print or display the data on the console window. The scanf() in C and cin in C++ are both used to accept data on the console window from the user.
In C, printf() and scanf() aree the member functions of stdio class. In C++, The cout is an object of class ostream that represents the standard output stream. The cin is an object of class istream that represents the standard input stream.
The clrscr() and getch() requires the header file conio.h as they are the member functions of conio class.
The clrscr() is used to "clear the screen" i.e. to clear the console window and the getch() or "get character" makes the compiler wait till the user enters a character from the keyword.
CONDITIONAL STATEMENTS in C and C++:
The conditional statements are the features of a programming language which perform different computational actions depending upon the Boolean result (true or false) of a specified condition.
The if statement:
if(condition)
statement;
The statement executes only when the condition is true.
The if-else statement:
if(condition1)
statement1;
else if(condition2)
statement2;
The compiler checks for the "condition2" only when the "condition1" is false. If the condition2 is true, the compiler executes th statement2.
Compilers for C and C++:
Borland TurboC 3.0 is the basic compiler for executing programs of C language. Since C++ is the advanced version of the C language, it generates C code as its object code. Therefore, both C and C++ programming languages can be executed by the same compiler.
Download Borland Turbo C 3.0
Let’s look at a basic difference between the syntax of C language and C++ language program by taking a simple example to add two numbers.
Using C language:
Download Borland Turbo C 3.0
Let’s look at a basic difference between the syntax of C language and C++ language program by taking a simple example to add two numbers.
Using C language:
// preprocessor directives - header files
#include<stdio.h>
#include<conio.h>
// main() function with no argument and void return type
void main()
{
// variables declaration with respective the data types
int n1,n2,sum;
clrscr();
// initialization
sum=0;
// standard output
printf("Enter the value of n1");
// standard input - store user's input at variable a
scanf("%d",&n1);
printf("Enter the value of n2");
scanf("%d",&n2);
sum = n1 + n2;
printf("The sum of the numbers is %d", sum);
getch();
void main()
{
// variables declaration with respective the data types
int n1,n2,sum;
clrscr();
// initialization
sum=0;
// standard output
printf("Enter the value of n1");
// standard input - store user's input at variable a
scanf("%d",&n1);
printf("Enter the value of n2");
scanf("%d",&n2);
sum = n1 + n2;
printf("The sum of the numbers is %d", sum);
getch();
}
Using C++ language:
// preprocessor directives - header files
#include<iostream.h>
#include<conio.h>
// main() function with no argument and void return type
void main()
{
// variables declaration with respective the data types
int n1,n2,sum;
clrscr();
// initialization
sum=0;
// standard output
cout<<"Enter the value of n1";
// standard input - store user's input at variable a
cin>>n1;
cout<<"Enter the value of n2";
cin>>n2;
sum = n1 + n2;
cout<<"The sum of the numbers is "<<sum;
getch();
void main()
{
// variables declaration with respective the data types
int n1,n2,sum;
clrscr();
// initialization
sum=0;
// standard output
cout<<"Enter the value of n1";
// standard input - store user's input at variable a
cin>>n1;
cout<<"Enter the value of n2";
cin>>n2;
sum = n1 + n2;
cout<<"The sum of the numbers is "<<sum;
getch();
}
Explanation of each term used in the above C or C++ program:
In order to make use of in-built functions and objects that comes with your compiler, you need to include a header files using preprocessor directives '#' and the "include" keyword which directs the compiler to include the header file (.h extention file) declared in between the angled brackets "< >". What this does is paste the contents of the header file it into your program. The main() is called first during the execution of a program. The void keyword tells the compiler that the return type of main() in "nothing" i.e. the main() does not return any value.
The printf() in C and cout in C++ are both used to print or display the data on the console window. The scanf() in C and cin in C++ are both used to accept data on the console window from the user.
In C, printf() and scanf() aree the member functions of stdio class. In C++, The cout is an object of class ostream that represents the standard output stream. The cin is an object of class istream that represents the standard input stream.
The clrscr() and getch() requires the header file conio.h as they are the member functions of conio class.
The clrscr() is used to "clear the screen" i.e. to clear the console window and the getch() or "get character" makes the compiler wait till the user enters a character from the keyword.
Few control statements common to both C and C++ programming languages:
The following are the different control statements in C and C++ along with the syntax and the flow of control diagrams.CONDITIONAL STATEMENTS in C and C++:
The conditional statements are the features of a programming language which perform different computational actions depending upon the Boolean result (true or false) of a specified condition.
The if statement:
if(condition)
statement;
The statement executes only when the condition is true.
The if-else statement:
if(condition1)
statement1;
else if(condition2)
statement2;
LOOPING STATEMENTS in C and C++:
A loop is a control flow statement that allows a set of statements to be executed repeatedly based on a given boolean condition.
The while statement:
while(condition)
{
statement;
}
The "statement" gets executed repeatedly till the "condition" is false. The "statement" does not execute even once if the "condition" is false.
The do-while statement:
do
{
statement;
} while (condition);
The "statement" gets executed repeatedly till the "condition" is false. The "statement" executes at least once even if the "condition" is false.
The expression in a do-while statement is evaluated after the body of the loop is executed. Therefore, the body of the loop is always executed at least once.
The for loop:
for(initialization; condition; updation;)
{
statement;
}
The initialization is done once at the beginning of the for loop. Then it checks for the condition to be true. If true, it executes the "statement" and then "updation" takes place. Then again it checks for the "condition". The for loop executes repeatedly till the condition is true. Once the condition is encountered to be false, the control jumps out of the for loop.
A loop is a control flow statement that allows a set of statements to be executed repeatedly based on a given boolean condition.
The while statement:
while(condition)
{
statement;
}
The "statement" gets executed repeatedly till the "condition" is false. The "statement" does not execute even once if the "condition" is false.
The do-while statement:
do
{
statement;
} while (condition);
The "statement" gets executed repeatedly till the "condition" is false. The "statement" executes at least once even if the "condition" is false.
The expression in a do-while statement is evaluated after the body of the loop is executed. Therefore, the body of the loop is always executed at least once.
The for loop:
for(initialization; condition; updation;)
{
statement;
}
The initialization is done once at the beginning of the for loop. Then it checks for the condition to be true. If true, it executes the "statement" and then "updation" takes place. Then again it checks for the "condition". The for loop executes repeatedly till the condition is true. Once the condition is encountered to be false, the control jumps out of the for loop.
For more updates from the world of PC & anDroid Techniques,
like us at
like us at
or follow us at
Thanks for reading this guide to install Turbo C / C++ in full screen mode in any version of Microsoft Windows.
Comment below if you have any queries regarding this guide.
Comment below if you have any queries regarding this guide.



Comments
Post a Comment