Conditional statement in c++ - Algomentor

Conditional statement in c++

conditional statement in c++

Hi in the section of data structure and algorithm made easy. we will learn about the Conditional statement in C++. Before learning conditional statement we must know what is a conditional statement what are benefits of the conditional statement and How to use conditional statements.

What are Conditional statements?

a condition statement is a type of programming statement which allow a program to decide on the basis of input, for example, you are writing a program of whether a number is even or odd so you use condition in the program like if a number is divisible by 2 then it's an even number otherwise it's an odd number. you, in that case, you decide then using conditional statements.

What are Benefits to using conditional statements?

to be honest, people take conditional very lightly and in reality, you don't even write something meaningful in a program without using condition because you see condition are everywhere we relate our program with real-life we are also bound by many conditions no on is 100% free from conditions same like our program is also bound by many conditions. This is like human logic we first think which way to go the same as the program chooses what to do.

How to use conditional statements?

for using conditional in the program you need to use if-else statements in some time you will see 3-4 programs on conditional statements like even or odd, you can vote or not and lastly max and min of two integers.

So let's begin with our First Program

// even or odd program in C++

#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter a number :";
cin>>n;
if(n%2==0)
{
cout<<"This is even number";
}
else
{
cout<<"This is odd number";
}
}

// Output of the code is


conditional statement in c++


In this program, we use % (modulo) operator to check if a number is divisible by 2 by using if-else we detect it's even or odd.


// you can vote or not

#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter your age:";
cin>>n;
if(n>=18)
{
cout<<"you can vote";
}
else
{
cout<<"You can't vote'";
}
}

// Output of the code is

conditional statement in c++

In this program, we take the output from user his/her age and if his/her age is greater then or equal to 18 then he/she is eligible for vote.

// program to say which is maximum and which is minimum

#include<iostream>
using namespace std;
int main()
{
int n1,n2;
cout<<"Enter two values ";
cin>>n1>>n2;
if(n1>n2)
{
cout<<n1<<" is maximum"<<endl;
}
else
{
cout<<n1<<" is minimum"<<endl;
}
if(n1<n2)
{
cout<<n2<<" is maximum"<<endl;
}
else
{
cout<<n2<<" is minimum"<<endl;
}
}

// hear is the output for the code

conditional statement in c++

In this program, we take two integers for input and apply condition on them of maximum and minimum

I hope you like our article on the conditional statement in C++ and now can read more article on our website regarding C++ and coding please do follow us on social media handle for more update

Thank you so much for reading this and please do share this article with your coder friend.

Hi if you want to write a post on our website then please check this article on How to do a guest post.

Post a Comment

0 Comments