0% found this document useful (0 votes)
9 views26 pages

C++ Control Statements Explained

The document provides an overview of control statements in C++, including decision-making statements like IF, IF-ELSE, ELSE IF Ladder, Nested IF, and SWITCH statements. It also covers loops such as FOR, WHILE, and DO-WHILE, along with examples and explanations for each type. Additionally, it discusses jump statements like GOTO, BREAK, and CONTINUE, detailing their usage and syntax.

Uploaded by

KUMUTHINI C
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)
9 views26 pages

C++ Control Statements Explained

The document provides an overview of control statements in C++, including decision-making statements like IF, IF-ELSE, ELSE IF Ladder, Nested IF, and SWITCH statements. It also covers loops such as FOR, WHILE, and DO-WHILE, along with examples and explanations for each type. Additionally, it discusses jump statements like GOTO, BREAK, and CONTINUE, detailing their usage and syntax.

Uploaded by

KUMUTHINI C
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

CONTROL STATEMENTS IN C++

Introduction :

 C++ Programs can be executed sequentially in the order in which they appear.
 In order to change the order of execution of statements based on certain conditions is
called as Decision Making Statement.
 According to the conditions statement will be executed.

Types:

java support the following decision making Statements are


1. IF Statement
2. IF ELSE Statement
3. ELSE IF Ladder Statement
4. Nested IF Statement
[Link] Statement

1. IF Statement
Definition
if statements is used to execute some code, if a condition is true otherwise if
condition is false, execute nothing.
Syntax-1:
If(test condition)
Statement;
Syntax-2:
If(test condition)
{
Statement;
}

Flowchart
Explanation
[Link] the test condition is true, the statement will be executed
2. If the test condition is false, the statement –x will be executed
3. When the test condition is true, both statement and the statement –x will be executed

Example 1

#include <iostream>
using namespace std;

int main() {
int a = 15, b = 20;

if (b > a) { // true
cout << "b is greater";
}
cout << " end";
}
Output
b is greater end
2. IF–ELSE Statement

Definition: If else statement is used to check the condition whether it is true


or false

Syntax-1:
if( Test condition)
{
Statement -1;
}
else
{
Statement -2;
}
Statement –X;

FLOWCHART

Explanation
1. If the test condition is true, the statement -1 and statement –x will be executed
2. if the test condition is false, the statement -2 and statement –x will will be
executed
3. In either case, Statement-1 or statement-2 will be executed , not both
Example

#include <iostream>
using namespace std;

int main() {
int mark;
cin >> mark;

if (mark > 40) {


cout << "Pass\n";
}
else {
cout << "Fail\n";
}
cout << "end";
}

Output:1

55

Pass end

Output: 2
33
Fail end

3. Else–If Ladder
Definition:
Else if ladder is used to check several test condition
Syntax:
if( Test condition-1)
{
Statement -1;
}
else if( Test condition-2)
{
Statement -2;
}
else if( Test condition-3)
{
Statement -3;
}
else
{
Default Statement;
}
Statement-x;
Flowchart
Explanation
1. If the test condition -1 is true, the statement -1 and statement –x will be executed
2. If the test condition -1 is false, the test condition 2 will be checked
3. If the test condition-2 is true, the statement -2 and statement –x will be executed
4. If the test condition -2 is false, the test condition 3 will be checked and soon
5. All the test condition are false, the default statement will be executed
Example

#include <iostream>
using namespace std;

int main() {
int day;
cin >> day;

if (day == 1)
cout << "Monday";
else if (day == 2)
cout << "Tuesday";
else if (day == 3)
cout << "Wednesday";
else
cout << "Nothing";
}

Output:1
2
Tuesday

Output:2
4
nothing

output:3
1
monday

4. Nested if
Definition:
if statement with in another if statement is called as nested if

