e6390af40fd246928c9f6b67562cf0fa37712fa8

8/23/2017

How To Use If Statement and nested if statement in C++ Programming Language

In this post we will discuss how to use if else statement in c++ programming language.The if and else are like a coin having two faces only.If  we toss a coin then there will be only two choices one is head and other is tail and we select the one head or a tail.If statement have also two choices only one is if and other is a else. that will be selected on your condition to which one is run if or else.

The other example of if else is like a street or a road having only two ways to move forward and we select the one that is true that depends on the condition we used.This is like as

In this  example we have a point where we are standing and want to move forward and we have only one choice to select a way for us.Examples of the if else statement are given bellow.

Syntax:
                  #include<iostream>
          using namespace std;   //header files
          void main() 
         {

            if(condition)
             {
              //body of if.
             }
            else
             {
              //body of else
             }

         }//body of main.

Example 1:
Write a program to check that the given number is Even number or Odd number.

Solution:
#include<iostream>
using namespace std;   //header files
void main() 
{
       int num;  //num stand for number
      
       cout<<"Enter a number :"<<endl;   //message
      
       cin>>num; //taking input from the user.

       if(num%2==0) //this is our condition to check the even number..because every even                          //number completely divisible by 2 and give remainder 0.                          
       {
               cout<<num<<" is Even Number"<<endl;
       }
       else
       {
              cout<<num<<" is Odd Number"<<endl;
       }
   
   cout<<"\n\n\n"<<endl;  //used for next line.
       system("pause");

}//body of main.


Example 2:
Write a program to check that the given number is a Leap Year or not.

Solution:
#include<iostream>
using namespace std;   //header files
void main() 
{
       int year; 
      
       cout<<"Enter a year :"<<endl;   //message to display
      
       cin>>year; //taking input from the user.

       if(year%4==0) //this is our condition to check the leap year...because every 4 yaer                         //  after  the month of febrary is 29 days.
       {
              cout<<year<<" IS A LEAP YEAR"<<endl;
       }
       else
       {
              cout<<year<<" IS NOT A LEAP YEAR"<<endl;
       }
   
   cout<<"\n\n\n"<<endl;  //used for next line.
       system("pause");


}//body of main.
















TAGS:

if, programming, else, structure, java, if else, tutorial, decision, conditional, elseif, then, excel vba tutorial, excel vba example, excel vba examples, code, vb, science, computer, c++, if-else, control flow, control, visual studio, visual basic, condition, control structure, c language, learn java, java tutorial, branching statements, decisions, javascript, syntax, conditions, php, if else statement, structures, how to program, control structures, c (programming language), statement, vba, c programming, beginners, python, language, or, basic, script, nested if, simple if, introduction, else if, 2010, control structures in c++, java if else, if statements, visual basic for applications, features, high-level, kanthety, saradhi, sundeep, java tutorials, flowchart, how to use for loop in java, how to use for loop in laravel, how to use for loop in android studio, how to use for loop in c++, how to use for loop in excel, how to use for loop in jquery, how to use for loop in php, if-else block, branching, matlab, if conditions, java branching, java programming, if block, c program, pgc lectures:, java example, mechanical engineering, microsoft excel (software), university, java course, how to, statements, learn to code, indentation, coding, scripting language, lectures, how to use for loop in javascript, how to use for loop in matlab, how to use for loop in c programming, how to use for loop in python, how to use for loop in vba, how to use for loop in c++ in hindi, 

No comments:

Post a Comment

Privacy Policy for Apps

Privacy Policy Muhammad Shahbaz built the Health Physio Association app as a Free app. This SERVICE is provided by Muhammad Shahbaz at no...