if condition programs in C++ language

if condition programs in C++ language

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

Syntax of if-else statement


Syntax of if-else if-else statement

 

Syntax


If … else statement used to check the various condition to decision making at C++ programming language.

 

If statements examples in C++ language

if-else statements

Program for Checking Voting eligibility

Program 1

#include <iostream>   // io stream header file

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 for Checking positive or negative

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;

}

When the above code is compiled and executed, it produces the following result

if elseif else  statements in C++ language

Program for check young or elders

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 for Checking positive or negative or zero

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:

 

 

Karmehavannan

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.