C++ if ( && , || ) else
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhsgQvFRqAEwMm-b1Y9HmSyAmqip_kCCmHIjvh1xlo_8ajwCZ1_cDQdwFaaSdT4ng2hfQG22tN9LKDuclGwjTmRC-lpCe3oaXtgHWjyEMYkndChDnpML971VoB1Ka1ihIT9w7MGDptFYWqx/s320/C%252B%252B-if-else-beginner-urdu-hindi-.png)
if else know as Decision making statement because it's depend on given condition.
As we discussed earlier about variables and different data types, there is slight difference of comparing int values and string values.
Every condition depends on values of variables and we use if statement to compare those values to execute the specific part of code. we use nested if else to add more check points in our program.
Every condition return only two states 'true' and 'false' it's bool data type in c++. if condition depends on these conditions
if
condition is true then it'll execute if condition body code.
if
condition is false then it'll skip if body code.
else
execute only when if condition is false.
Syntax of if else statement is very simple
if( condition ){
Every condition return only two states 'true' and 'false' it's bool data type in c++. if condition depends on these conditions
if
condition is true then it'll execute if condition body code.
if
condition is false then it'll skip if body code.
else
execute only when if condition is false.
Syntax of if else statement is very simple
if( condition ){
//body of if
}else{
}
}else{
}
it's one line if else syntax.
int y = 10 ;
string var = ( y <= 10 ) ? "This is if part" : "This is else part";
string var = ( y <= 10 ) ? "This is if part" : "This is else part";
cout << var ;
Output :
This is if part
we use && and || Operator which behaves like Boolean AND OR
&&
execute if body code when both condition are true
||
execute if body code when one condition is true
we use && and || Operator which behaves like Boolean AND OR
&&
execute if body code when both condition are true
||
execute if body code when one condition is true
Example 1
If
else
- #include <iostream>
- using namespace std;
- int main( ){
- int x = 5 , y = 10 , z = 15 ;
- if ( x > y ){
- cout << " X > Y " << x << " > " << y << endl ;
- }
- if ( x == y ){
- cout << " X = Y " << x << " = " << y << endl ;
- }
- if ( x < y ){ //execute because x is less than y
- cout << " X < Y " << x << " < " << y << endl ;
- }
- if ( x > y && x > z ) {
- cout << " X is greater than Y and Z " << endl ;
- }else if ( y > x && y > z ){
- cout << " Y is greater than X and Z "<< endl ;
- }else{ //execute because z is greater than x and y
- cout << " Z is greater than X and Y "<< endl ;
- }
- return 0;
- }
Console :
X < Y 5 < 10
Z is greater than X and Y
Example 2
If
else
- #include <iostream>
- using namespace std;
- int main( ){
- int x = 5 , y = 10 , z = 15 ;
- if ( x > y || x > z ){
- cout << " x is greater than y and z "<< endl ;
- }else if ( y > x || y > z ){ // execute
- cout << " y is greater than x and z "<< endl ;
- }else{
- cout << " z is greater than x and y "<< endl ;
- }
- return 0;
- }
Console :
Y is greater than X and Z
Watch Video
C++ if( && || ) else statement for Beginners Lecture in Urdu Hindi
Comments
Post a Comment