Introduction

  • A Block/Compound Statement is a group of statements which are separated by semicolons (;)and are included/ grouped together in a block enclosed in braces { }.For example : –
{
statement1;
statement2;
statement3;
}

Definition

  • Control statements are statements in the source code that control/specify the flow of program execution.

Types of Control Statements/Structure

  • There are two types of control statements: conditional and un/non-conditional.
  • In C++ object oriented programming, the control structure can be classified into following three categories : –
    1. Selection or Decision control or Conditional statement
    2. Iterating or Repetition or Looping statement
    3. Breaking statement

(A)Selection or Conditional Statement 

  • In this type of statement, the execution of a block depends on the next condition. If the condition evaluates to true, then one set of statement is executed, otherwise another set of statements is executed.
  • C++ provides following types of selection statements : –
    1. If Statement
    2. If-Else Statement
    3. Nested if-else Statement
    4. Else-if Statement
    5. Switch Statement

(I) If Statement

  • Syntax :
If (expression)
{
  (Body of if  Statements);
}
Here, expression is the condition that is being evaluated. If this condition is true, statement is executed. If it is false, statement is ignored (not executed) and the program continues right after this conditional structure.

(II) If-Else Statement

  • Syntax :
If (expression)
{
  (Body of if  Statements 1);
}
else
{
  (Body of else Statements 2);
}
Here, expression is the condition that is being evaluated. If this condition is true, statement 1 is executed. If it is false, then if statement is skipped and the body of else statement 2 is executed.

(III) Nested If-Else Statement

  • A nested if-else statement is a statement that has another if in its if’s body or in its else’s body.

(IV) Else -if Statement

  • Syntax:
if (expression 1)
    statement 1;
else if (expression 2)
    statement 2;
else if (expression 3)
    statement 3;
.
.
.
else
   statement;

(V) Switch Statement

  • Switch statement is used for multiple branch selection.
  • Syntax :
switch (expression)
{
   case exp 1:
        First case body of codes;
        break;
   case exp 2:
        Second case body of codes;
        break;
  case exp 3:
        Third case body of codes;
        break;
.
.
.
  case exp n:
       n case body of codes;
        break;
  default:
       default case body;
}
Here, expression is the condition that is being evaluated. If the case 1 condition is true, First case body code is executed, otherwise case exp 2 is checked and so on….If none of case expressions is true then the value of default case body code is executed.

(B)Iterating or Looping Statement 

  • In C++ , programming language looping statement is used to repeat a set of instructions until certain condition is fulfilled.
  • The iteration statements are also called loops or looping statement.
  • C++ allows following four kinds of iterative loops : –
    1. for looping statement
    2. while looping statement
    3. do-while looping statement and
    4.  nested looping statement

(a) for looping statement

  • This loop is easiest amongst all loops in C++ programming.
  • This loop is specially designed to perform a repetitive action with a counter which is initialized and increased on each iteration.
  • Syntax :
for (initialization; termination condition; increment/decrement)
{
   (body of the loop statements);
}
  • Working Mechanism :
For loop works in the following way : – initialization condition is executed first and once. Generally, it is an initial value setting for a counter variable. This is executed only once and condition is checked. If it is true the loop continues, otherwise the loop ends and statement is skipped (not executed). As usual, it can be either a single statement or a block enclosed in braces { }. finally, whatever is specified in the increment/decrement field is executed and the loop gets back to true or false condition.

(b) while looping statement

  • If we do not know the number of iterations before starting the loop then while loop is used.
  • syntax :

initialization;
while (expression)
{
  statement;
  increment/decrement;
}

  • The functionality of this loop is simply to repeat statement while the condition set in expression is true.

(c) do-while looping statement

  • Unlike for and while loops, the do-while is an exit-controlled loop i.e., it evaluates its test – expression at the bottom of the loop after executing its loop-body statement. This means that a do-while loop always executes at least once, even when the test –expression evaluates to false initially.
  • Syntax :
