Control structures in cpp, C/C++ Programming

Assignment Help:

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); 


Related Discussions:- Control structures in cpp

Explain input output function, Input Output Most languages have "statem...

Input Output Most languages have "statements" to perform I/O . Though in C and C++ we use "functions" to perform I/O. C++ also has its own I/O mechanism - the cin and cout obje

How to make a triangle number, i wont to make triangle with number but numb...

i wont to make triangle with number but number from the largest possible number to 01 (reversed).

What is the reward of operator overloading?, A: you can exploit by overload...

A: you can exploit by overloading standard operators on a class, the intuition of the users of that class. This allow users program in the language of the problem domain instead of

Classes, write a grading program for a class with the following grading pol...

write a grading program for a class with the following grading polices: a.there are two quizzes eaxh graded on the basis of 10 points b.there is ome midterm exam and one final exam

Thermodynamics, want to solve the thermodynamics problems on c language

want to solve the thermodynamics problems on c language

Luminous Jewels - The Polishing Game, Byteland county is very famous for lu...

Byteland county is very famous for luminous jewels. Luminous jewels are used in making beautiful necklaces. A necklace consists of various luminous jewels of particular colour. Nec

Explain zero based addressing, Explain zero based addressing. - Array s...

Explain zero based addressing. - Array subscripts always start at zero. - These subscript values are used to identify elements in the array. - As subscripts start at 0, a

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd