12th Class (HSSC-II) Computer SLO Based Key Point Notes
(National Book Foundation - As Federal Textbook Board, Islamabad Based on National Curriculum Pakistan 2023-2024 and Onward prescribed by Federal Board of Intermediate and Secondary Education, Islamabad, and All Pakistan Boards)
(National Book Foundation - As Federal Textbook Board, Islamabad
Based on National Curriculum Pakistan 2023-2024 and Onward prescribed by Federal Board of Intermediate and Secondary Education, Islamabad, and All Pakistan Boards)
{Contact WhatsApp # 03339719149 for Class-XII Computer Science Complete SLO-Based Key Points notes in pdf format as well as in PowerPoint Presentation for preparing/delivering the Lectures}
************************************
************************************
👉👉👉 Computer Science 12th Class Notes (Main Page)
************************************
Unit 4: Control Structures
Control Structure
- A statement used to control the flow of execution of instructions in a program is called a control structure.
- Control structures are used to implement the logic of a program.
There are three kinds of control structures: i.e.
1. Sequential Structure: In sequential structure, the statements of a program are executed in the same order in which they are written in the program.
2. Selection Structure: The selection structure is used for selecting a statement or group of statements for execution on the basis of a given condition. It is also known as decision structure.
3. Repetition structure: Repetition structure is used to execute a statement or group of statements repeatedly as long as the given condition is true. It is also known as iteration structure or Loop structure.
Types of Decision Structure
1. The if statement
The if statement is a decision-making statement that is used to execute or skip a statement by checking a condition.General Syntax:
if (condition){
statements;
}
Flow chart
Explanation:
- The condition is evaluated.
- If the condition is true, the block of statement within the braces is executed.
- If the condition is false, the block of statements is skipped and control is transferred to the next statement after the closing brace.
- If there is a single statement to be executed then braces are not required.
Example
Write a program that inputs marks and displays “Congratulations, You have passed” if the marks are 40 or more.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int marks;
cout<<“Please enter your Marks: “;
cin>>marks;
if (marks >= 40)
{
cout<<“Congratulations, You have passed”;
}
getch();
}
#include<conio.h>
void main()
{
clrscr();
int marks;
cout<<“Please enter your Marks: “;
cin>>marks;
if (marks >= 40)
{
cout<<“Congratulations, You have passed”;
}
getch();
}
Limitation of if statement
if the statement is the simplest selection structure which execute the statement or set of statements if the condition is true, but if the condition is false then nothing will happen.
The if-else statement
An if-else statement is another type of decision-making statement that executes one block of statement when the condition is true and the other when it is false.
General Syntax
if (condition){
statements;
}
else
{
statements;
}
Flow chart
Explanation
- The condition is evaluated.
- If the condition is true, the block of statements under “if” will be executed.
- If the condition is false, the block of statement under “else” will be executed.
- If there is a single statement to be executed whether the condition is true or false then braces are not required.
Example:
Write a program that inputs marks and displays the result. If the marks are greater than or equal to 40 then display pass otherwise Fail.#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int marks;
cout<<“Please enter your Marks: “;
cin>>marks;
if (marks >= 40)
{
cout<<“Congratulations, You have passed”;
}
else
{
cout<<“Sorry, Your are fail”;
}
getch();
}
#include<conio.h>
void main()
{
clrscr();
int marks;
cout<<“Please enter your Marks: “;
cin>>marks;
if (marks >= 40)
{
cout<<“Congratulations, You have passed”;
}
else
{
cout<<“Sorry, Your are fail”;
}
getch();
}
Similar Examples as an assignment:
- Write a program that reads a number and prints whether it is an even or odd number.
- Write a program that reads a number and prints its square if the number is greater than 10 otherwise prints its cube.
- Write a program that reads three numbers and prints the largest one.
- Write a program that reads a letter and prints whether it is a lowercase or uppercase letter.
else-if Statement
- else-if statement is used to choose one block of statements out of many blocks.
- It is used when there are many options and only one should be selected on the basis of a condition.
General Syntax
if (condition1){
Block of statement 1
}
else if (condition2)
{
Block of statement 2
}
else if (condition n)
{
Block of statement n
}
else
{
Block of statement n
}
Explanation
- In this structure, if any of the conditions is true, the program executes the statements under that if or else-if.
- If none of the conditions is true, the program executes the statements under the final else.
Flow Chart
Explanation
- Condition 1 is evaluated.
- If it is true, the block of statement 1 is executed and control is transferred to the next statement.
- If condition 1 is false then condition 2 is evaluated. If it is true then the block of statements 2 following condition 2 is executed.
- In this manner, conditions are evaluated one by one. When any condition is true, the block of the statement following that condition is executed, the rest of the code is skipped and control is transferred to the next statement.
- If none of the conditions is true then the last block of statements following the keyword else is executed. The else block is optional.
- If a single statement is to be executed then braces are not required.
Example 1
Write a program that inputs the marks of a student and prints his Grade according to the following scheme:
70 – 79 B
60 – 69 C
50 – 59 D
Below 50 F
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int marks;
cout<<“Enter your Marks: “;
cin>>marks;
if (marks >= 80)
cout<<“Your Grade is A\n”;
else if (marks >= 70)
cout<<“Your Grade is B\n”;
else if (marks >= 60)
cout<<“Your Grade is C\n”;
else if (marks >= 50)
cout<<“Your Grade is D\n”;
else
cout<<“Your Grade is F\n”;
getch();
}
Assignment
• Write a program that inputs two numbers and performs an arithmetic operation on those numbers by entering the choice. i.e.1. Addition
2. Subtraction
3. Multiplication
4. Division
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int num1, num2, result;
int option;
cout<<“Enter 1st Number”;
cin>>num1;
cout<<“Enter 2nd Number”;
cinn>>num2;
cout<<“1. Addition \n 2. Subtraction \n 3. Multiplication \n 4. Division \n”;
cout<<“Enter option 1-4: “;
cin>>option;
if(option == 1)
cout<<“Sum of numbers is: “<<num1+num2;
else if(option ==2)
cout<<“Difference of numbers is: “<<num1-num2;
else if(option == 3)
cout<<“Product of numbers is: “<<num1*num2;
else if (option == 4)
cout<<“Division of numbers is: “<<num1/num2;
else
cout<<“you have entered the wrong option”;
getch();
}
#include <conio.h>
void main()
{
clrscr();
int num1, num2, result;
int option;
cout<<“Enter 1st Number”;
cin>>num1;
cout<<“Enter 2nd Number”;
cinn>>num2;
cout<<“1. Addition \n 2. Subtraction \n 3. Multiplication \n 4. Division \n”;
cout<<“Enter option 1-4: “;
cin>>option;
if(option == 1)
cout<<“Sum of numbers is: “<<num1+num2;
else if(option ==2)
cout<<“Difference of numbers is: “<<num1-num2;
else if(option == 3)
cout<<“Product of numbers is: “<<num1*num2;
else if (option == 4)
cout<<“Division of numbers is: “<<num1/num2;
else
cout<<“you have entered the wrong option”;
getch();
}
Switch Statement
- The switch statement is another conditional structure that can be easily used when there are many choices available and only one should be executed.
- Nested if becomes very difficult in such a situation.
General Syntax
switch(variable/expression){
case val1:
statements;
break;
case val2:
statements;
break;
:
:
case valn:
statements;
break;
default:
statement;
}
case valn:
statements;
break;
default:
statement;
}
Explanation
- The switch statement starts with the keyword “switch”, followed by brackets containing an integer or character variable which is also known as a switch variable.
- It can also be an expression giving an integer value.
- Each “case” keyword is followed by a constant integer or character value which is terminated with a colon.
- There can be one or more statements following each case keyword.
Working of Switch statement
- The switch statement compares the value of the switch variable or the result of an expression with the constant values of each case.
- If it matches with any case, the corresponding block of statements is executed.
- The “default”, appears at the end of the switch statement, is executed only when the value of the switch variable or the result of an expression does not match with any case value. Its use is optional.
- The break statement in each case is used to terminate the switch statement when the body of statements in the particular case has been executed.
- If no break statement is in a case, then the control will fall through to all the following cases.
Example
Write a program that inputs two numbers and performs an arithmetic operation on those numbers by entering the choice. i.e.1. Addition
2. Subtraction
3. Multiplication
4. Division
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int num1, num2;
int option;
cout<<“Enter 1st Number”;
cin>>num1;
cout<<“Enter 2nd Number”;
cinn>>num2;
cout<<“1. Addition \n 2. Subtraction \n 3. Multiplication \n 4. Division \n”;
cout<<“Enter option 1-4: “;
cin>>option;
switch(option)
{
case 1:
cout<<“Sum of numbers is: “<<num1+num2;
break;
case 2
cout<<“Difference of numbers is: “<<num1-num2;
break;
case 3:
cout<<“Product of numbers is: “<<num1*num2;
break;
case 4:
cout<<“Division of numbers is “<<(float) num1/num2;
break;
default :
cout<<“INVALID OPTION \n“;
}
getch();
}
#include <conio.h>
void main()
{
clrscr();
int num1, num2;
int option;
cout<<“Enter 1st Number”;
cin>>num1;
cout<<“Enter 2nd Number”;
cinn>>num2;
cout<<“1. Addition \n 2. Subtraction \n 3. Multiplication \n 4. Division \n”;
cout<<“Enter option 1-4: “;
cin>>option;
switch(option)
{
case 1:
cout<<“Sum of numbers is: “<<num1+num2;
break;
case 2
cout<<“Difference of numbers is: “<<num1-num2;
break;
case 3:
cout<<“Product of numbers is: “<<num1*num2;
break;
case 4:
cout<<“Division of numbers is “<<(float) num1/num2;
break;
default :
cout<<“INVALID OPTION \n“;
}
getch();
}
➰➰ LOOP ➰➰
Loop
- The mechanism through which a statement or set of statements can be executed repeatedly is called a loop OR
- A statement or a set of statements that is executed repeatedly is known as a loop.
There are two essential elements of a loop:
1. A loop body that consists of the statement or a set of statements that is to be executed repeatedly.
2. A loop condition is used to determine when to stop the loop.
1. A loop body that consists of the statement or a set of statements that is to be executed repeatedly.
2. A loop condition is used to determine when to stop the loop.
Control variable:
- A variable, which is used in loop conditions to control the repetition of a loop is known as a loop control variable.
- The control variable must be initialized before the body of the loop.
- Its value must be changed inside the body of the loop so that the repetition of the loop can be controlled.
Types of Loop
C++ provides three types of different loops which are:1. For Loop
2. While loop
3. Do-while loop
2. While loop
3. Do-while loop
1. for Loop
for loop is used to execute a statement or set of statements repeatedly for a specified number of times. It is also known as Counter Loop.Syntax:
for( Initialization; Condition; Increment/Decrement ){
statement 1;
statement 2;
…
…
statement N;
}
statement N;
}
Working of “for loop”
The number of iterations or repetitions depends on the initialization, condition, and increment/decrement parts of the “for loop”.
The for loop is executed as follows:
- First, the initialization part is executed. It is executed only once when the control enters the loop.
- Then the condition is evaluated, if it is TRUE, then the body of the loop is executed.
- After executing the body of the loop, the increment/decrement part is executed that changes the value of a counter variable.
- Then control again transfers the condition part. This process continues while the condition is TRUE. The loop is terminated when the given condition becomes FALSE.
Write a program that displays “Pakistan” for five times.
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
for ( int n=1 ; n<=5 ; n++ )
{
cout<<“ \n PAKISTAN”;
}
getch();
}
Example # 2:
Write a program that reads an integer and prints its multiplicative table up to 20.#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int n;
cout<<“Enter a number: “;
cin>>n;
for (int i=1; i<=10; i++)
{
cout<<n<<“\tx”<<i<<“\t=“<<n*i<<“\n”;
}
getch();
}
Example # 3:
Write a program that inputs a number and finds its factorial using a for loop.#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int n,f=1;
cout<<“Enter an integer number: “;
cin>>n;
for (int c =1; c<=n; c++)
{
f = f * c;
}
cout<<“\n\tFactorial of a number is: “<<f;
getch();
}
While loop
- While loop is used to execute one or more statements repeatedly as long as the given condition remains TRUE.
- It is useful when the number of repetitions or iterations is not known in advance.
- It is the simplest loop of C.
Condition: The condition is given as a relational expression that controls the repetition of a loop. If the condition is TRUE, the body of the loop is executed. If the condition is FALSE, the body of the loop is not executed.
Loop Body: It consists of the statements that are to be executed repeatedly as long as the given condition is TRUE.
Working of while Loop
“while loop” is executed as follows:- First, the condition is evaluated.
- If the condition is TURE, the body of the loop is executed. After executing the body, the control goes back to evaluate the condition.
- Step 1 and step 2 will be repeated until the condition becomes FALSE. When the condition is FALSE, the loop is terminated or exited.
Flow chart of “while loop”
Example 1:
Write a program that displays “Pakistan” five times using a while loop.#include <iosteam.h>
#include <conio.h>
void main()
{
clrscr();
int n = 1;
while (n<=5)
{
cout<<“\n Pakistan”;
n++;
}
getch();
}
#include <conio.h>
void main()
{
clrscr();
int n = 1;
while (n<=5)
{
cout<<“\n Pakistan”;
n++;
}
getch();
}
Example 2:
Write a program that prints the sum of all the positive numbers up to 15 using a while loop.#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int k=1, sum=0;
while(k<=15)
{
sum = sum + k;
k++;
}
cout<<“Sum = “<<sum<<endl;
getch();
}
#include <conio.h>
void main()
{
clrscr();
int k=1, sum=0;
while(k<=15)
{
sum = sum + k;
k++;
}
cout<<“Sum = “<<sum<<endl;
getch();
}
do-while loop
- The do-while loop is used to execute one or more statements repeatedly as long as the given condition is TRUE.
- It is similar to a “while loop” but in the do-while loop, the body of the loop comes before the condition.
- In a do-while loop, the body of the loop executes at least once even if the given condition is FALSE.
Syntax:
do{
statement 1;
…………
…………
}
while ( condition );
Working of do-while Loop
- The block of statements following the keyword do is executed.
- The condition at the end of the loop is evaluated. If the condition is true, control is transferred back to the beginning of the loop.
- The loop is executed once again and then the condition is also checked again. This process continues till the condition becomes false.
- When the condition becomes false, control is transferred to the next statement.
- If the body of the loop consists of a single statement then braces are not required.
Flow chart of “do-while” loop
Example # 1:
Write a program that displays the first five numbers with their cubes.#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int c = 1;
do
{
cout<<“\n”<<c*c*c;
c++;
}
while ( c <= 5);
getch();
}
Example # 2:
Write a program that reads the current state of a telephone line. The user should enter ‘w’ for working and ‘d’ for dead. Any other input will be invalid.
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
char ch;
do
{
cout<<“ \n Enter the state of phone: w for working and d for dead: “;
ch = getche();
}
while ( ch != ‘w’ && ch != ‘d’);
getch();
}
Differentiate between while and do-while loop
The break and Continue Statement
The break Statement:
The break statement has two usages:- It is used to terminate a case in the switch statement and program execution continues from the next statement following switch statement.
- The break statement is also used to terminate a loop when it is encountered inside a loop and program execution continues from the next statement following the loop.
The continue Statement:
The continue statement is used inside a loop, which transfers control to the beginning of the loop, skipping the remaining statements.
Example # 1:
#include <iostream.h>#include <conio.h>
void main()
{
clrscr();
for (int k=1; k<=10; k++)
{
cout<<k<<“\n”;
if(k == 4)
break;
}
getch();
}
Example # 2:
#include <iostream.h>#include <conio.h>
void main()
{
clrscr();
for(int n=1;n<=15;n++)
{
if((n>5) && (n<10))
continue;
cout<<n<<endl;
}
}
}
}
4.2.6 EXIT() FUNCTION
The exit() function is used to terminate a C++ program before its normal termination and exit to the operating system. It requires the standard library header file stdlib.h.
The following is the general form of the exit function.
exit (value);
Here, value is an integer value or integer variable. It is known as the exit code. The exit code "0" exits a program without any error and exit code "1" indicates that an error must have occurred. It helps the programmer in debugging the program.
Consider the following if-else statement. if(n>0) cout<<n<<"is a positive number"; else
exit(0);
In the above if-else statement, the control function exit is used to terminate the execution of the if-else statement if the value of n is not greater than 0 and returns to the operating system.
Nested Loop
- A loop within another loop is called a nested loop.
- The loop that contains another loop in its body is called the outer loop.
- The loop used inside the body of the outer loop is called the inner loop.
- In a nested loop, the inner loop is completely executed for each iteration of the outer loop.
- Any loop can be placed inside the body of any other loop.
The general syntax of the nested loop is as follows:
Outer Loop
{
Inner Loop
{ }
}
{
Inner Loop
{ }
}
Working of nested loop
The nested loop is executed as follows:1. The condition given in the outer loop is evaluated. If the condition is TRUE, controls enter inside the body of the outer loop. Otherwise, the loop is terminated.
2. If the body of the outer loop contains any statement before the inner loop, it is executed.
3. The condition of the inner loop is evaluated.
- When the condition is TRUE, the body of the inner loop is completely executed repeatedly until the given condition remains TRUE.
- When the condition of the inner loop is FALSE, the inner loop is terminated.
- If there is any statement after the body of the inner loop, it is executed, and then control transfers at the beginning of the outer loop.
4. Steps 1 to 3 are repeated until the given condition of the outer loop remains TRUE.
Example # 1:
#include <iostream.h>#include <conio.h>
void main()
{
clrscr();
for (int i = 1; i<=2; i++)
{
cout<<“\n PAKISTAN”;
for ( int j=1; j<=3; j++)
{
cout<<“\n ZINDABAD”;
}
}
getch();
}
Example # 2:
Write a program that displays the following shape using nested for loops. *
* *
* * *
* * * *
* * * * *
* *
* * *
* * * *
* * * * *
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
for (int i = 1; i<=5; i++)
{
cout<<“\n”;
for (int j = 1; j<=i; j++)
{
cout<<“ *”;
}
}
getch();
}
#include <conio.h>
void main()
{
clrscr();
for (int i = 1; i<=5; i++)
{
cout<<“\n”;
for (int j = 1; j<=i; j++)
{
cout<<“ *”;
}
}
getch();
}
************************************👉👉👉 Computer Science 12th Class Notes (Main Page)
************************************
Shortcut Links For:
1. 5th Class All Subjects Notes
2. 8th Class All Subjects Notes
3. Easy English Grammar Notes
1. Website for School and College Level Physics 2. Website for School and College Level Mathematics 3. Website for Single National Curriculum Pakistan - All Subjects Notes
© 2023 & onwards Academic Skills and Knowledge (ASK)
Note: Write me in the comments box below for any query and also Share this information with your class-fellows and friends.
1. Website for School and College Level Physics
2. Website for School and College Level Mathematics
3. Website for Single National Curriculum Pakistan - All Subjects Notes
0 Comments
Start Tuition (in Pakistan or Abroad) from Experienced Online Teachers.
Contact WhatsApp for tutoring and Assignment help on +923339719149