Breaking

C++ if-else statements with example-programmingworld




                         C++ if else:-

In C++ if else, if statement tests a particular condition where the various type of statement in C++.
    ·       If statement
    ·      If else statement  
    ·       Nested ifs statement
    ·       If else if ladder


If statement if statement tests particular condition.

C++ if statement syntax:-

If (condition)
{
Statement;
}
                                                    









C++ if Example:-

#include<iostream.h>
Int main()
{
int x,y,z,max;
cout<<”Enter three numbers=”;
cin>>x>>y>>z;
max=x;
If(y>max)
{
max=y;
}
If(z>max)
{
max=z;
}
cout<<”\n”<”The largest of”<<x<<”,”<<y<<”and”<<z<<”is”<<max;
return 0;
}
Output:-

Enter three number= 3 5 9
The largest  of 3,5 and 9 is 9

C++ If-else statement:-

There is another form of if that allows for this kind of either – or condition by providing an else clause.

C++ if-else syntax:-

If (expression)
{
Statement 1;
}
else
{
Statement 2;
}


C++ If- else Example:-


#include<iostream.h>
int main()
{
int num=2016;
If(num%4==0)
{
cout<<”this is a leap year”;
}
else
{
cout<<”this is not a leap year”;
}
}
Output:-

This is a leap year



C++ Nested ifs statement:-

A nested if is an if that has another if in its if’s body or in its else’s body.

C++ Nested ifs Syntax :-


if (expression 1)
{
If (expression 2)
{
Statement 1;
}
Else
{
Statement 2;
}
}


C++ Nested ifs example:-

#include<iostream.h>
Int main()
{
if (grade >= 65)
{
    print("Pass");
}

    if (grade >= 90)
{
        print("A grade");
}
    if (grade >=80)
{
        print("B grade");
}
    if (grade >=70)
    {
    print("C grade");
}
    if (grade >= 65)
{
        print("D grade");
}
else
{
    print("Fail");
}
}

C++ If -else-if ladder:-

The if-else-if ladder statement executes one condition from multiple statements.

If(expression1)
{
Statement 1;
}
else if(expression 2)
{
Statement 2;
}
else if (expression 3)
{
Statement 3;
}
else
{
Statement 4;
}

  C++ If -else-if ladder Example:-


#include<iostream.h>
int main()
{
char ch;
cout<<”\n enter a character”;
cin>>ch;
If(ch>==48&&ch<=57)
{
Cout<<”\n”<<”you entered a digit”;
}
elseif(ch>=65&&ch<=90)
{
cout<<”\n”<<”you entered an uppercase character”;
}
elseif(ch>=97&&ch<=122)
{
cout<<”\n”<<”you entered a lowercase character”;
}
Else
{
cout<<”\n”<<”you entered a special character”;
}
return 0;
}

switch statement:-

The switch statement executes one statement from multiple conditions. It is like if-else-if ladder statement in C++.

     switch(expression)
    {      
      case value1:      
     //code to be executed;      
      break;    
     case value2:      
     //code to be executed;      
     break;    
  ......      
                    
    1      default:                     //code to be executed if all cases fail.     
               break;    
             


Switch case Example:-

#include <iostream>

int main ()
{
   // local variable declaration:
   char grade = 'C';

   switch(grade)
   {
   case 'A' :
      cout << "A grade" << endl;
      break;
   case 'B' :
      cout << "B grade" << endl;
      break;
   case 'C' :
      cout << "C grade" << endl;
      break;
   case 'D' :
      cout << "D grade" << endl;
      break;
   default :
      cout << "Invalid grade" << endl;
   }
   cout << "Your grade is " << grade << endl;

   return 0;
}
Output :
D grade
Your grade is D

C++ for Loop:-

The C++ for loop is the easiest to understand the c++. The C++ for loop is used to iterate a part of the program several times.

Syntex:-

for(Initial Statement; Expression; Update statement)
{

// Statements
}



for Loop Example:-

#include <iostream>
int main()
{
int j;
for(j=1; j<=50; j++)
{
if(j%2==0)
{
cout<<j<<"even "; 
}
else
{
cout<<"odd";
}
 }
}

C++ While Loop:-

The while loop is an entry-controlled loop. If the number of iteration is not fixed, it is recommended to use while loop than for loop.

Syntax:-

 While(expression)
{
  // body of loop
}

C++ While loop Example:-


     #include <iostream>

       int main()

      {

       int k=1;

      while(k<=100)

    {

        if(k%3==0 || k%5==0 || k%7==0)

        {

            continue;

        }

        cout<<k<<" ";

        k++;

    }

    return 0;

}

C++ do while loop:-

In the do while loop, firstly execute the while section after completing this part do section is executed.                                                       

The syntax of do while loop :

 do

{

  //statements

}

while( condition );


Example of do while loop :

    #include <iostream>

         int main ()
{

   int i = 0;

  // do loop

   do

   {
       cout << "value of i :=" << i << endl;
       i++;
   }

while( i < 10 );

    return 0;

}


goto statement:-

A goto statement can transfer the program control anywhere in the program. The target destination of a goto statement is marked by a label. The target label and goto must appear in the same function.

Example of goto statement:-

   #include <iostream>

  using namespace std;

int main ()
{
    int i=0;

loop:
    cout << i << endl;
    i++;

    if (i<10)
        goto loop;

    return 0;
}

The break Statement:-

The break statement enables a program to skip over part of the code. A break statement terminates the smallest enclosing while, do-while, for or switch statement.

Example:-


        #include <iostream>
    
        int main()
 {
        int i=0
        while (true)
{
         cout<<"x=";
           cin>> x;
       cout<<"Value of x="<<x<<endl;
    
   if ((x % 2 == 0)
      break;
   }
     cout<<"Bye"<<endl;
}

C++ continue statement:-

The continue statements unconditionally transfer program control within function C++ has four statements that perform an unconditional branch: return, goto, break and continue.

     #include<iostream.h>
       #include<conio.h>
     main()
{
     for(int z=0; z<6; z++)
{
      cout<<" The User is a very good programmer :".       <<endl;
     continue;
      cout<<" The User is a very bad programmer :".         <<endl;
}
   getch();
}