0% found this document useful (0 votes)
19 views

03-Operators in C++,control Statement, Loops

Operators are used to perform operations on variables and values in C++. The main types of operators include arithmetic, assignment, comparison, logical, and bitwise operators. Common operations include addition, subtraction, multiplication, division, incrementing/decrementing values, comparing values, and performing logical and bitwise operations. C++ also supports conditional statements like if-else and switch statements to execute code under certain conditions. Loops like while, do-while, and for loops are used to repeat blocks of code.

Uploaded by

Faneeshwar Patel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

03-Operators in C++,control Statement, Loops

Operators are used to perform operations on variables and values in C++. The main types of operators include arithmetic, assignment, comparison, logical, and bitwise operators. Common operations include addition, subtraction, multiplication, division, incrementing/decrementing values, comparing values, and performing logical and bitwise operations. C++ also supports conditional statements like if-else and switch statements to execute code under certain conditions. Loops like while, do-while, and for loops are used to repeat blocks of code.

Uploaded by

Faneeshwar Patel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Operators in C++ Programming

C++ Operators

✔ Operators are used to perform operations on variables and values.

✔ C++ divides the operators into the following groups:


✔ Arithmetic operators
✔ Assignment operators
✔ Comparison operators
✔ Logical operators
✔ Bitwise operators
Arithmetic Operators

✔ Arithmetic operators are used to perform common mathematical operations.

Operator Name Description Example


+ Addition Adds together two values x+y
- Subtraction Subtracts one value from another x-y
* Multiplication Multiplies two values x*y
/ Division Divides one value by another x/y
% Modulus Returns the division remainder x%y
++ Increment Increases the value of a variable by 1 ++x
-- Decrement Decreases the value of a variable by 1 --x
Assignment Operators

✔ A list of all assignment operators

Operator Example Same As


= x=5 x=5
+= x += 3 x=x+3
-= x -= 3 x=x-3
*= x *= 3 x=x*3
/= x /= 3 x=x/3
%= x %= 3 x=x%3
&= x &= 3 x=x&3
|= x |= 3 x=x|3
^= x ^= 3 x=x^3
>>= x >>= 3 x = x >> 3
<<= x <<= 3 x = x << 3
Comparison Operators
✔ Comparison operators are used to compare two values.
✔ Note: The return value of a comparison is either true (1) or false (0).
✔ A list of all comparison operators:

Operator Name Example


== Equal to x == y
!= Not equal x != y
> Greater than x>y
< Less than x<y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y
Logical Operators
✔ Logical operators are used to determine the logic between variables or values:

Operator Name Description Example


&& Logical and Returns true if both statements are true x < 5 && x < 10
|| Logical or Returns true if one of the statements is x < 5 || x < 4
true
! Logical not Reverse the result, returns false if the !(x < 5 && x < 10)
result is true
C++ Conditions and If Statements
✔ C++ has the following conditional statements:
✔ Use if to specify a block of code to be executed, if a specified condition is true
✔ Use else to specify a block of code to be executed, if the same condition is false
✔ Use else if to specify a new condition to test, if the first condition is false
✔ Use switch to specify many alternative blocks of code to be executed
The If Statements
✔ Use the if statement to specify a block of C++ code to be executed if a condition is true.

Syntax:

if (condition) {
// block of code to be executed if the condition is true
}

Example:

if (20 > 18) {


cout << "20 is greater than 18";
}
The If….else Statements
✔ Use the else statement to specify a block of code to be executed if the condition is false.

Syntax:
if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}

Example:

int time = 20;


if (time < 18) {
cout << "Good day.";
} else {
cout << "Good evening.";
}
// Outputs "Good evening."
The Switch Statements
✔ This is how it works:
✔ The switch expression is evaluated once
✔ The value of the expression is compared with the values of each case
✔ If there is a match, the associated block of code is executed
✔ The break and default keywords are optional, and will be described later in this chapter

Syntax:

switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
The Switch Example
Program: int day = 4;
switch (day) {
case 1:cout << "Monday";
break;
case 2:cout << "Tuesday";
break;
case 3:cout << "Wednesday";
break;
case 4:cout << "Thursday";
break;
case 5:cout << "Friday";
break;
case 6:cout << "Saturday";
break;
case 7:cout << "Sunday";
break;
}
Loops in C++

✔ 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
}

Example:

int i = 0;
while (i < 5) {
cout << i << "\n";
i++;
}
C++ 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);

Example:
int i = 0;
do {
cout << i << "\n";
i++;
}
while (i < 5);
C++ For loop
✔ When you know exactly how many times you want to loop through a block of code, use
the for loop instead of a while loop:
Syntax:
for (statement 1; statement 2; statement 3) {
// code block to be executed
}

✔ Statement 1: is executed (one time) before the execution of the code block.
✔ Statement 2: defines the condition for executing the code block.
✔ Statement 3: is executed (every time) after the code block has been executed.

Example:

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


cout << i << "\n";
}
C++ Break Statement
✔ The break statement can be used to jump out of a loop.

Example:
for (int i = 0; i < 10; i++) {
if (i == 4) {
break;
}
cout << i << "\n";
}
C++ Continue Statement
✔ The continue statement breaks one iteration (in the loop), if a specified condition occurs, and
continues with the next iteration in the loop.
Example:
for (int i = 0; i < 10; i++) {
if (i == 4) {
continue;
}
cout << i << "\n";
}
Practice Program

1) WAP in C++ to check entered number by user is EVEN number or ODD number.

OUTPUT:

Enter any number : 43


Entered number is : ODD

Enter any number : 64


Entered number is : EVEN
Practice Program

2) WAP in C++ to print all EVEN number on screen from 1 to 100.

OUTPUT:
2
4
6
.
.
.
94
96
98
100
Practice Program

3) WAP in C++ to print all number on screen which are divisible by 3, 5 & 7 from 1 to 500.

OUTPUT:
105
210
315
420
Practice Program

4) WAP in C++ to check entered number is prime number on not .

OUTPUT:

Enter any number: 17


Entered number is : Prime
Practice Program

5) WAP in C++ to check whether a character is vowel or Consonant.

OUTPUT:

Enter any character: e


Entered character is : vowel
Practice Program

6) WAP in C++ to generate the multiplication table of entered number upto 10.

OUTPUT:

Enter an integer: 5
5*1=5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
Practice Program

7) WAP in C++ to check entered year is leap year or not.

OUTPUT:

Enter a year : 2014


2014 is not a leap year.
Practice Program

8) WAP in C++ to print following pattern on the screen.

OUTPUT:

*
**
***
****
*****
Practice Program

8) WAP in C++ to print following pattern on the screen.

OUTPUT:

0
12
345
6789

You might also like