Conditional Statements:

Conditional Statements checks the condition or compares two variables using comparison operators and performs action.
We use if statement, if-else statement, else-if statement as conditional statements.


If statement:

If statement checks the condition and execute the specific code.
if(condition){...}

If-else:

If-else is used as conditional statement. In if else statements, if one condition is true, it will be executed and all others will be ignored.
if ( condition ){
    //code
}
else{
    //code
}

Note:

  • If requires condition.
  • Else does not require condition.


if-else.cpp
#include<iostream>
using namespace std;
int main(){
    int a=10;
    int b=20;
    if(a>b){
        cout<<"a is greater than b.\n";
    }
    else{
        cout<<"b is greater than a.\n";
    }
    return 0;
}

Else if:

If you want to check conditions more than one time then use else if.

if-else.cpp
#include<iostream>
using namespace std;
int main(){
    int a=10;
    int b=20;
    if(a>b){
        a++;
        cout<<a<<endl;
    }
    else if(b>a){
        b++;
        cout<<b<<endl;
    }
    else{
        //in this condition a will be equal to b
        cout<<"a is equal to b";
    }
    return 0;
}

If else with logical operators:

If you want to check two condition in single if statement, you will use logical operators.

if-else.cpp
#include<iostream>
using namespace std;
int main(){
    int a=10, b=20, c=30;
    if( a<b && a<c ){
        cout<<"a is less than b and c";
    }
    else{
        cout<<"a is not the smallest";
    }
    if( a==10 || b==10 ){
        cout<<"a = 10 or b =  10";
    }
    return 0;
}

Nested if else:

In nested if else, we use if statement within if statement.

if-else.cpp
#include<iostream>
using namespace std;
int main(){
    int a = 100, b = 20, c = 30;
    if( a>b ){
	    if(a>c ){
		    cout<<"a is the largest";
	    }
    }
    else{
        cout<<"a is not the largest";
    }
    return 0;
}

Dangling else Grammar:

If you are using multiple if statements and single else statement, then there is dangling else problem.

if(condition){...}
if(condition){...}
Else{...}
Compiler does not know that with which if, else will go.

Ternary Operator:

It is also used to check condition like if else.
type var_name = Condition ? if true : if false

if-else.cpp
#include<iostream>
using namespace std;
int main(){
    string res = a > b ? "a > b" : "a < b";
    cout<<res<<endl;
    return 0;
}

Note : You can use ternary operator when logic is simple. It makes your code more compact and easy to use. We can also use nested ternary operator. Ternary operator is also called conditional operator.


Goto Statement:

As name indicates goto statemet is used to go to a particular line of code. We labelled a line of code and at some point, we write "goto label" and our control jumps to that label.

int main() {
  label:
    //code
  goto label;
  //code
}

goto.cpp
#include <iostream>
using namespace std;


int main() {
    int age;
    age:
    cout << "Enter your age";
    cin >> age;
    if(age >= 18){
    	cout<<"You are an adult\n";
	}
	else{
		cout<<"You are not an adult\n";
		goto age;
	}
}