if and if else statement in C++ programming language
If and if-else statements in C++ programming language
In this blog post we will look at the basic working of if and if-else statement in C++ programming language. These if and if-else statements are also referred as conditinal statements in programming languages. But today we will study them under the context of C++ programming.
In C++ programming language we use if and if-else to run a specific peace of code under one condition and another peace of code for some different condition. We also refer them as decision statements.
Types of if statements in C++ programming language
If we talk about types of if statements there are mainly four types
1) if statement
2) if-else statement
3) if else-if ladder statement
4) nested if statements
But in this blog post we will cover the if and if-else statements.
Syntax of if statement and basic working of if statement
Syntax of if statement
if (condition) { // code to be executed here. }
#include<iostream> #include<conio.h> using namespace std; void main() { int Number; cout << "Enter Number = "; cin >> Number; if (Number % 2 == 0) { cout << Number << " is even number"; } _getch(); }
Let us examine the above coding example
1) In the above coding example we took a variable of type int and named it as Number.
int Number;2) We will enter some integer value in Number variable.
cin >> Number;3) After entering the value in Number variable we will use if statement and insert the condition like this,
if (Number % 2 == 0)if(Number % 2 == 0) this condition will check whether the entered number is even number or not based on the result of modulus operator. The result of the Number % 2 would be remainder after dividing Number by 2 i.e, 4 % 2 = 0 (assume value of Number = 4)
4) And if the value of Number variable is 5 i.e, 5%2 = 1 the remainder would be 0.
5) So in case of even numbers remainder would be 0 and in case of odd numbers remainder would be 1.
In the above code we only have if condition so it will print entered number is even. But if the condition is false then the piece of code will do nothing. And hence in this part the if-else statement will come to tackel this scenario when the if condition is false.
Syntax of if-else statement and basic working of if-else statement
Syntax of if-else statement
if (condition){ // code to be executed here. } else { // code to be executed here. }
Working of if-else statement
In the above coding syntax of if-else statement, it again starts with the if condition and the if condition works in exactly the same way as discussed earlier. But now suppose if the conditional part in the if condition is false then what will happen. So this is the point where the if-else statement rise and shine. When the if condition is false control of our C++ program moves from if condition to the else part and the code written inside the opening and closing curly brackets will be executed.
Simple if-else statement coding example in C++ coding example
#include<iostream> #include<conio.h> using namespace std; void main() { int Number; cout << "Enter Number = "; cin >> Number; if (Number % 2 == 0) { cout << Number << " is even number"; } else { cout << Number << " is odd number"; } _getch(); }
In the above coding example we used if-else statement with the same if condition example, but in this case we have used the else part along with if part.
if (Number % 2 == 0) { cout << Number << " is even number"; } else { cout << Number << " is odd number"; }
So if the entered number is even number then the if condition will print the entered number is even number.
And if the entered number is odd then the if condition would become false and in that case the else part will run which will show us the message that the entered number is odd number which is coded in the else part with in the opening and closing curly brackets of else part.
So in this way the if-else condition will run and our program would be implemented successfully.
Congratulations! like always we have successfully implemented if and if-else statement in C++ programming language.
Comments
Post a Comment