0% found this document useful (0 votes)
38 views14 pages

SOFE103 W9 Loops

This document provides information on relational and logical operators in C++. It defines relational operators as comparing two operands and returning true or false. The common relational operators are listed along with their meanings. Logical operators are used to check if an expression is true or false, and the common logical operators and their meanings are defined. Examples of using relational and logical operators are also provided.

Uploaded by

bashkebbay
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)
38 views14 pages

SOFE103 W9 Loops

This document provides information on relational and logical operators in C++. It defines relational operators as comparing two operands and returning true or false. The common relational operators are listed along with their meanings. Logical operators are used to check if an expression is true or false, and the common logical operators and their meanings are defined. Examples of using relational and logical operators are also provided.

Uploaded by

bashkebbay
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
You are on page 1/ 14

C++ Relational and Logical Operators

In this tutorial, we will learn about relational and logical operators with the
help of examples.

In C++, relational and logical operators compare two or more operands and
return either true or false values.
We use these operators in decision making.

C++ Relational Operators


A relational operator is used to check the relationship between two
operands. For example,

// checks if a is greater than b


a > b;

Here, > is a relational operator. It checks if a is greater than b or not.


If the relation is true, it returns 1 whereas if the relation is false, it returns 0.
The following table summarizes the relational operators used in C++.

Operator Meaning Example

== Is Equal To 3 == 5 gives us false

!= Not Equal To 3 != 5 gives us true

> Greater Than 3 > 5 gives us false

< Less Than 3 < 5 gives us true


>= Greater Than or Equal To 3 >= 5 give us false

<= Less Than or Equal To 3 <= 5 gives us true

== Operator

The equal to == operator returns


 true - if both the operands are equal or the same
 false - if the operands are unequal
For example,

int x = 10;
int y = 15;
int z = 10;

x == y // false
x == z // true

Note: The relational operator == is not the same as the assignment


operator = . The assignment operator = assigns a value to a variable,
constant, array, or vector. It does not compare two operands.

!= Operator
The not equal to != operator returns
 true - if both operands are unequal
 false - if both operands are equal.
For example,

int x = 10;
int y = 15;
int z = 10;

x != y // true
x != z // false

> Operator

The greater than > operator returns


 true - if the left operand is greater than the right
 false - if the left operand is less than the right
For example,

int x = 10;
int y = 15;

x > y // false
y > x // true

< Operator

The less than operator < returns


 true - if the left operand is less than the right
 false - if the left operand is greater than right
For example,

int x = 10;
int y = 15;

x < y // true
y < x // false

>= Operator

The greater than or equal to >= operator returns


 true - if the left operand is either greater than or equal to the right
 false - if the left operand is less than the right
For example,

int x = 10;
int y = 15;
int z = 10;

x >= y // false
y >= x // true
z >= x // true

<= Operator

The less than or equal to operator <= returns


 true - if the left operand is either less than or equal to the right
 false - if the left operand is greater than right
For example,

int x = 10;
int y = 15;

x > y // false
y > x // true

In order to learn how relational operators can be used with strings, refer to
our tutorial here.

C++ Logical Operators


We use logical operators to check whether an expression is true or false. If
the expression is true, it returns 1 whereas if the expression is false, it
returns 0.
Operator Example Meaning

Logical AND.
&& expression1 && expression 2
true only if all the operands are true.

Logical OR.
|| expression1 || expression 2
true if at least one of the operands is true.

Logical NOT.
! !expression
true only if the operand is false.

C++ Loops
Loops can execute a block of code as long as a specified condition is reached.

Loops are handy because they save time, reduce errors, and they make code
more readable.

C++ While Loop


The while loop loops through a block of code as long as a specified condition
is true:

Syntax
while (condition) {
// code block to be executed
}

In the example below, the code in the loop will run, over and over again, as
long as a variable (i) is less than 5:

Example
int i = 0;
while (i < 5) {
cout << i << "\n";
i++;
}

Note: Do not forget to increase the variable used in the condition, otherwise
the loop will never end!

The Do/While Loop


The do/while loop is a variant of the while loop. This loop will execute the
code block once, before checking if the condition is true, then it will repeat
the loop as long as the condition is true.

Syntax
do {
// code block to be executed
}
while (condition);
The example below uses a do/while loop. The loop will always be executed at
least once, even if the condition is false, because the code block is executed
before the condition is tested:

