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
Please use C or C++ to write your programs. Our homework will focus on SUN sparc machines because to exploit x86 stack-overflow is too easy. This is not just a programming assignme

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

Precedence and Order of evaluation The languages follow a standard precedence for basic operators. Precedence rules help in deleting ambiguity of the order of operations perfor

Calculate the area of circle using c program: const float Pi = 3.1415926;   inline float area(const float r) {return Pi * r * r;}   main() {    float radius;

how to decode the numerals to string..

Make QtAV Videoplayer simple integrateable for Qt Applications Project Description: I'm preparing an application for Windows and Mac using QtCreator. I want a videoplayer

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

There is a pebble merchant. He sells the pebbles, that are used for shining the floor. His main duty is to take the length of the room’s sides. But he sometimes mistakes doing that

C Program to Check  EVEN & ODD NO   main() {           int n, a;           clrscr();           printf("ENTER ANY NUMBER: ");           scanf("%d", &n);

how to make a program this struct items/product