Syntax:1
if( Test condition-1)
{
if( Test condition-2)
{
Statement -1;
}
else
{
Statement -2;
}
else
{
Statement -3;
}
Statement -x;

Syntax:2
If(test condition)
{
If(test condition)
{
Statement;
}
Statement;
}
Flowchart

Explanation
1. If the test condition -1 is true, the Test condition -2 will be checked
2. If the test condition -2 is true, the statement -1 and statement –x will be executed
3. If the test condition -2 is false, the statement -2 and statement –x will be executed
4. If the test condition -1 is false, the statement -3 and statement –x will be executed

Example

#include <iostream>
using namespace std;

int main() {
int m = 16, n = 10;

if (m > n) {
if (m - n > 5)
cout << "m is much greater than n";
else
cout << "m is slightly greater";
}
else {
cout << "n is greater";
}
}

Output

m is much greater than n

5. SWITCH Statement
Definition:
Switch statement is a control statement that allows us to choose only one choice
among the many given choices
Syntax:
switch(expression)
{
case value-1:
block-1;
break;
case value-2:
block-2;
break;
case value-3:
block-3;
break;
case value-4:
block-4;
break;
default:
default-block;
break;
}

Flowchart

Explanation
1. The expression (after switch keyword) must yield an integer value i.e it can be an
integer or a variable or an expression that evaluates to an integer.
2. The case label i.e values must be unique.
3. The case label must end with a colon(:)
4. break statements are used to exit the switch block
5. When a switch expression value is match with any one of the case value, control
of the program passes to the block of code associated with that case.
6. The compiler will execute the block of code associate with the case statement
until the end of switch block, or until the break statement is encountered.
Example 1
#include <iostream>
using namespace std;

int main() {
char grade = 'B';

switch(grade) {
case 'A': cout << "Excellent"; break;
case 'B': cout << "Well done"; break;
case 'C': cout << "Good"; break;
case 'D': cout << "Needs improvement"; break;
default: cout << "Invalid grade";
}
}

Output
Well done

Example 2: No break
#include <iostream>
using namespace std;

int main() {
int i = 2;

switch(i) {
case 1: cout << "Case1 ";
case 2: cout << "Case2 ";
case 3: cout << "Case3 ";
default: cout << "Default ";
}
}

Output:

Case2 Case3 Default


✅ LOOPS IN C++

Types of Loops

✔ for loop
✔ while loop
✔ do-while loop

1. For Loop
Defintion:

Loops in C programming are used to repeat a block of code until the specified
condition is met. It allows programmers to execute a statement or group of statements
multiple times without writing the code again and again.

Types:
There are three types of loops in Java.
• for loop
• while loop
• do-while loop

1. For Loop
Definition:
 it is used to execute the same statements several times
 The for… loop is used to execute a block of a specified number of times

Syntax:
for(initialization ; condition ; increment/decrement)
{
//statement or code to be executed
}

Example:
for(i=0;i<4;i++)
{
}

Explanation:
1. For Loop in Java is a statement which allows code to be repeatedly
executed.
2. For loop contains 3 parts 1. Initialization, 2. Condition and 3. Increment or
Decrements

Control flow of for loop


 First initialize the variable

 In second step check condition

 In third step control goes inside loop body and execute.

 At last increase the value of variable

 Same process is repeat until condition not false.

Flowchart

Explanation:
1. Initialization: It is the initial condition which is executed once when the loop
starts. Here, we can initialize the variable, or we can use an already initialized
variable. It is an optional condition.
2. Condition: It is the second condition which is executed each time to test the
condition of the loop. It continues execution until the condition is false. It must
return boolean value either true or false. It is an optional condition.
3. Increment/Decrement: It increments or decrements the variable value. It is an
optional condition.
4. Statement: The statement of the loop is executed each time until the second
condition is false.

Example 1: Print 1 to 10
#include <iostream>
using namespace std;

int main() {
for (int i = 1; i <= 10; i++) {
cout << i << endl;
}
}

Output:
1
2
3
4
5
6
7
8
9
10

Example 2: Even numbers (2 to 10)


for (int i = 2; i <= 10; i += 2)
cout << i << endl;

output:
2
4
6
8
10

Example 3: Odd numbers (1 to 15)


for (int i = 1; i <= 15; i += 2)
cout << i << endl;

output:
1
3
5
7
9
11
13
15

3. Nested For Loop

Definition: loop with in another loop

Syntax:

 // outer loop
 for (int i = 1; i <= 5; ++i) {
 // codes

 // inner loop
 for(int j = 1; j <=2; ++j) {
 // codes
 }
 ..
 }
 Explanation:
If we have a for loop inside the another loop, it is known as nested for loop. The
inner loop executes completely whenever outer loop executes.

Example
#include <iostream>
using namespace std;

int main() {
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
cout << i << " " << j << endl;
}
}
}

