Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c

if condition programs in C++ language

Posted on July 17, 2016April 2, 2020

Table of Contents

  • if condition programs in C++ language
    • If statements examples in C++ language
      • if-else statements
        • Program for Checking Voting eligibility Program 1 #include <iostream>     // io stream header file
        • Program for Checking positive or negative
      • if elseif else  statements in C++ language
        • Program for check young or elders
        • Program for Checking positive or negative or zero

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

if condition programs in C++ language
Syntax

Syntax of if-else statement


Syntax of if-else if-else statement

 

if condition programs in C++ language
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:

 

 

While Loop in Java             while Loop in C++         While Loop In C

Do-while Loop in Java         Do-while Loop C++           Do-while loop in C

For Loop in C++               For Loop in C              For loop in java

If condition in Java          If condition in C

 

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes