Decision-making is at the heart of programming, enabling developers to control the flow of execution based on conditions. In C++, decision-making statements play a crucial role in implementing logic and providing dynamic responses in real-world applications. From automated systems like ATMs to daily functionalities like traffic signal management and e-commerce platforms, these statements are indispensable.
For instance, in an ATM withdrawal machine, a simple if
statement determines whether the user has sufficient balance for a transaction. Similarly, a traffic light system uses an if-else
ladder to decide the light’s state based on a timer. In e-commerce, conditions such as membership levels or cart items dynamically influence user experiences like discounts and checkout availability.
These statements are depending on conditions.
Real time scenarios where the decision making statements are used.
if (balance >= withdrawalamount) {
printf(“transaction successful. please collect your cash.\n”);
balance -= withdrawalamount;
} else {
printf(“insufficient balance. transaction denied.\n”);
}
if (timer <= 10) {
printf(“Green Light\n”);
} else if (timer > 10 && timer <= 15) {
printf(“Yellow Light\n”);
} else {
printf(“Red Light\n”);
}
if(cartitems>0)
{
printf(“checkout possible”);
else
{
printf(“checkout not possible”);
}
Example 4:
if(membership==gold)
{
discount=20;
}
elseif(membership==silver)
{
discount=10;
}
else
{
discount=5;
}
int a=10;
if(a>10)
{
printf(“ you can go”);
}
int a=10;
if(a>10)
{
printf(“ you can go”);
}
else
{
printf(“you cant go”);
}
if (distance <= 5) {
deliveryCharge = 20;
} else if (distance <= 10) {
deliveryCharge = 40;
} else {
deliveryCharge = 60;
}
cout<<“delivery charge”<< deliverycharge;
#include<iostream>
using namespace std;
int main()
{
int choice;
int balance =1000;
int withdraw;
int deposit;
cout<<“atm menu\n”;
cout<<“check balance\n”;
cout<<“withdraw\n”;
cout<<“savings\n”;
cout<< “enter the option\n”;
cin>>choice;
if(choice==1)
{
cout<<balance<<endl;
}
else if(choice==2)
{
cin>>withdraw;
cout<<balance+withdraw;
}
else if(choice==3)
{
cin>>deposit;
cout<<balance+deposit;
}
else{
cout<<“transaction not possible”;
}
}
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
int password=1234;
char username[5]=”iies”;
if((strcmp(username,”iies”)==0))
{
if(password==1234)
{
cout<<“login successful”;
}
else{
cout<<“invalid password”;
}
}
else{
cout<<“invalid username”;
}
}
#include <iostream>
using namespace std;
int main() {
char grade = ‘A’;
switch (grade) {
case ‘A’:
cout << “Excellent”;
break;
case ‘B’:
cout << “Good”;
break;
case ‘C’:
cout << “Fair”;
break;
case ‘D’:
cout << “Poor”;
break;
default:
cout << “Invalid Grade”;
}
return 0;
}
Conditional operator is used to reduce the no of line of the code.
The conditional operator is often used to assign a value to a variable based on a condition, making the code more concise and readable.
#include <iostream>
using namespace std;
int main() {
int age = 18;
string result = (age >= 18) ? “Adult” : “Minor”;
cout << “You are: ” << result << endl;
return 0;
}
By using break statement it exit the particular loop.
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
break; // Exit the loop when i is 3
}
cout << i << ” “;
}
cout << “\nLoop terminated at i = 3\n”;
return 0;
}
Continue is going to skip current iteratona dn goes to the next iteration.
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
for(int k=0;k<5;k++)
{
cout<<k;
if(k==2)
{
continue;
}
}
}
The goto statement transfers control to a labeled statement in the program. It is generally avoided in modern programming due to its tendency to make code harder to follow.
#include <iostream>
using namespace std;
int main() {
int x = 1;
loop_start: // Label
if (x > 5) {
cout << “Exiting loop\n”;
return 0;
}
cout << “x = ” << x << endl;
x++;
goto loop_start; // Jump back to loop_start
return 0;
}
Indian Institute of Embedded Systems – IIES