CSE 4107
QUIZ 4
Name: Tahsan Ferdous Lihan
ID: 220041203
Department: Computer Science and Engineering
Section: 2
Date of Submission: 24.12.2023
Assigned Topic: Conditional expression
Conditional statements are programming constructs that enable you to control the flow of
execution within your C code based on specified conditions. They empower you to make
decisions and execute different blocks of code depending on whether certain conditions are true
or false.
If statement:
The if statement is one of C's selection statements (sometimes called conditional statements). Its
operation is governed by the outcome of a conditional test that evaluates to either true or false.
Simply put, selection statements make decisions based upon the outcome of some condition.
In its simplest form, the if statement allows your program to conditionally execute a statement.
This form of the if is shown here:
if(expression)
statement;
The expression may be any valid C expression. If the expression evaluates as true, the statement
will be executed. If it does not, the statement is bypassed, and the line of code following the if is
executed. In C, an expression is true if it evaluates to any nonzero value. If it evaluates to zero, it
is false. The statement that follows an if is usually referred to as the target of the if statement.
Commonly, the expression inside the if compares one value with another using a relational
operator. Although you will learn about all the relational operators later in this chapter, three are
introduced here so that we can create some example programs. A relational operator tests how
one value relates to another. For example, to see if one value is greater than another, C uses the >
relational operator. The outcome of this comparison is either true or false. For example, 10 9 is
true, but 9 > 10 is false. Therefore, the following if will cause the message true to be displayed...
if (10> 9) printf("true"); However, because the expression in the following statement is false, the
if does not execute its target statement. if (5 > 9) printf("this will not print");
C uses < as its less than operator. For example, 10 < 11 is true. To test for equality, C provides
the == operator. (There can be no space between the two equal signs.) Therefore, 10 == 10 is
true, but 10 == 11 is not. Of course, the expression inside the if may involve variables.
Though usually a condition is used in if statement, any valid expressions will also do. In C a non-
zero value is considered to be true, whereas 0 is considered to be false. 10, -5,3.1416 each
number is considered as true.
If-else statement:
You can add an else statement to the if. When this is done, the if statement looks like this:
if(expression) statement1; else statement2; If the expression is true, then the target of the if will
execute, and the else portion will be skipped. However, if the expression is false, then the target
of the if is bypassed, and the target of the else will execute. Under no circumstances will both
statements execute. Thus, the addition of the else provides a two-way decision path.
Syntax of if-else:
if(condition){
//Code to execute if condition is true
}
else {
//Code to execute if condition is false
}
Nested if-else and else if:
else if statement also needs a condition inside the first bracket. This block executes only
if condition1 is false and condition2 is true. You can have multiple else if
statements, checking additional conditions one by one.
Think of it like a decision tree:
if (condition1) {
// Do something for condition1
} else if (condition2) {
// Do something for condition2
} else if (condition3) {
// Do something for condition3
} else {
// Do something for none of the above
}
The switch-case statement:
While if is good for choosing between two alternatives, it quickly becomes cumbersome when
several alternatives are needed. C's solution to this problem is the switch statement. The switch
statement is C's multiple selection statement. It is used to select one of several solution to this
problem is the switch statement. The switch statement alternative paths in program execution and
works as follows. A value is successively tested against a list of integer or character constants.
When a match is found, the statement sequence associated with that match is executed. The
general form of the switch statement is this:
switch (value) (
case constant1:
statement sequence
break;
case constant2:
statement sequence
break;
case constant3:
statement sequence
break;
case constant4:
statement sequence
break;
default:
statement sequence
break;
The default statement sequence is performed if no matches are found. The default is optional. If
all matches fail and default is absent, no action takes place.
When a match is found, the statements associated with that case are executed until break is
encountered or, in the case of default or the last case, the end of the switch is reached.
The Conditional Operator:
The conditional operators ? and are sometimes called ternary operators since they take three
arguments. In fact, they form a kind of foreshortened if-then-else. Their general form is,
expression 1 ? expression 2: expression 3
What this expression says is: "if expression 1 is true, then the value returned will be expression
2, otherwise the value returned will be expression 3".
Problem Description
You're tasked with developing a C program to efficiently manage restaurant bills. Your program
should accurately calculate the final bill amount, including discounts and charges, while
accommodating various payment methods.
Input
The first line of input contains a single integer N, representing the number of items ordered.
The following N lines each contain two space-separated values:
item_name: The name of the item (string without spaces).
item_price: The price of the item (float).
The next line contains a single character dessert, indicating whether dessert was ordered ('y' for
yes, 'n' for no).
The last line contains a single character payment_method, indicating the payment method ('c' for
cash, 'd' for credit).
Output
Print the formatted bill in the following format:
---------- Bill Details ----------
item1_name: item1_price
item2_name: item2_price
...
(Other items)
Subtotal: subtotal_amount
Discount: discount_amount (if applicable)
Service Charge: service_charge_amount (if applicable)
Total Amount: total_amount
Payment Method: payment_method
(For cash payment, also print:)
Change Due: change_due
(For credit payment, prompt for simulated card details)
Constraints
1 <= N <= 100
Item names consist of letters only, without spaces.
0.00 <= item_price <= 1000.00
dessert is either 'y' or 'n'.
payment_method is either 'c' or 'd'.
Sample Input
4
Burger 240
Pizza 400
Pasta 55
Snickers 100
y
d
Sample Output
---------- Bill Details ----------
Burger: 240.00
Pizza: 400.00
Pasta: 55.00
Snickers: 100.00
Subtotal: 795.00
Discount: 79.50
Service Charge: 39.75
Total Amount: 755.25
Solution:
#include <stdio.h>
struct item {
char name[50];
float price;
};
int main() {
int numItems, dessert;
char paymentMethod;
float billAmount = 0, discount, serviceCharge, totalAmount, changeDue;
// Gather order information
printf("Number of items ordered: ");
scanf("%d", &numItems);
struct item items[numItems]; // Array to store items
for (int i = 0; i < numItems; i++) {
printf("Item %d name: ", i + 1);
scanf("%s", items[i].name);
printf("Item %d price: ", i + 1);
scanf("%f", &items[i].price);
billAmount += items[i].price;
}
// Dessert and payment method
printf("Did you order dessert? (y/n): ");
scanf(" %c", &dessert);
printf("Payment method (‘c’ for credit /‘d’ for cash): ");
scanf(" %c", &paymentMethod);
// Calculate discount and service charge
discount = (billAmount > 500) ? billAmount * 0.1 : 0;
serviceCharge = (dessert == 'y') ? billAmount * 0.05 : 0;
// Calculate total amount
totalAmount = billAmount - discount + serviceCharge;
// Switch case for payment method
switch (paymentMethod) {
case 'c':
printf("Total amount: %.2f\n", totalAmount);
break;
case 'd':
float paidAmount;
printf("Enter paid amount: ");
scanf("%f", &paidAmount);
changeDue = paidAmount - totalAmount;
printf("Change due: %.2f\n", changeDue);
break;
default:
printf("Invalid payment method.\n");
}
// Generate bill output
printf("\n---------- Bill Details ----------\n");
for (int i = 0; i < numItems; i++) {
printf("%s: %.2f\n", items[i].name, items[i].price);
}
printf("Subtotal: %.2f\n", billAmount);
printf("Discount: %.2f\n", discount);
printf("Service Charge: %.2f\n", serviceCharge);
printf("Total Amount: %.2f\n", totalAmount);
return 0;
}