initialization;
do
{
   statements
   increment/decrement;
}
while (test condition);
  • The do-while loop is usually used when the condition that has to determine the end of the loop is determined within the loop statement itself, like in the previous case, where the user input within the block is what is used to determine if the loop has to end.

(d) nested looping statement

  • When a loop is placed inside the same loop(Homogeneous nesting loop) or different loop(Heterogeneous nesting loop) then it is called nested for loop in C++ programming language.

(C) Breaking statement 

  • Using breaking statement, we can leave a loop even if the condition is true or for its end is not fulfilled.
  • It can be used to end an infinite loop, or to force it to end before its natural end.
  • There are following breaking statements used in C++ programming : –
    1. break statement
    2. continue statement
    3. goto statement and
    4. exit statement

(a) break statement

    • The break statement is used to terminate the execution of the loop program.
    • It terminates the loop in which it is written and transfers the control to the immediate next statement outside the loop.
    • The break statement is normally used in the switch & if conditional statement.

(b) continue statement

    • The continue statement causes the program to skip the rest of the loop in the current iteration as if the end of the statement block had been reached, causing it to jump to the start of the following iteration.

(c) goto statement

    • The goto statement is used to transfer control to some other parts of the program.
    • It is used to alter the execution sequence of the program.

(d) exit statement

  • The exit statement is used to terminate the execution of the program.
  • It is used when we want to stop the execution of the program depending on some condition.
  • When we use exit () statement, we have to include other library functions such as process.h, or stdio.h file.

I/O Formatting in C++

Input/Output Statement in C++

  • Input is accomplished by using cin, which opens a “stream” from the standard input device (keyboard). Data is retrieved from the stream by using the >> (extraction) operator.
  • Output is accomplished by using cout, which opens a “stream” to the standard output device (the screen). Data is inserted into the output stream using the << (insertion) operator.

Formatted/Unformatted Input/Output in C++

  • Those functions which are used to format the Input and output of a C++ program.
  • These functions are helpful in managing the I/O operations in C++ programming.
  • C++ supports input/output statements which can be used to feed new data into the computer or obtain output on an output device such as VDU, printer etc.
  • It provides both formatted and unformatted stream I/O statements.
  • The following C++ I/O formatting functions/streams can be used for the input/output purpose : –
    • Comments in C++
    • Unformatted Console I/O Functions
    • Formatted Setw I/O Function in C++
    • Inline Functions

Unformatted Console I/O Functions in C++

  • The I/O functions such as getch(), putchar(), get(), and put() etc. are called unformatted console I/O functions.
  • The header file for these functions is <stdio.h> and should be included in the beginning of the program.
  • The meaning and use of these functions can be illustrated as follows:
    • getch() – This function is used to accept the input character which is typed by the keyboard during the execution of C++ program.
    • putchar() – It displays the character on the screen at the current location of cursor.
    • Gets (), and puts () are the string functions in C++ programming language.

Formatted Console I/O Functions in C++

  • setw() –
    • In C++ programming language, setw () function is used to set the number of characters to be used as the field width for the next insertion operation.
    • The field width determines the minimum number of characters to be written in some output representations.
    • This manipulator is declared in header <iomanip.h>, along with the other parameterized manipulators.
    • This header file declares the implementation-specific requirement for setw ().
  • Setprecision () – 
    • This function is used to control the number of digits of an output stream to be displayed on the screen in floating point value.
    • This function is included in <iomanip.h> the header file and is included in the beginning of any C++ program.
    • Syntax = setprecision (int p);
    • Example = setprecision(4);
  • showpoint bit flag()
    • This flag is used to show the decimal point for all floating point values.
    • By default, it takes six decimal point values in C++ programming.
    • The syntax of this flag is given as follows :
cout.setf(ios::shpowpoint);

 1,895 total views,  1 views today


0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.