Topic :If & Else statement: If Statement | If & Else: | Nested If & Else:

Decision Making:

In every day life, we are often making decision. We perform different tasks while taking decisions. For example, we say: "if You are interested in Programming, then Join infobrother", in this statement we make a decision.

on the other side our parents say: "if the milk shop is open, bring one liter of milk" then we will bring one liter of milk if the shop is open. and if the shop is closed, we will come back to home without milk. thus we making a decision on the condition that the shop is open. Computer Programs often seem smart because of their ability to use selections or make decision. in this lecture we will use if and else statement to make decision:



If statement:

If statements consists of a Boolean expression followed by one or more statements. If statement allows us to control whether a program enters a section of code or not based on whether a given condition is true or false. by using if statement we can check whether a user entered a correct password, our program can decide whether a user is allowed access to the computer or not.



Basic Syntax for if Statement:


/*The operator like logical, Relational or any other operator
can be used in the brackets:*/
if (condition) { // body begin: statement... statement... statement... } // body close:


we can use several statement to executed if the condition is true.



if the statement is single then there is no need of curly braces. but if there are more statement then single, blocks is important. (the statement between two curly braces { } is know has block).



if statement

Let's have an example to understand the concept of IF statement: let's suppose we Need to create an Program which can tell us whether the student is pass or fail on the condition that if student percentage is greater than or equal to 60 then He/she will pass otherwise fail.

this Program will ask user to enter the percentage. user will enter their percentage. after press enter this Program will Make decision whether the user is pass or fail. if pass then code inside the block and outside the block will execute. otherwise only the code outside of block will be execute:



#include <iostream>
using namespace std;
main()
{
  int percentage; //variable to store student percentage:
  cout<<" Enter the percentage of Student: "<<endl;
  cin>>percentage; //i will enter 60:
  if(percentage >= 60)
  {
     cout<<" Congratulation You are pass: "<<endl; //if pass then show this:
  }
  return 0;
}


If and else statement:

We have seen that the if structure executes its block of statement only when the condition is true, otherwise the statement are skipped. The if and else structure allows the Programmer to specify that a different block of statements is to be executed when the condition is false.



Basic Syntax for if and Else:

if (condition ) // any operator we can use to check condition:
{ 
  //if the condition is true then this block will be executed:
} 

else 
{ 
  //if the condition is false then this block will be executed.
}



in If and Else, only one block of statements will execute. if condition is true the if-block will be execte, if condition is false then else-block will be execute:



if statement

let's have an example of same Program that we discuss in if statement: here we will add "else" too in if statement: in this cause if percentage is greater then or equal to 60 then only the statement within the if block will execute. if percentage is less then 60. then only the statement within the else block will execute.



using namespace std;
main()
{
  int percentage; //variable to store student percentage:
  cout<<" Enter the percentage of Student: "<<endl;
  cin>>percentage; //i will enter 60.

  if(percentage >= 60)
  {
     //if condition is true then show this:
     cout<<" Congratulation You are pass: "<<endl; 
  }

  else
  {
      //if condition is false then show this:
     cout<<" you are fail: "<<endl;
  }

  return 0;
}


Nested If-else statement:

in if-else statement, we can check whether the condition is true or false. if the condition is true, we can display a message, if the condition is false, then we can display another message. but what's about, if we Need to test Multiple conditions. well, let's have an example to clear this Nested if-else statement:

Let's suppose we have a Program, which will allow User to enter their Gander. If the user enters "M" then it will display a Message, if User Enter "F" then will display another Message. But if a user enters another Character then M and F, then how our Program can Handle this. Here are the Uses of a Nested if-else statement. C++ allows us to check the Multiple conditions at the same times using Nested if-else Statement:



Basic Syntax for Nested if-else.



if (1st condition ) 
{ 
    /* if the 1st condition is true, then this statement will be executed */
}

else if (2nd condition) 
{ 
   /*if 2nd condition is true and 1st is false. then this statement will execute. */
} 

else 
{ 
   /* if both condition will false then this statement will be execute: */
}
 



In Nested if-else Statement, only one statement can be executed. the Program will test, if the first condition is true, then it will show the statement of first block. if the first is not true, then the program will try next condition and so on.



Nested if-else statement

Let's have an Example here. in this example. Program will ask the user to enter their Gender. if user enter "F" then it will display an statement. if user enter "M" another statement will be executed. if user enter another character then it will display an error message. "Make sure you enters capital letter."



#include <iostream>
using namespace std;
main()
{
  char gender;
  cout<<" Enter your Gender: Enter capital 'M' for Male, capital 'F' for Female" <<endl;
  cin>>gender; //i'll enter M, because am Male.

  if(gender == 'M') 
  {
     cout<<" You are Male: "<<endl;
  }

  else if(gender =='F')
  {
     cout<<" You are Female: "<<endl;
  }

  else
  {
     cout<<" You Enter Wrong Answer: "<<endl;
  }

  return 0;
}

Let's have another Example. in this example, the program will ask User to enter any character. the user will enter the character and the program will check whether a given character is a Uppercase or Lowercase alphabet, a digit or a special character. in this example, we will use ASCII Values to check the condition. the following tables show the range of ASCII values for various characters.


CharactersASCII Values
Uppercase Alphabet: 65 - 90
Lowercase Alphabet: 97 - 122
Digit: 48 - 57

If ASCII value of character is other than the values mentioned above, then it is a special character.



#include<iostream> 
using namespace std;
 
int main()
{  
    char ch;
    cout<<"Enter any character: ";
    cin>>ch;
    
    //Uppercase Alphabet: (65-90)
    if(ch>=65 && ch<=90)
        cout<<endl<<"You Have entered an uppercase character";
        
    //Lowercase Alphabet: (97-122)    
    else if(ch>=97 && ch<=122)
            cout<<endl<<"You Haveentered a lowercase character:";
            
    //Digit: (48-57)       
    else if(ch>=48 && ch<=57)
           cout<<endl<<"You entered a entered a digit";
           
    //Other character:       
        else
            cout<<endl<<"You entered a special character";
    return 0;
}









I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement