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

Change to palindrome, a palindrome is a string that reads the same from bho...

a palindrome is a string that reads the same from bhote the ends

Explain variables, Variables Information stored in a variable can alter...

Variables Information stored in a variable can alter in the course of the program. The type used in the definition explains the kind of information the symbol can store. Variab

Program for hangman game problem, Program for Hangman Problem   #inclu...

Program for Hangman Problem   #include   #include   #include   #include   #include   static void playGame();   static void printMistakes(int n);   st

Online poker web application, Project Description:  LOOKING TO DEVELOP ...

Project Description:  LOOKING TO DEVELOP A ONLINE POKER Web Application. WEBSITE WILL BE DEVELOPED OVER THE NEXT 6 MONTHS Duration & WILL REQUIRE FULL SOFTWARE CODING OF WEB

What is namespace and explicit container, What is Namespace and Explicit ...

What is Namespace and Explicit container Namespace - Namespaces are used to group entities such as objects, classes and functions under a name. Explicit container.

Change to palindrome, A palindrome is a string that reads the same from the...

A palindrome is a string that reads the same from the both the ends. Given a string S convert it to a palindrome by doing character replacement. Your takes is to convert S to palin

Explain public derivation, Public derivation Public derivations are muc...

Public derivation Public derivations are much more common than private derivations. In this situation: The private members inherited from the base class are inaccessible

Queue - c++ program, Queue - C++ program: Write a program to show the ...

Queue - C++ program: Write a program to show the basic operations on queue. namespace stack {  const int max_size = 200;  char v(max_size);  int top=0;  void pu

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