0% found this document useful (0 votes)
16 views5 pages

C++ Conditions

The document explains the use of if, if...else, and nested if...else statements in C++ programming for conditional execution of code blocks. It provides syntax and examples for each statement type, including checking for positive/negative integers and finding the greatest of three numbers. Additionally, it covers the use of nested if statements and includes examples for a simple calculator.

Uploaded by

xubairch596
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)
16 views5 pages

C++ Conditions

The document explains the use of if, if...else, and nested if...else statements in C++ programming for conditional execution of code blocks. It provides syntax and examples for each statement type, including checking for positive/negative integers and finding the greatest of three numbers. Additionally, it covers the use of nested if statements and includes examples for a simple calculator.

Uploaded by

xubairch596
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/ 5

C++ if, if...else and Nested if...

else
In computer programming, we use the if...else statement to run one block of
code under certain conditions and another block of code under different
conditions.

C++ if Statement
Syntax

if (condition) {

// body of if statement

The if statement evaluates the condition inside the parentheses ( ).

If the condition evaluates to true, the code inside the body of if is executed.

If the condition evaluates to false, the code inside the body of if is skipped.

Example 1: C++ if Statement

// 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 <<”\n”;
}

cout << "This statement is always executed.";

return 0;
}
C++ if...else
The if statement can have an optional else clause.

Syntax:

if (condition) {
// block of code if condition is true
}
else {
// block of code if condition is false
}
Example: C++ if...else Statement
// 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 << “\n”;
}
else {
cout << "You entered a negative integer: " << number <<”\n”;
}

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

return 0;
}

Example2: check whether a number is even or odd.

C++ if...else...else if statement


The if...else statement is used to execute a block of code among two
alternatives. However, if we need to make a choice between more than two
alternatives, we use the if...else if...else statement.

Syntax:
if (condition1) {
// code block 1
}
else if (condition2){
// code block 2
}
else {
// code block 3
}
Example 1: Program to check whether an integer is positive, negative or zero.
Example 2: Program to find greatest of three numbers.
Example 3: Assign grades on the basis of percentage.

C++ Nested if...else


Sometimes, we need to use an if statement inside another if statement. This is
known as nested if statement.

Think of it as multiple layers of if statements. There is a first, outer if


statement, and inside it is another, inner if statement .

Syntax:

// outer if statement
if (condition1) {

// statements

// inner if statement
if (condition2) {
// statements
}
}
Example 1: check greatest of three numbers using nested if..else
#include<iostream>
using namespace std;
int main(){
int n1,n2,n3;
cout<<"enter three number";
cin>>n1>>n2>>n3;
if(n1==n2 && n2==n3){
cout<<"numbers are equal";
}
else{
if(n1>n2){
if(n1>n3){
cout<<n1<<" is greater";
}
else{
cout<<n3<<" is greater";
}
}
else{
if(n2>n3){
cout<<n2<<" is greater";
}
else{
cout<<n3<<" is greater";
}
}
}

return 0;
}

Example 2: Make a simple calculator performing addition, subtraction,


multiplication and division (also check that division by zero is not possible).

#include<iostream>
using namespace std;
int main(){
int n1,n2;
char op;
cout<<"enter the numbers";
cin>>n1>>n2;
cout<<"enter the operator + - * /";
cin>>op;
if(op=='+'){
cout<<n1+n2;
}
else if(op=='-'){
cout<<n1-n2;
}
else if(op=='*'){
cout<<n1*n2;
}
else if(op=='/'){
if(n2!=0){
cout<<n1/n2;
}
else{
cout<<"division by 0 is not possible";
}
}
else{
cout<<"invalid operator";
}
return 0;
}

You might also like