Example
int i = 0;
do {
cout << i << "\n";
i++;
}
while (i < 5);

Do not forget to increase the variable used in the condition, otherwise the
loop will never end!

C++ for loop


The syntax of for-loop is:

for (initialization; condition; update) {


// body of-loop
}

Here,

 initialization - initializes variables and is executed only once


 condition - if true , the body of for loop is executed
if false , the for loop is terminated
 update - updates the value of initialized variables and again checks the
condition

Flowchart of for Loop in C++


Flowchart of for loop in C++
Example 1: Printing Numbers From 1 to 5

#include <iostream>

using namespace std;

int main() {
for (int i = 1; i <= 5; ++i) {
cout << i << " ";
}
return 0;
}

Output

1 2 3 4 5

Here is how this program works


Iteration Variable i <= 5 Action
1st i = 1 true 1 is printed. i is increased to 2.
2nd i = 2 true 2 is printed. i is increased to 3.
3rd i = 3 true 3 is printed. i is increased to 4.
4th i = 4 true 4 is printed. i is increased to 5.
5th i = 5 true 5 is printed. i is increased to 6.
6th i = 6 false The loop is terminated

Example 2: Display a text 5 times

// C++ Program to display a text 5 times

#include <iostream>

using namespace std;

int main() {
for (int i = 1; i <= 5; ++i) {
cout << "Hello World! " << endl;
}
return 0;
}

Output

Hello World!
Hello World!
Hello World!
Hello World!
Hello World!

Here is how this program works


Iteration Variable i <= 5 Action
1st i = 1 true Hello World! is printed and i is increased to 2.
2nd i = 2 true Hello World! is printed and i is increased to 3.
3rd i = 3 true Hello World! is printed and i is increased to 4.
4th i = 4 true Hello World! is printed and i is increased to 5.
5th i = 5 true Hello World! is printed and i is increased to 6.
6th i = 6 false The loop is terminated

Example 3: Find the sum of first n Natural Numbers

// C++ program to find the sum of first n natural numbers


// positive integers such as 1,2,3,...n are known as natural numbers

#include <iostream>

using namespace std;

int main() {
int num, sum;
sum = 0;

cout << "Enter a positive integer: ";


cin >> num;

for (int i = 1; i <= num; ++i) {


sum += i;
}

cout << "Sum = " << sum << endl;

return 0;
}

Output

Enter a positive integer: 10


Sum = 55

In the above example, we have two variables num and sum . The sum variable
is assigned with 0 and the num variable is assigned with the value provided
by the user.
Note that we have used a for loop.

for(int i = 1; i <= num; ++i)

Here,

 int i = 1 : initializes the i variable


 i <= num : runs the loop as long as i is less than or equal to num

 ++i : increases the i variable by 1 in each iteration


When i becomes 11 , the condition is false and sum will be equal to 0 + 1 + 2

+ ... + 10 .

Ranged Based for Loop


In C++11, a new range-based for loop was introduced to work with
collections such as arrays and vectors. Its syntax is:

for (variable : collection) {


// body of loop
}

Here, for every value in the collection , the for loop is executed and the
value is assigned to the variable .

Example 4: Range Based for Loop

#include <iostream>

using namespace std;

int main() {

int num_array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

for (int n : num_array) {


cout << n << " ";
}

return 0;
}

Output

1 2 3 4 5 6 7 8 9 10

In the above program, we have declared and initialized an int array


named num_array . It has 10 items.
Here, we have used a range-based for loop to access all the items in the
array.
C++ Infinite for loop

If the condition in a for loop is always true , it runs forever (until memory is
full). For example,

// infinite for loop


for(int i = 1; i > 0; i++) {
// block of code
}

In the above program, the condition is always true which will then run the
code for infinite times.
Example: Sum of Natural Numbers using loop

#include <iostream>
using namespace std;

int main() {
int n, sum = 0;

cout << "Enter a positive integer: ";


cin >> n;

for (int i = 1; i <= n; ++i) {


sum += i;
}

cout << "Sum = " << sum;


return 0;
}

Output

Enter a positive integer: 50


Sum = 1275

This program assumes that user always enters positive number.

If user enters negative number, Sum = 0 is displayed and program is


terminated.

You might also like