Wednesday, November 15, 2023

Decision or branching control structures

Control structures allow to change the flow of execution sequence of a program. 

C++ has two types of control structures

1. Decision or branching control structures

2. Looping or iterative control structures

Decision or branching control structures

Conditional branching statements help to jump from one part of the program to another depending on whether a particular condition is satisfied or not..

In C++ we have the following Decision Control Statements (or) Conditional Branching Statements

1. If statement.

2. If – else statement.

3. If—else – if statement.

4. Switch case statement.

1. IF statement

If statement is the simplest form of decision control statement. It is used frequently in decision making


Syntax:

If( Test Expression)

{

Body of the if;

}

Statement x;

If the test expression is true the “body of the if” will be executed, if it false control goes to the statements just below the if block.

Example program:

#include<iostream.h>

#include<conio.h>

{

Void main()

{

clrscr();

int age;

cout<<”enter your age \n”;

cin>>age;

if(age>=18)

Cout<<”you are eligible to vote”;

cout<<”not eligible to vote”;

getch();

}

2. IF-ELSE statement

The If-Else statement is an extension of simple if Statement. It has additional block that is else-block (or) false block.


Example Program: To find the Biggest of Two numbers

 

#include<iostream.h>

#include<conio.h>

Void main()

{

clrscr();

int a=0,b=0;

cout<<”Enter a,b values…\n”;

cin>>a>>b;

if(a>b)

cout<<a<<” is big”;

else

cout<<b<<” is big”;

getch();

}

3. IF – Else- IF statement (Nested if)

When a series of decisions are involved we may have to use more than one if else statement in nested form.

Syntax:

If (condition-1)

{

Statement block 1;

}

else if ( condition-2)

{

Statement block 2;

}

lse if( condition-n)

{

Statement block-n;

}

Statement x;

4. Switch Case Statement: A switch in C++ checks for equating multiple values to the variable (or) expression given in the switch header.

If case result is true, the set of the statements written in that case are executed. The keyword “break” should be placed at the end of the statement block otherwise the control passes to the statements of next case and goes on executing till a break is encountered or the statements in the switch case or over.


Syntax:

 

switch(expression)

{

case value1 :

body1

break;

case value2 :

body2

break;

case value3 :

body3

break;

default :

default-body

break;

}

next-statement;

Example Program:

#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

char o;

float num1,num2;

cout<<"Select an operator either + or - or * or / \n";

cin>>o;

cout<<"Enter two values: ";

cin>>num1>>num2;

switch(o) {

case '+':

cout<<num1<<" + "<<num2<<" = "<<num1+num2;

break;

case '-':

cout<<num1<<" - "<<num2<<" = "<<num1-num2; break;

case '*':

cout<<num1<<" * "<<num2<<" = "<<num1*num2;

break;

case '/':

cout<<num1<<" / "<<num2<<" = "<<num1/num2;

break;

default:

/* If operator is other than +, -, * or /, error message is shown */

cout<<"Error! operator is not correct";

break;

}

getch();

}





0 comments:

Post a Comment

Data Structures with C++



NET/SET/CS PG



Operating Systems



Computer Networks



JAVA



Design and Analysis of Algorithms



Programming in C++

Top