Output

11

12

13

21

22

23

31

32

33

3. While Loop
Defintion

A while loop is also an entry-controlled loop in which the condition is checked before
entering the body.
Syntax
while (condition) {
// Body of the loop
}
Only the condition is the part of while loop syntax, we have to initialize and update
loop variable manually.
Flowchart of while Loop

Example
#include <iostream>
using namespace std;

int main() {
int i = 0;

while (i <= 5) {
cout << i << " ";
i++;
}
}

Output

12345

4. Do–While Loop
The do-while loop is an exit-controlled loop, which means that the condition is
checked after executing the loop body. Due to this, the loop body will execute at least
once irrespective of the test condition.

Syntax
do {
// Body of the loop
} while (condition);
Like while loop, only the condition is the part of do while loop syntax, we have to do
the initialization and updating of loop variable manually.

Flowchart of do-while Loop


The below flowchart demonstrates execution flow of the do while loop.

Example

#include <iostream>
using namespace std;

int main() {
int i = 0;

do {
cout << i << " ";
i++;
} while (i <= 10);
}

Output

0 1 2 3 4 5 6 7 8 9 10

5. Infinite Loops
Infinite Loop
An infinite loop is executed when the test expression never becomes false, and the
body of the loop is executed repeatedly. A program is stuck in an Infinite loop when the
condition is always true. Mostly this is an error that can be resolved by using Loop
Control statements.

Using for

for( ; ; )
{
cout << "This loop runs forever\n";
}

Using while

while (true)
{
cout << "Infinite\n";
}

Using do-while

do {
cout << "Infinite\n";
} while (true);
✅ JUMP STATEMENTS

1. GOTO Statement

📌 Not recommended in modern C++, but still allowed.

Definition:
This statement does not contain any condition. This statement passes control anywhere in
the program without care for any condition.
Syntax-1: (forward Jump)
goto label;

label:
Statement;
Syntax-2: (Backward Jump)
label:
Statement;

goto label;
Label : can be given anywhere in the program either before or after the goto label;
statement

Forward Jump
if the label: is placed after the goto label;
some statement will be skipped and the jump is known as forward jump
Flowchart
Example: Even or Odd
#include <iostream>
using namespace std;

int main() {
int x;
cin >> x;

if (x % 2 == 0)
goto even;
else
goto odd;

even:
cout << "Even number";
return 0;

odd:
cout << "Odd number";
}

Output

Even number
Explanation

1. The given value of x is checked for even or odd number with the modulo division
operator

2. When the number is even, goto statement transfer the control to the label even

3. When the number is odd, goto statement transfer the control to the label odd and
the particular messages will be displayed

Backward Jump

if the label: is placed before the statement goto label; a loop will be
formed and some statement will be skipped is known as backward jump

Example

Void main()

int age;

Vote:

Cout<<”you are eligible for voting";

NoVote:

Cout<<”you are not eligible to vote";

Cout<<”Enter you age:";

Cin>>age;

if(age>=18)

goto Vote;

Else

goto NoVote;

Output:
22

You are eligible for voting

Break and continue in C++

[Link] statement
 Definition: The break statement can also be used to jump out of a loop.

Syntax:
break;

 The break statement is almost always used with if...else statement inside the loop.

Used to exit a loop or switch.

Example
for (int i = 0; i < 5; i++) {
if (i == 3)
break;
cout << i;
}

Output: 012

[Link] statement:
Definition:

The continue statement in C is a jump statement used to skip the current iteration of a
loop and continue with the next iteration.

Syntax:

Continue

Example

Used to skip the current iteration.

Example
for (int i = 0; i < 5; i++) {
if (i == 3)
continue;
cout << i;
}

Output: 0124

You might also like