Control Statements in C++ may broadly classified into –
Introduction of Control Statements in C++
- A Block/Compound Statement is a group of statements that are separated by semicolons (;)and are included/ grouped in a block enclosed in braces { }.For example: –
Definition of Control Statements in C++
- Control statements are statements in the source code that control/specify the flow of program execution.
Types of Control Statements in C++
- There are two types of control statements: conditional and un/non-conditional.
- In C++ object-oriented programming, the control structure can be classified into the following three categories: –
- Selection or Decision control or Conditional statement
- Iterating or Repetition or Looping statement
- 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 statements is executed, otherwise, another set of statements is executed.
- C++ provides the following types of selection statements: –
- If Statement
- If-Else Statement
- Nested if-else Statement
- Else-if Statement
- Switch Statement
(I) If Statement
- Syntax :
(Body of if Statements);
}
(II) If-Else Statement
- Syntax :
(Body of if Statements 1);
}
(Body of else Statements 2);
}
(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:
statement 1;
else if (expression 2)
statement 2;
else if (expression 3)
statement 3;
.
.
.
else
statement;
(V) Switch Statement
- A switch statement is used for multiple branch selection.
- Syntax :
{
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;
break;
default:
default case body;
}
(B)Iterating or Looping Statement
- In C++, a programming language looping statement is used to repeat a set of instructions until a certain condition is fulfilled.
- The iteration statements are also called loops or looping statements.
- C++ allows the following four kinds of iterative loops: –
-
- for looping statement
- while looping statement
- do-while looping statement and
- nested looping statement
(a) for looping statement
- This loop is the easiest among 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 :
- Working Mechanism :
(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 a statement while the condition set in the 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 :
{
statements
}
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 a different loop(Heterogeneous nesting loop) then it is called a nested for loop in C++ programming language.
(C) Breaking/Jumping control statements in C++
- Using a breaking statement, we can leave a loop even if the condition is true or if 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 the following breaking statements used in C++ programming: –
- break statement
- continue statement
- goto statement and
- 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 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 a 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 a 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 :
0 Comments