Table of Contents
In this tutorial, we will discuss some “if condition programs” with its solution
The if statement is used to evaluate the test expression inside the parenthesis in C++ language.
When the test expression is evaluated to true, the Body of “if statement” will be executed.
When the test expression is evaluated to false, the control of the loop exits from the body of if and executed its else part.
Here we let’s learn an example of some programs for “if statement”
Syntax of if statement
Syntax of if-else if-else statement
If … else statement used to check the various condition to decision making at C++ programming language.
using namespace std;
int main() // start this program this main method – starting point
{
int age=16; // variable assignment
if(age<=17) // if condition check statement
{
cout << “you can’t vote to te election please go home !” << endl; // if condition is true display this statement – true part
}else
{
cout<<“ya you can vote please enter the booth !”<<endl; // if condition is false display this statement -else part
}
return 0; // end the program
}
When the above code is compiled and executed, it produces the following result:
Above program is tested age for voting to election.
If age is above 18, he/she eligible for voting, if not he/she is not eligible for voting.
Program 2
#include <iostream>
using namespace std;
int main() {
int number;
cout<< “Enter an integer: “;
cin>> number;
if ( number > 0) { // Checking whether an integer is positive or not.
cout << “You entered a positive integer: “<<number<<endl;
}
else{
cout<<“this is negative integer”<<endl;
}
cout<<“This statement is always executed because it’s outside if statement.”;
return 0;
}
Program 3
#include <iostream>
using namespace std;
int main()
{
int age=25;
if(age<=20)
{
cout << “you are young” << endl;
}else if(age<=40)
{
cout<<“you are in middle age man !”<<endl;
}else
{
“you are a old guy”;
}
return 0;
}
When the above code is compiled and executed, it produces the following result:
Program 4
#include <iostream> #include <conio.h> using namespace std; int main() { int number; cout << "Enter a integer as you wish" << endl; cin>>number; if(number>0){ cout << "You entered "<<number<< endl; cout << number<<" is a positive integer"<<endl; } else if(number<0){ cout << "You entered "<<number<< endl; cout << number<<" is a negative integer"<<endl; } else if(number==0){ cout << "You entered "<<number<< endl; } getch(); return 0; }
When the above code is compiled and executed, it produces the following result
Case 1
Enter a integer as you wish 15 you entered 15 15 is a positive integer
Case 2
Enter a integer as you wish -34 you entered -34 -34 is a negative integer
Case 3
Enter a integer as you wish 0 you entered 0
Nested if C++ language
Program 5
#include <iostream>
using namespace std;
int main()
{
int a=100;
int b=200; // local veriable diclaration
if(a==100) //check boolean condition
{
if(b==200) //Outer if is true then check inner if condition
{
cout << “Value of a is 100 and b is 200” << endl; //inner if condition is true then print this statement
}
}
cout << “Exact value of a is:”<< a << endl;
cout << “Exact value of b is:”<< b << endl;
return 0;
}
When the above code is compiled and executed, it produces the following result:
Subtract two numbers using method overriding Program 1
PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…
Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…
Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…
Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…
How to write a program to convert Fahrenheit into Celsius In this article, we will…
This website uses cookies.