0% found this document useful (0 votes)
13 views10 pages

Controlstmts

Control statements in C++ dictate the flow of execution based on logical conditions, allowing for decision-making, repetition, and jumps within the program. The main types include decision-making statements (like if, if-else, and nested if-else), conditional statements (such as switch), goto statements, and loop control statements (like while, do-while, and for loops). These statements are essential for creating efficient and readable code by controlling how and when different parts of the program are executed.

Uploaded by

suganya.cse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views10 pages

Controlstmts

Control statements in C++ dictate the flow of execution based on logical conditions, allowing for decision-making, repetition, and jumps within the program. The main types include decision-making statements (like if, if-else, and nested if-else), conditional statements (such as switch), goto statements, and loop control statements (like while, do-while, and for loops). These statements are essential for creating efficient and readable code by controlling how and when different parts of the program are executed.

Uploaded by

suganya.cse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Every statement in a computer is executed based on pre-defined rules.

The control flow is


also based on logic. At times, you find a necessity to execute a few customized logics.
Custom statements can be executed using control statements.
Here, the control enters the statements block and gets executed if the logic is satisfied. Hence,
they are called control statements. They are often used to determine the order in which
statements must be executed.
What Are Control Statements in C++?
In simple words, Control statements in C++ help the computer execute a certain logical
statement and decide whether to enable the control of the flow through a certain set of
statements or not. Also, it is used to direct the execution of statements under certain
conditions.
Types of Control Statements in C++
The primary types of control statements in C++ are:
 Decision-making control statements
1. Simple if statement
2. If-else statements
3. Nested if-else statements
4. else-if ladder
 Conditional statements
 Goto statements in C++
 Loop control statements in C++
1. While Loop
2. Do-while Loop
3. For Loop
Simple if Statement
Simple if statements are carried out to perform some operation when the condition is only
true. If the condition of the if statement is true then the statements under the if block is
executed else the control is transferred to the statements outside the if block.
Syntax of the if statement is as given below:

Flow Chart:
If-else Statement
In some situations, you may have to execute statements based on true or false under certain
conditions, therefore; you use if-else statements. If the condition is true, then if block will be
executed otherwise the else block is executed.
Syntax of the if-else statement is as given below:

Flow Chart:
 Nested if-else Statements
The nested if-else statements consist of another if or else. Therefore; if the condition of “if” is
true (i.e., an outer if) then outer if’s if block is executed which contains another if (that is
inner if) and if the condition of if block is true, statements under if block will be executed
else the statements of inner if’s “else” block will be executed.
If the outer “if” condition is not true then the outer if’s “else” block is executed which
consists of another if. The outer else’s inner if the condition is true then the statement under
outer else’s inner if is executed else the outer else’s else block is executed.
Syntax of the nested if-else statement is as given below:
Flow Chart:

 Else-if Ladder Statements


The else-if ladder statements contain multiple else-if, when either of the condition is true the
statements under that particular “if” will be executed otherwise the statements under the else
block will be executed.
Suppose the “if” condition is true, statements under “if” will be executed else the other “if”
condition is tested, and if that condition is true statements under that particular “if” will be
executed. This process will repeat as long as the else-if’s are present in the program.
Syntax of the else-if ladder statement is as given below:
Flow Chart:

Conditional Control Statements in C++


As per the value of the switch expression, the switch statement will allow multi-way
branching.
Depending on the expression, the control is transferred to that particular case label and
executed the statements under it. If none of the cases are matched with the switch expression,
then the default statement is executed.
The syntax of the switch statement is as given below:
Flow Chart:

Goto Statements in C++


The goto statements are used to transfer the flow of control in a program, goto statement is
also known as a jump control statement because it is used to jump to the specified part of the
program. The label in goto statement is a name used to direct the branch to a specified point
in the program.
Syntax of the goto statement is as given below:

Loop Control Statements in C++


 While Loop
A while loop is also known as an entry loop because in a while loop the condition is tested
first then the statements underbody of the while loop will be executed.
If the while loop condition is false for the first time itself then the statements under the while
loop will not be executed even once.
The syntax of the while loop is as given below:
Flow Chart:

do-while Loop
The do-while is also known as an exit loop because in the do-while loop, the statements will
be executed first and then the condition is checked.
If the condition of the while loop is true then the body of the loop will be executed again and
again until the condition is false. Once the condition is false, the control will transfer outside
the do-while loop and execute statements followed soon after the do-while loop.
The syntax of the do-while loop is as given below:
Flow Chart:

For Loop
The for loop is also known as a pre-test loop. From the following syntax, expression1 is an
initialization, expression2 is the conditional expression and expression3 is an updation. The
variables can be initialized in for the statement itself.
The syntax of the for loop is as given below:
for (initialization; condition; update) {
// Code block to be executed repeatedly as long as the condition is true
}
1. Initialization: This is where you initialize the loop control variable. This variable is
typically used to keep track of the current iteration of the loop.
2. Condition: This is the condition that is evaluated before each iteration of the loop. If
the condition is true, the loop continues; otherwise, it terminates.
3. Update: This is where you update the loop control variable. Typically, you increment
or decrement the variable to ensure progress toward the loop termination condition.
In the for loop, expression1 is used to initialize the variable, expression2 is evaluated and if
the condition is true, then the body of for loop will be executed and then the statements under
expression3 will be executed. This process is repeated as long as the for loop condition is
true, once the condition is false control will return to the statements following the for loop
and execute those statements.
Flow Chart:
1. What are control statements in C++?
Control statements are used to specify the flow of execution of a C++ program. They allow
the programmer to make decisions, repeat code, and jump to different parts of the program.
2. What are the different types of control statements in C++?
There are four main types of control statements in C++:
 Conditional statements: These statements allow the programmer to make decisions
based on the value of a variable or expression. The most common conditional
statements in C++ are the if statement, the else statement, and the switch statement.
 Loop statements: These statements allow the programmer to repeat a block of code
multiple number of time or until a certain condition is met. The most common loop
statements in C++ are the for loop, the while loop, and the do-while loop.
 Jump statements: These statements allow the programmer to jump to a different part
of the program. The most common jump statement in C++ is the goto statement.
 Labeled statements: These statements allow the programmer to give a name to a
specific point in the program. Labeled statements can be used with jump statements to
create more complex control flow structures.
3. What is the purpose of control statements in C++?
Control statements are essential for writing efficient and readable C++ programs. They allow
the programmer to control the flow of execution of the program, which is necessary for tasks
such as making decisions, repeating code, and jumping to different parts of the program.
4. How is the if statement different from the if-else statement?
The if statement only executes a block of code if a given condition is true. In contrast, the if-
else statement provides two blocks of code: one that executes if the condition is true and
another that executes if the condition is false.
5. Can I nest if statements inside each other?
Yes, you can nest if statements (or if-else statements) inside each other to create more
complex decision-making structures. This is often called "nested if."
6. What's the primary difference between while and do-while loops?
The main difference is in their evaluation of the loop's condition. A while loop evaluates the
condition before executing the loop body, whereas a do-while loop evaluates the condition
after the loop body has been executed, ensuring the loop body runs at least once.
7. When should I use the break and continue statements?
The break statement is used to exit from a loop or a switch statement prematurely. The
continue statement allows you to bypass the current iteration of a loop and proceed to the
next iteration, without exiting the loop entirely.

You might also like