SLO Title & Number : 12 – Control Structures
• Number of Subtopics: 2
• Marks Weightage: 24%
• MCQ’s: 7
• CRQ’s: 3
• ERQ’s: 10
By: Najeeb Ather
12.1 Selection Statements in C++
Programming
By: Najeeb Ather
12.1.1 If Statement
• In C++ we use if statement to check any condition, if it results true, then the desired output will be shown.
• Syntax:
if(condition)
{
//Body of the if statement
}
//Statements execute one by one after the if statement
By: Najeeb Ather
12.1.1 If/Else Statement
• In if/else structure the block of code execute if the Else
condition if true and if it is false the other block of code
{
will be executed.
//Block of code if condition is false
• Syntax:
}
if(condition)
{
//Block of code if condition is true
}
By: Najeeb Ather
12.1.1 Else/If Statement
• In Else/If structure the block of code execute if the first Else
condition is true if it is not true then check the other
{
condition with else/if and so on. We can check any
number of conditions. //Block of code if condition is false
• Syntax: }
if(condition) else if(Condition)
{ {
//Block of code if condition is true //Block of code if another condition is true
} }
By: Najeeb Ather
12.1.2 If statement Program
int number = 6;
if( number > 5)
{
cout<<“The number is greater than 5”;
}
//Statements execute one by one after the if statement
By: Najeeb Ather
12.1.2 If/Else statement Program
int number = 6; else
if( number > 7) {
{ cout<<“The number is not greater
than 7”;
cout<<“The number is greater than 7”;
}
}
//Statements all ways execute not depending
upon what the result of condition.
By: Najeeb Ather
12.1.2 Else/If Statement Program
#include <iostream> if (number > 0) {
cout << "You entered a positive integer: " << number << endl;
using namespace std;
}
int main() {
else if (number < 0) {
int number; cout << "You entered a negative integer: " << number << endl;
cout << "Enter an integer: "; }
cin >> number;
By: Najeeb Ather
12.1.3 Using Nested If Statement Program
• We can add else and else if statement to the inner if statement if required.
By: Najeeb Ather
12.1.3 Using Nested If Statement Program
// C++ program to find if an integer is positive, // Starting outer if condition
negative or zero if (num != 0) {
// using nested if statements // Starting inner if condition
#include <iostream>
if (num > 0) {
using namespace std;
cout << "The number is positive." << endl;
int main() {
} // Starting inner else condition
int num;
else {
cout << "Enter an integer: ";
cout << "The number is negative." << endl;
cin >> num;
}
By: Najeeb Ather }
12.1.3 Using Nested If Statement Program
// outer else condition
else {
cout << "The number is 0 and it is neither positive nor negative." << endl;
}
cout << "This line is always printed." << endl;
return 0;
}
By: Najeeb Ather
12.1.4 Explain The Use of Switch Statement
• Switch statement is used to compare a
variable or expression to different values.
• The different values are called case
statements.
• The expression is evaluated only once and
compare to each single value/case of the
switch statement.
• Simply, it is the best alternative for if
statement.
By: Najeeb Ather
12.1.4 Explain The Use of Switch Statement
Syntax: case constant2:
switch (Value/expression) { // code to be executed if
case constant1: // expression is equal to constant2;
// code to be executed if break;
// expression is equal to constant1; ………………………
break; default:
// code to be executed if
// expression doesn't match any constant
}
By: Najeeb Ather
12.1.5 Using Switch Statement Program
#include <iostream> case 2:
using namespace std; cout << "Tuesday";
int main() { break; ………………………….
int day = 4; case 4:
switch (day) { cout << "Thursday";
case 1: break;
cout << "Monday"; case 5:
break; cout << "Friday";
break;
By: Najeeb Ather
12.1.5 Using Switch Statement Program
case 6:
cout << "Saturday";
break;
case 7:
cout << "Sunday";
break;
}
return 0;
}
By: Najeeb Ather
12.1.7 Describe the Role of Default and Break
keyword
Break Keyword:
• The break keyword is used in switch, for, do-while, while loop
to termination of the switch body and switch the control to
the next statement after the switch. If we did not use break
keyword then compiler will check all the cases with their
values/expression that match or not.
By: Najeeb Ather
12.1.7 Describe the Role of Default and Break
keyword
Break Keyword:
• It will stop execution of more code and case testing inside the block.
• Break save a lot of time because it ignores the rest of code in the switch block.
By: Najeeb Ather
12.1.7 Describe the Role of Default and Break
keyword
Default Keyword:
• The default keyword in switch statement is used if it did not find any case match.
• (As we match value/expression with all cases listed in switch statement.)
By: Najeeb Ather
12.1.8 Describe the Role of Exit and Return
Functions
Exit Function:
• Exit function is used to terminate the program.
• It terminate a process or function calling immediately in the program.
• I means any open file or function belonging to the process is closed immediately as the
exit() function is called.
By: Najeeb Ather
12.1.8 Describe the Role of Exit and Return
Functions
Return Function:
• It terminates the execution of a function and returns control to the calling function or
operating system if you transfer control from main function.
• Execution resumes in the calling function at the point immediately following the call.
By: Najeeb Ather
12.1.9 Rewrite a program having if/if-else,
else-if statement using switch statement
• It terminates the execution of a function
and returns control to the calling function
or operating system if you transfer control
from main function.
• Execution resumes in the calling function
at the point immediately following the call.
By: Najeeb Ather
12.2.2 Repetition Loop in C++ Programming
By: Najeeb Ather
12.2.1 List the Types of Loops Available in C++
Programming
For Loop: • Syntax:
• The for loop in C++ iterates a section of C++ for(initialization; condition;
code for the fixed number of times as long increment/decrement)
as condition remains true.
{
• For loop has THREE parts: initialization, //block of code which will execute each
condition and increment/decrement
time of iteration
}
By: Najeeb Ather
12.2.1 List the Types of Loops Available in C++
Programming
While Loop:
• The while loop in C++ repeats the block of code as many times as the condition remains
true.
• The loop will end when condition becomes false.
• While loop is also called entry controlled loop because condition is checked before the
enter in the loop body.
• It is best suitable when we didn’t know number of iterations in advance.
By: Najeeb Ather
12.2.1 List the Types of Loops Available in C++
Programming
While Loop:
Syntax:
while(condition)
{
//block of code which will execute each time of iteration
}
By: Najeeb Ather
12.2.1 List the Types of Loops Available in C++
Programming
Do-While Loop: • It is best suitable when we didn’t know
• The do-while loop in C++ repeats the block number of iterations in advance.
of code as many times as the condition
remains true. • The body of the loop will execute at least
once.
• The loop will end when condition becomes
false.
• While loop is also called exit controlled loop
because condition is checked after the end
of the loop statement.
By: Najeeb Ather
12.2.1 List the Types of Loops Available in C++
Programming
Do-While Loop:
• Syntax:
do
{
//block of code which will execute each time of iteration
} while (condition);
By: Najeeb Ather
12.2.2 Write a C++ program that uses for loop
#include <iostream>
using namespace std;
int main() {
for (int j = 1; j <= 5; j++) {
cout << j << " ";
}
return 0;
}
By: Najeeb Ather
12.2.3 Write a C++ program that uses while
loop
// C++ Program to print numbers from 1 to 5 // while loop from 1 to 5
while (j <= 5) {
#include <iostream>
cout << j << " ";
j++;
using namespace std;
}
int main() { return 0;
int j = 1; }
By: Najeeb Ather
12.2.4 Write a C++ program that uses do-
while loop
// C++ Program to print numbers from 1 to 5 // do...while loop from 1 to 5
do {
#include <iostream> cout << j << " ";
j++;
using namespace std;
}
while (j <= 5);
int main() {
int j = 1;
return 0;
}
By: Najeeb Ather
12.2.5 a-Difference between a for loop and a
while loop
For Loop While Loop
• For loop is used when the number of • While loop is used when number
iteration are known of iterations are not known
• For loop have counter • There is no built-in counter variable
variable/increment/decrement variable in while loop
inside the declaration itself
• Loop repeats infinite number of time • If condition is not specified it
if condition is not specified. shows compilation error.
By: Najeeb Ather
12.2.6 Write a C++ program in which break
statement is used
//Break statement break the loop when
condition is true.
cout<<"The loop is exit when j value is = "<<j<<endl;
#include <iostream>
//so, j value is print as 1 and 2 because at 3 it exit or
using namespace std; break.
int main() {
for (int j = 1; j <= 5; j++) { }
// break condition
if (j == 3) { return 0;
break; }
}
By: Najeeb Ather
12.2.7 Write a C++ program in which continue
statement is used
// Continue statement is used to skip the cout << j << endl;
iteration in the loop
}
#include <iostream>
using namespace std; return 0;
int main() { }
for (int j = 1; j <= 5; j++) {
// condition to continue
if (j == 3) {
continue;
}
By: Najeeb Ather
12.2.8 Write a C++ program in which exit()
function is used
#include <iostream> if (( z == 'n') || (z == 'N'))
using namespace std; {
int main() { cout<<"You have pressed n or N.....Play game
now: ";
char z;
}
cout<<"Want to quit: (y/n) ?";
cout<<"Statement-1 after the exit function :";
cin>> z;
return 0;
if (( z== 'y'|| z == 'Y'))
}
{
cout<<"You have pressed y or Y ";
exit(0);
}
By: Najeeb Ather
12.2.9 Write a program using nested for loop
// C++ program to draw a pattern for (int row = 1; row <= MaxRows; row++)
#include <iostream> {
using namespace std; for (int column = 1; column <= MaxColumns; column++)
int main() {
{ cout << "* ";
int row, column, MaxRows, MaxColumns; }
cout<<"Enter the number of rows: "; cout << endl;
cin>>MaxRows; }
cout<<"Enter the number of columns: "; return 0;
cin>>MaxColumns; }
By: Najeeb Ather