Control structures in cpp, C/C++ Programming

Control structures

The control structures appear in both structured programming languages as well as object oriented programming languages.  The three constructs used are:

i).  Sequence: - This is when one statement is executed following another.

ii). Selection: -It involves making a choice between at least two alternative courses of action. In C++
      there are two ways of making selections.

  • The if statements

Syntax

if  (condition)
      -----

else

with else being optional

  • The switch statements

The following is the general format:

switch(variable|value)
{

Case value:  ;  [-----break;]

}

The switch is appropriate when a number of possible states of variable have to be evaluated at
once.

 

iii). Iteration: - It involves repeating a section of code, for a number of times. In C++ it is achieved in
      three ways

  • for loop
  • while loop
  • do.....while loop.

In each case, there is a condition which allows the loop to terminate.

 

  • The for loop:

The for loop has three principle elements:
      9  The start condition

9  The terminating condition

9  The action which takes place at the end of each iteration Syntax

for(start condition; continue condition; re-evaluation) 

 

{

Statement(s);

}

Example:

The following code segment outputs the squares of numbers one to ten

for(int i=1;i<=10;i++)

{

cout<<"The square of:  "<

}

  • while loop

It is used to execute a block of statements an indefinite number of times, for zero or more number of times (i.e. pre-test loop).

Syntax

while  (condition)
{

Statement(s);

}

  • do.....while loop

It is used to execute a block of statements an indefinite number of times, for one or more number of times and will execute at least once (i.e post-test loop).

Syntax

do

{

Statement(s);

} while  (condition); 

Posted Date: 8/21/2012 11:47:24 AM | Location : United States







Related Discussions:- Control structures in cpp, Assignment Help, Ask Question on Control structures in cpp, Get Answer, Expert's Help, Control structures in cpp Discussions

Write discussion on Control structures in cpp
Your posts are moderated
Related Questions
Question : (a) (i) What is a structure member in C? What is the relationship between a structure member and the structure? (ii) How can structure variables be declared in C?

Presently we are using several spreadsheets to give weekly activity and income reports. Much of the information is being frequently re-keyed into several spreadsheets - we are look

This is a test program that tests the Student and ITECH7603Class classes.     In this assignment you are provided with three input text files associated with this program:

I want a PROGRAM - Facebook auto like system to increase fans to different fanpage It has in a program that take Facebook mails and passwords from a excel file (.xlsx) for examp

Write a C++ program that reads from keyboard 3 integers, with proper input prompt, and then displays the maximum sum of any pair of numbers from these three. If the 3 numbers are 5

Define the Data Types of c language? The C has a concept of 'data types' which are used to define a variable before its use and the definition of a variable will assign storage

basic salary of employees & calculate net salary in C++ Programming

Write a program to find the area under the curve y = f(x) between x = a and x = b, integrate y = f(x) between the limits of a and b. The area under a curve between two points can b

Overloading Unary Operators class sign {int a,b,c; public: sign(){}; sign(int,int,int); void putdata(void); void operator-(); }; void sign::operator-() {a=

Given an integer n and a permutation of numbers 1, 2 ... , n-1, n write a program to print the permutation that lexicographically precedes the given input permutation. If the given