PST Unit III
PST Unit III
PST Unit III
RELATIONAL OPERATORS
> Greater than: True if the left operand is greater than the right x>y
< Less than: True if the left operand is less than the right x<y
Less than or equal to True if the left operand is less than or equal to
<= x <= y
the right
Example
a = 13
b = 33
# a > b is False
print(a > b)
# a < b is True
print(a < b)
# a == b is False
print(a == b)
# a != b is True
print(a != b)
# a >= b is False
print(a >= b)
# a <= b is True
print(a <= b)
Output
False
True
False
True
False
True
LOGICAL OPERATORS
Python Logical operators perform Logical AND, Logical OR, and Logical NOT operations. It
is used to combine conditional statements.
and Logical AND: True if both the operands are true x and y
a = True
b = False
# Print a or b is True
print(a or b)
Output
False
True
False
SELECTION FROM SEVERAL ALTERNATIVES /DECISION CONTROL
STATEMENTS
The selection statements are also known as Decision control statements or branching statements.
The selection statement allows a program to test several conditions and execute instructions
based on which condition is true.
1. f Statement
2. if-else Statement
3. Nested if Statement
4. if-else-if Ladder
5. switch Statement
The if statement is the most simple decision-making statement. It is used to decide whether a
certain statement or block of statements will be executed or not i.e if a certain condition is true
then a block of statements is executed otherwise not.
Syntax of if Statement
if(condition)
{
// Statements to execute if
// condition is true
}
2. if-else statement
The if statement alone tells us that if a condition is true it will execute a block of statements
and if the condition is false it won’t.
if (condition)
{
// Executes this block if
// condition is true
}
else
{
// Executes this block if
// condition is false
}
Nested if-else
A nested if is an if statement that is the target of another if statement. Nested if statements
mean an if statement inside another if statement.
if (condition1)
{
// Executes when condition1 is true
if (condition2)
{
// Executes when condition2 is true
}
else
{
// Executes when condition2 is false
}
4. if-else-if Ladder
if (condition)
statement;
else if (condition)
statement;
.
.
else
statement;
5. switch Statement
The switch case statement is an alternative to the if else if ladder that can be used to execute
the conditional code based on the value of the variable specified in the switch statement. The
switch block consists of cases to be executed based on the value of the switch variable.
Syntax of switch
switch (expression)
{
case value1:
statements;
case value2:
statements;
....
....
....
default:
statements;
}
Applications of Selection Statements
Example
#include <stdio.h>
int main() {
int num = 10;
if (num > 0) {
printf("The number is positive\n");
} else {
printf("The number is negative\n");
}
int i = 0;
while (i < 5) {
printf("%d\n", i);
i++;
}
switch (num) {
case 0:
printf("The number is zero\n");
break;
case 10:
printf("The number is ten\n");
break;
default:
printf("The number is not zero or ten\n");
break;
}
return 0;
}
Output
Repetition Statements
Repetition statements are called loops, and are used to repeat the same code mulitple
times in succession.
The number of repetitions is based on criteria defined in the loop structure, usually a
true/false expression
The three loop structures in C++ are:
o while loops
o do-while loops
o for loops
Three types of loops are not actually needed, but having the different forms is convenient
while (expression)
statement
do
statement
while (expression);
The expression in these formats is handled the same as in the if/else statements discussed
previously (0 means false, anything else means true)
The "statement" portion is also as in if/else. It can be a single statement or a compund
statement (a block { } ).
We could also write the formats as follows (illustrating more visually what they look like
when a compound statement makes up the loop "body"):
Example:
int i = 1;
while (i <= 10)
{
Cout<<i;
i++;
}
Output:
1
2
3
4
5
6
7
8
9
10
int i = 1;
do
cout<<i;
While(i>5);
When we know how many times loop body will be executed known as Counter Controlled
Loop, for example - print natural numbers from 1 to 100, such kind of problem will be solved
using counter controlled loop.
int count;
for( count=1; count<=100; count++)
printf("%d",count);
A count-controlled repetition will exit after running a certain number of times. The count is kept
in a variable called an index or counter. When the index reaches a certain value (the loop bound)
the loop will end.
NESTED LOOP
A nested loop is a loop inside a loop. The "inner loop" will be executed one time for each
iteration of the "outer loop"
Syntax of Nested loop
Outer_loop
{
Inner_loop
{
// inner loop statements.
}
// outer loop statements.
}
Examples:
while(condition)
{
while(condition)
{
statement(s);
}
statement(s);
}
Python Example
for x in adj:
for y in fruits:
print(x, y)
Output
red apple
red banana
red cherry
big apple
big banana
big cherry
tasty apple
tasty banana
tasty cherry
Applications of Repetitive Statements
# Code to find the sum of squares of each element of the list using for loop
# creating the list of numbers
numbers = [3, 5, 23, 6, 5, 1, 2, 9, 8]
Output:
#include<stdio.h>
int main()
{
int i=1,number=0;
printf("Enter a number: ");
scanf("%d",&number);
for(i=1;i<=10;i++)
{
printf("%d \n",(number*i));
}
return 0;
}
Output
Enter a number: 2
2
4
6
8
10
12
14
16
18
20