Nothing Special   »   [go: up one dir, main page]

PST Unit III

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

UNIT III

RELATIONAL OPERATORS

In Python Comparison of Relational operators compares the values. It either


returns True or False according to the condition.

Operator Description Syntax

> 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

== Equal to: True if both operands are equal x == y

!= Not equal to – True if operands are not equal x != y

Greater than or equal to True if the left operand is greater than or


>= x >= y
equal to the right

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.

Operator Description Syntax

and Logical AND: True if both the operands are true x and y

or Logical OR: True if either of the operands is true x or y

not Logical NOT: True if the operand is false not x


Example:

a = True
b = False

# Print a and b is False


print(a and b)

# Print a or b is True
print(a or b)

# Print not a is False


print(not a)

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.

Following are the decision-making statements available,

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.

Syntax of if else in C/C++

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.

Syntax of Nested if-else

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

Syntax of 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

o Control statements serve to control the execution flow of a program.


o Control statements in C are classified into selection statements, iteration statements, and
jump statements.
o Selection statements serve to execute code based on a certain circumstance.

o Control statements are used extensively in programming, especially in more complex


programs. By managing the flow of execution based on particular conditions, they enable
a programmer to design more efficient and understandable code.

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");
}

for (int i = 0; i < 5; i++) {


printf("%d\n", i);
}

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

The number is positive


0
1
2
3
4
0
1
2
3
4
The number is ten
REPETION STRUCTURES

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 and do-while loops

Format of while loop:

while (expression)
statement

Format of do/while loop:

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

Example for Do While

int i = 1;
do

cout<<i;

While(i>5);

COUNTER CONTROLLED LOOP

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);

Count-controlled repetition requires

 control variable (or loop counter)


 initial value of the control variable
 increment (or decrement) by which the control variable is modified each iteration through
the loop
 condition that tests for the final value of the control variable

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.

Count-controlled repetition is often called definite repetition

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

adj = ["red", "big", "tasty"]


fruits = ["apple", "banana", "cherry"]

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]

# initializing a variable that will store the sum


sum_ = 0

# using for loop to iterate over the list


for num in numbers:
sum_ = sum_ + num ** 2
print("The sum of squares is: ", sum_)

Output:

The sum of squares is: 774


C Program: Print table for the given number using C for loop

#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

You might also like