Skip to main content

6. Conditional Statements

●  Conditional Statements:-

                The if, if...else and nested if...else statement are used to make one-time decisions in C++ Programming, that is, to execute some code/s and ignore some code/s depending upon the test condition. Without decision making, the program runs in similar way every time. Decision making is an important feature of every programming language using C++ programming. 

1) Simple if Statement:- 

               The if statement checks whether the test condition is true or not. If the test condition is true, it executes the code/s inside the body of if statement. But it the test condition is false, it skips the code/s inside the body of if statement. 

Structure of if Statement:- 

if (Boolean Expresson) 

/* if Expression is true */ 

Statements; 


● Flowchart of if :-



Example program:-

#include <iostream.h>
#include <conio.h>
Void main () 
{
       int a=10; 
       if (a>5) 
      { 
             cout<<"if Condition is Satisfied\n";  
             cout<<"a is greater than 5\n";
      }
      cout<<"Value of a is: "<<a<<endl;

      getch();
}


Output:-

if Condition is Satisfied
a is greater than 5
Value of a is:10


2) if...else Statement:-

           The if...else executes body of if when the test expression is true and executes the body of else if 
test expression is false.

Structure of if… else Statement:-

if(Test expression)
{
true block Statements…
}
else
{
false block Statements….
}

         The if statement checks whether the test expression or condition is true or not. If the test 
condition is true, it executes the code/s inside the body of if statement. But it the test condition 
is false, it executes the code/s inside the body of else.


Flowchart of if...else:-



Example Program:- 

#include <iostream.h>
#include <conio.h> 
void main() 
         int n; 
         clrscr(); 
         cout<<("Enter The Number \n"); 
         cin>>n; 
         if(n%2==0) 
                 cout<<("Number is Even "); 
         else 
                 cout<<("Number is Odd"); 
         getch(); 

Output:- 

Enter the Number 
Number is Odd


3) Nested if...else Statement:-
             Nested if...else are used if there are more than one test expression. 

Structure of Nested if….else:- 

if(Test expression) 
        Statement… 
else if(Test expression) 
        Statements…. 
else 
        Statements…. 


           The nested if...else statement has more than one test expression. If the first test expression is true, it executes the code inside the braces{ } just below it. But if the first test expression is false, it checks the second test expression. If the second test expression is true, if executes the code inside the braces{ } just below it. This process continues. If all the test expression are false, code/s inside else is executed and the control of program jumps below the nested if...else 

Example Program:- 

#include <iostream.h> 
#include <conio.h>
int main() 
           int n;
           cout<< "Enter an integer: ";
           cin>> n;
           if ( n>0)
          {
                    cout << "You entered a positive integer"<<endl;
          }
          else if(n<0)
         {
                   cout<<"You entered a negative integer"<<endl;
         }
         else
        {
                   cout<<"You Entered Zero";
        }
        getch();
}


Output:-

Enter an integer
5
You entered Positive integer.


Comments

Popular posts from this blog

C++ programming language

                c++ is a general programming language and is widely used nowadays for competitive programming. It has imperative, object-oriented and generic programming features.C++ runs on lots of platforms like Windows, Linux, Unix, Mac etc.   ❖❖  C++ with oops ❖❖

Pratical -206 paper solution -3

Full course of c++ programming language Click here > C++ programming language Q-1 [A] Write a c program to create structure of employee with members Empid, EmpName, Qualification and EmpSalary by taking input of 5 employees display the employee whose qualification is "MBA" and salary greater than 20000 . #include <stdio.h> #include <conio.h> #include <string.h> struct Employee {     int Empid, EmpSalary;     char EmpName[50];     char Qualification[50]; }; Void main() {     struct Employee emp[5];     int i;     for (i = 0; i < 5; i++)     {         printf("Enter employee ID: ");         scanf("%d", &emp[i].Empid);         printf("Enter employee name: ");         scanf("%s", emp[i].EmpName);         printf("Enter employee qualification: ");         scanf("%s", emp[i].Quali...

9. Jumps Statement in c++

➢ Jumps in Loops:- ➢ C++ break and continue Statement:-                     There are two statements (break; and continue ;) built in C++ programming to alter the normal flow of program.                     Loops are used to perform repetitive task until test expression is false but sometimes it is desirable to skip some statement/s inside loop or terminate the loop immediately with checking test condition. On these type of scenarios, continue; statement and break; statement is used respectively. The break; statement is also used to terminate switch statement. 1) break Statement :-                       The break; statement terminates the loop(for, while and do..while loop) and switch statement immediately when it appears . Syntax of break :- break;              ...