0% found this document useful (0 votes)
30 views15 pages

Conditional Statements 1

The document discusses different conditional statements in C++ including if, else, and else if. It explains that if is used to specify code to execute if a condition is true, else is used to specify code to execute if the condition is false, and else if can be used to check another condition if the first is false. Examples are provided to demonstrate the usage of if and else statements.

Uploaded by

Aleeza gondal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views15 pages

Conditional Statements 1

The document discusses different conditional statements in C++ including if, else, and else if. It explains that if is used to specify code to execute if a condition is true, else is used to specify code to execute if the condition is false, and else if can be used to check another condition if the first is false. Examples are provided to demonstrate the usage of if and else statements.

Uploaded by

Aleeza gondal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Conditional statements

• You can use these conditions to perform different actions for different
decisions.

• 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 Statement

• 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
•}
• Note that if is in lowercase letters. Uppercase letters (If or IF) will
generate an error.
In the example below, we test two values to find out if 20
is greater than 18. If the condition is true, print some text:
• #include <iostream>
• using namespace std;

• int main() {
• if (20 > 18) {
• cout << "20 is greater than 18";
• }
• return 0;
• }
• Output 20 is greater than 18
• #include <iostream>
• using namespace std;

• int main() {
• int x = 20;
• int y = 18;
• if (x > y) {
• cout << "x is greater than y";
• }
• return 0;
• }
• x is greater than y
• / Program to print positive number entered by the user
• // If the user enters a negative number, it is skipped
• #include <iostream>
• using namespace std;
• int main() {
• int number;
• cout << "Enter an integer: ";
• cin >> number;
• // checks if the number is positive
• if (number > 0) {
• cout << "You entered a positive integer: " << number << endl;
• }
• cout << "This statement is always executed.";
• return 0;}
• output
• Enter an integer: 5
• You entered a positive number: 5
• This statement is always executed.
The else Statement
• 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
• #include <iostream>
• using namespace std;
• int main() {
• int time = 20;
• if (time < 18) {
• cout << "Good day.";
• } else {
• cout << "Good evening.";
• }
• return 0;
• }
• output:
• Good evening.
• If the condition evaluates true,

• the code inside the body of if is executed


• the code inside the body of else is skipped from execution
• If the condition evaluates false,

• the code inside the body of else is executed


• the code inside the body of if is skipped from execution
/ Program to check whether an integer is positive or negative
// This program considers 0 as a positive number

#include <iostream>
using namespace std;
int main() {
int number;
cout << "Enter an integer: ";
cin >> number;
if (number >= 0) {
cout << "You entered a positive integer: " << number << endl;
}
else {
cout << "You entered a negative integer: " << number << endl;
}

cout << "This line is always printed.";


return 0;
}
output
• Enter an integer: 4
• You entered a positive integer: 4.
• This line is always printed.
• Explanation
• In the above program, we have the condition number >= 0. If we enter
the number greater or equal to 0, then the condition evaluates true.

• Here, we enter 4. So, the condition is true. Hence, the statement inside
the body of if is executed.
• Output 2

• Enter an integer: -4
• You entered a negative integer: -4.
• This line is always printed.
• #include <iostream>
• using namespace std;
• int main() {
• int a=10;
• int b= 20;
• int sum=a+b;
• cout<<sum<<endl;
• if (sum < 18) {
• cout << "sum is smaller.";
• } else {
• cout << "sum is greater.";
• }
• return 0;
• }
output
• 30
• sum is greater.

You might also like