Conditional Statement

 

Conditional Statement in C++




The statements of a computer program are executed one after the other in the order in which they are written. This is known as sequential execution of the program.

The order of execution of the statements in a program can be changed. This is done with the help of conditional statements. The conditional statements are used to execute a set of statements after testing a condition. The conditional statements are also called selection statements.


if Statement

The if statement is used to execute a set of statement after testing a condition.

The if statement evaluates a condition. If the given conditions is true, the statement following the "if statement" is executed. If the given condition is false, the statement following the "if statement" condition is ignored and the control transfers to the next statement.


Syntax:

The syntax of the "if statement" is:


if (condiiton)

{

Statement-1;

Statement-2;

Statement-3

Statement-m

}

Statement-n


In the above syntax, the set of statements (from statement-1 to statement-m) has been enclosed in curly braces. These are called compound statement.

If the condition given in the "if statement" is true, the compound statement given in the curly braces are executed. If the condition is false, the control shifts to the statement-n and the set of statement that follows the "if statement" is ignored.


Flowchart of if statement:

The flowchart for the "if statement" is shown below:





Example:

The following program executes a single statement if the given condition is true.

#include<iostream>

using namespace std;

int main()

{

int a,b;

a=70;

b=50;

if(a>b)

cout<<"Karachi"<<"\n";

cout<<"ok"<<"\n";

}



In the above program, the given conditions is true and the output of the program will be:
Karachi
ok

If the values of a is 20, then the given conditions becomes false and the statement cout<<"Karachi"<<"\n"; will be ignored. The control will shift to the next statement and only the statement cout<<"ok"<<"\n"; will be executed.



if-else Statement

This is another form of "if statement". It is used for making two way decision. In this statement, one condition and two blocks of statements are given. Either one of the two blocks of statements is executed after evaluating a condition.






The if-else statement tests the given relational condition. If the condition is true then the first block of statements is executed. If the condition is false, the first block of statement is ignored and the second block following the else is executed.


Syntax:

if (condition)

    Statement-1;

else

Statement-2;


Flowchart of if-else Statement:




Example:

Write a program to input two integers and then to find out whether these numbers are equal or not.


#include<iostream>

using namespace std;

int main()

{

int a,b;

cout<<"Enter first number :";

cin>>a;

cout<<"Enter second number :";

cin>>b;

if(a%2==b%2)

cout<<"Both numbers are equal";

else

cout<<"Numbers are different";

}



nested if Statement

When an if statement is used within another if statement, it is called the "nested if statement". The nested if statement is used for multi way decision making.




Syntax:


if (condition-1)

{
         if(conditon-2)

           {

              Statement-2;

             }

Statement-3;

}


Flowchart of nested if Statement:






Example:


Write a program to input three integer values from the keyboard. Find out the greatest number and print it on the screen.

#include<iostream>

using namespace std;

int main()

{
    int a,b,c;

cout<<"Enter first number: ";
cin>>a;

cout<<"Enter second number: ";
cin>>b;

cout<<"Enter third number: ";
cin>>c;

if(a>b)

   if(a>c)

cout<<"first number is greater.";

else 

cout<<"third number is greater.";

else

if(b>c)

cout<<"second number is greater.";

else

cout<<"third number is greater.";

}




Switch Statement

A switch statement enables the comparison of a variable's value to a list of possible values. Each value is referred to as a case, and each case checks the variable that is being turned on.


Syntax

The syntax for a switch statement in C++ is as follows:

switch(expression) {
   case constant-expression  :
      statement(s);
      break; //optional
   case constant-expression  :
      statement(s);
      break; //optional
  
   // you can have any number of case statements.
   default : //Optional
      statement(s);
}


A switch statement must adhere to the following guidelines:

A switch statement's expression must be of an integral or enumerated type, or it must be of a class type with one conversion function to an integral or enumerated type.

A switch may contain any number of case statements. The value to be compared to and a colon follow each case.

The constant-expression for a case must be either a constant or a literal, and it must have the same data type as the variable in the switch.

The statements after a case will run until a break statement is reached when the variable that is being switched on equals that case.

The switch statement ends when a break statement is encountered, and control moves to the line after the switch statement.

It's not necessary to have a break in every circumstance. In the absence of a break, the control will pass to consecutive cases until one is reached.

An optional default case that must come at the end of a switch statement is permitted. When none of the situations apply, a task can be completed using the default case. In the default instance, there is no break required.



Flowchart:



Example:


#include <iostream>
using namespace std;
 
int main () {
   // local variable declaration:
   char grade = 'D';

   switch(grade) {
      case 'A' :
         cout << "Excellent!" << endl; 
         break;
      case 'B' :
      case 'C' :
         cout << "Well done" << endl;
         break;
      case 'D' :
         cout << "You passed" << endl;
         break;
      case 'F' :
         cout << "Better try again" << endl;
         break;
      default :
         cout << "Invalid grade" << endl;
   }
   cout << "Your grade is " << grade << endl;
 
   return 0;
}



This would produce the following result:

You passed
Your grade is D


Break Statement


There are two uses for the break statement in C programming:

When a loop encounters a break statement, the loop is instantly broken, and programmed control moves on to the statement that follows the loop.

It can be applied to the switch statement to end a case (covered in the next chapter).

The break statement will halt the execution of the innermost loop if you are using nested loops and begin the execution of the line of code following the block.


Syntax

The syntax for a break statement in C is as follows:

break;


Flow Diagram:






Example

// program to print the value of i

#include <iostream>
using namespace std;

int main() {
    for (int i = 1; i <= 5; i++) {
        // break condition     
        if (i == 3) {
            break;
        }
        cout << i << endl;
    }

return 0;
}

output:

1
2




Post a Comment

Previous Post Next Post