DECISION MAKING STATEMENTS IN C++
The decision making statement in a programming language help the programmer to transfer the control from one part to another part of the program and thus controls the flow of program execution in c++. we are having different types of decision making statement such as .
case 5:
The decision making statement in a programming language help the programmer to transfer the control from one part to another part of the program and thus controls the flow of program execution in c++. we are having different types of decision making statement such as .
- if statement
- if-else statement
- else-if statement
- switch statement
- Go-to statement
- nesting of if -else statement
- Break statement
- continuous statement
C++ use the keyword if to execute a set of a command of lines or one command line when the logical condition is true it means that the if block statement only execute when the condition is true and if condition is false than the control passes to the next statement after the if block .
SYNTAX:
if(condition)
statement 1;
OR
if (condition)
{
statement 1;
statement 2;
}
EXAMPLE:
int a=2;
int b=3;
int c=4;
if(a<<b&&b<<c)
{
cout<<"helloo";
cout<<"exit";
}
IF-ELSE STATEMENT
If the condition in if statement evaluates to true,then the specified set of statement block is executed ,otherwise the statement -block in the else block is executed. In other words,the if-else statements contains a set of statement-block for both the cases , either the condition evaluates to true or false , The syntax of the if statement is as follows :-
SYNTAX:
if(condition)
{
statement 1;
statement 2;
}
else
{
statement 1;
statement 2;
}
EXAMPLE:
#include<iostream>
using namespace std ;
void main()
{
int a=15,b=20;
if(b>a)
{
cout<<"b is greater "<<endl;
}
else
{
cout<<"a is greater "<<endl;
}
}
ELSE-IF STATEMENTS
What is the need of else-if statement when is-else has both the options i.e, Either the specified conditions evaluate to true or false,a corresponding set of statement block will be executed depending upon the condition status.But what if ,When we want to use multiple conditions with the if statement .For this , C++ provides an else-if block.
SYNTAX:
if(condition)
{
statement 1;
statement 2;
}
else if
{
statement 1;
statement 2;
}
else if
{
statement 1;
statement 2;
}
else
{
statement 1;
}
EXAMPLE:
#include<iostream>
using namespace std ;
void main()
{
int a,b,c;
cout<<"enter the values ";
cin>>a>>b>>c;
if(a>b&&a>c)
{
cout<<"a is greater ";
}
else if (b>a&&b>c)
{
cout<<"b is greater ";
}
else
{
cout<<"c is greater ";
}
getch();
}
SWITCH STATEMENT
A switch statement allows a variable to be tested for quality against a list of values .Each value is called a case , and the variable being switched on is checked for each case .
SYNTAX:
Switch(variable)
{
case 1: // execute your code
break;
case n: // execute your code
break;
default: // execute your code
break;
}
EXAMPLE:
#include<iostream>
using namespace std;
void main()
{
int a ;
cout<<"please enter a number between 1 and 5 :"<<endl;
cin>>a;
switch(a)
{
case 1:
cout<<"you chose one "<<endl;
cin>>a;
break;
case 2:
cout<<"you chose two"<<endl;
break;
case 3:
cout<<"you chose three "<<endl;
break;
case 4:
cout<<"yopu chose four "<<endl;
break;
case 5:
cout<<"you chose five "<<endl;
break;
default:
cout<<"invalid choice enter a number between 1 and 5"<<endl;
break;
}
GO-TO STATEMENT
t The go-to statement does not require only condition. This is an unconditional control jump which means that this statement passes control any where in the program . i.e control is transferred to another part of the program without any text condition.go-to is always associated with a lab of which describes the position where the control is to be transferred.
SYNTAX:
void main()
{
statement 1;
statement 2;
{
statement 1;
statement 2;
go to 2;
}
label 1;
statement 1;
statement 2;
label 2:
statement 1;
statement 2;
go to 1;
statement 1;
statement 2;
}
EXAMPLE:
void main()
{
int x;
cout<<"enter a number ";
cin>>x;
if(x%2==0)
go to even;
else
go to odd;
cout<<"even number:"<<x;
cout<<"odd number:"<<x;
}
NESTED IF-ELSE STATEMENT
Nested if-else is used as a block or statement within the if else block. By using this we can have multiple if else statements.When we are having inner is else block than we first solve that ans then move on to outer if else block.
SYNTAX:
if(expression)
{
if(expression)
{
statement-block 1;
}
else
{
statement-block 2;
}
}
else
{
statement-block 3;
}
if 'expression 'is false the' statement block-3' will be executed ,otherwise it continuous to perform the test for' expression 1' , if the 'expression 1' is true the 'statement block 1' is executed otherwise 'statement -block 2 'is executed
EXAMPLE:
void main()
{
int a,b,c;
clrscr();
cout<<"enter the 3 numbers of your choice ";
cin>>a>>b>>c;
if(a>b)
{
if(a>c)
{
cout<<"a is greatest ";
}
else
{
cout<<"c is greatest ";
}
}
else
{
if(b>c)
{
cout<<"b is greatest";
}
else
{
cout<<"c is greatest";
}
}
getch();
}
BREAK STATEMENT
The break statement is used to terminated the loop as it means that whenever we encounter a break statement in a loop than the control skips the remaining statements of the loop,and passes the control to the first statement after the loop
- no header file is indeed
- it is a keyword
- it stops the execution of the loop
{
statement 1;
statement 2;
break;
}
statement 3;
CONTINUOUS STATEMENT
Continuous statement is used to reenter in the loop which means that if we encounter continuous statement than the control skip the rest of the statement in the loop and takes the control to the text condition at the beginning of the loop and re enter in it.
if(condition)
{
statement 1;
statement 2;
continuous
statement 3;
}
No comments:
Post a Comment