SOFE103 W9 Loops
SOFE103 W9 Loops
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.
== Operator
int x = 10;
int y = 15;
int z = 10;
x == y // false
x == z // true
!= 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
int x = 10;
int y = 15;
x > y // false
y > x // true
< Operator
int x = 10;
int y = 15;
x < y // true
y < x // false
>= Operator
int x = 10;
int y = 15;
int z = 10;
x >= y // false
y >= x // true
z >= x // true
<= Operator
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.
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.
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!
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!
Here,
#include <iostream>
int main() {
for (int i = 1; i <= 5; ++i) {
cout << i << " ";
}
return 0;
}
Output
1 2 3 4 5
#include <iostream>
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!
#include <iostream>
int main() {
int num, sum;
sum = 0;
return 0;
}
Output
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.
Here,
+ ... + 10 .
Here, for every value in the collection , the for loop is executed and the
value is assigned to the variable .
#include <iostream>
int main() {
return 0;
}
Output
1 2 3 4 5 6 7 8 9 10
If the condition in a for loop is always true , it runs forever (until memory is
full). For example,
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;
Output