Logical Statements and Selection Structures
Logical Statements and Selection Structures
Structures
Car Loan Problem
• You have saved some amount to use as a down payment
on a car. Before beginning your car shopping, you decide to
write a program to help you figure out what your monthly
payment will be, given the car’s purchase price, the
monthly interest rate, and the time period over which you
will pay back the loan. The formula for calculating your
payment is
payment = iP / (1 - (1 + i)-n )
where P = principal (the amount you borrow)
i = monthly interest rate (1/12 of the annual rate)
n = total number of payments
Total number of payments is usually 36, 48, or 60 (months).
Program should then display the amount borrowed and the
monthly payment rounded to two decimal places.
Car Loan problem
• %[flags][width][.precision][length]specifier
Formatting Output
Flags Description
- Left-justify within the given field width; Right
justification is the default
+ Forces to precede the result with a plus or
minus sign (+ or -) even for positive numbers.
By default, only negative numbers are
preceded with a -ve sign.
(space) If no sign is going to be written, a blank space
is inserted before the value
0 Left-pads the number with zeroes (0) instead
of spaces, where padding is specified
Formatting Output
Width Description
(number) Minimum number of characters to be printed. If
the value to be printed is shorter than this number,
the result is padded with blank spaces.
.precision Description
.number precision specifies the minimum number of digits
to be written. If the value to be written is shorter
than this number, the result is padded with leading
zeros.
Formatting Output
length Description
h Argument is interpreted as a short int or unsigned
short int (only applies to integer specifiers: i, d, o,
u, x and X).
l argument is interpreted as a long int or unsigned
long int for integer specifiers (i, d, o, u, x and X),
and as a wide character or wide character string
for specifiers c and s.
L argument is interpreted as a long double (only
applies to floating point specifiers: e, E, f, g and G)
Example for Formatting
Output
Example for Formatting
Output
Example for Formatting
Output
printf(“:%s:\n”, “Hello, world!”); - nothing special happens
printf(“:%15s:\n”, “Hello, world!”); print 15 characters. If
string is smaller then “empty” positions will be filled with
“whitespace.”
printf(“:%.10s:\n”, “Hello, world!”); statement prints the
string, but print only 10 characters of the string.
printf(“:%-10s:\n”, “Hello, world!”); statement prints the
string, but prints at least 10 characters. If the string is
smaller “whitespace” is added at the end.
printf(“:%-15s:\n”, “Hello, world!”); statement prints
the string, but prints at least 15 characters. The string
in this case is shorter than the defined 15 character,
thus “whitespace” is added at the end (defined by the
minus sign.)
printf(“:%.15s:\n”, “Hello, world!”); statement prints
the string, but print only 15 characters of the string. In
this case the string is shorter than 15, thus the whole
string is printed.
To compile – gcc carloan.c –lm
./a.out
When input is:
400000
100000
10
36
Output is:
Loan Amount 300000.00
Monthly installment 9680.16
Nature of a Solution
• A solution may be classified into acidic, very
acidic, neutral, alkaline or very alkaline based
on its pH value. The nature of the solution is
given by the following table and determined
as shown in the figure:
pH value Nature of Solution
0 to 2 Very acidic
Greater than 2 and less than 7 Acidic
Equal to 7 Neutral
Greater than 7 and less than 12 Alkaline
Greater than 12 Very Alkaline
Nature of a Solution
pH problem
1 (true)
x + y / z <= 3.5
1 ||1 = 1
Short Circuit Evaluation
• Stopping evaluation of a logical expression as
soon as its value can be determined is called
short-circuit evaluation
if ( condition )
statement T ;
else
statement F ;
Example
if (x >= 0.0)
printf("positive\n");
else
printf("negative\n");
If condition evaluates to true ( a nonzero value), then
statementT is executed and statementF is skipped;
otherwise, statementT is skipped and statementF is
executed.
Example 1
• #include<stdio.h>
void main()
{
float i=0.0f;
if(i)
printf("Yes");
else
printf(“No”);
}
Output 1
• No
Example 2
• #include<stdio.h>
void main()
{
int i=-3;
if(i)
printf("Yes");
else
printf(“No”);
}
Output 2
• Yes
Example 3
• #include<stdio.h>
void main()
{
char i =‘a’;
if(i)
printf("Yes");
else
printf(“No”);
}
Output 3
• Yes
Example 4
• #include<stdio.h>
void main()
{
int a = 2 , b=5;
if ((a==0)&&(b=1))
printf(“Hi”);
printf(“a is %d and b is %d”, a, b);
}
Output 4
• a is 2 and b = 5
Example 5
• #include<stdio.h>
void main()
{
int a = 2 , b=5;
if ((a==2)&&(b=1))
printf(“Hi”);
printf(“a is %d and b is %d”, a, b);
}
Output 5
• a is 2 and b = 1
Conditional Operator (Ternary Operator)
Syntax:
expression 1 ? expression 2 : expression 3
“if expression 1 is true (that is, if its value is non-zero), then the
value returned will be expression 2, otherwise the value returned
will be expression 3”.
Conditional Operator (Ternary Operator)
Example:
char a ;
int y ;
scanf ( "%c", &a ) ;
y = ( a >= 65 && a <= 90 ? 1 : 0 ) ;
Here 1 would be assigned to y if a >=65 && a <=90 evaluates to
true, otherwise 0 would be assigned
Conditional Operator (Ternary Operator)
a>b?g=a:g=b;
Compiler Error
a>b?g=a:(g=b);
Partial C code of Isogram Problem Extended
/* Code to get the number of letters from user and print it*/
#include<stdio.h>
int main()
{
int num_Of_Letters; // Declaration of variable is mandatory in C
int num_Of_Words ; // Memory is allocated but not initialized
scanf(“%d”,&num_Of_Letters);
//If num_Of_Letters in less than or equal to zero then error
if (num_Of_Letters<=0)
printf(“Invalid input”);
Partial C code of Isogram Problem Extended
else
{
printf(“Number of letters is %d”,num_Of_Letters);
// multiply all numbers from n to 1 to find number of
// number of isogram words that can be formed
}
}
Nested if Statements
if (x >= 0.0)
printf("positive\n");
else
printf("negative\n");
If condition evaluates to true ( a nonzero value), then
statementT is executed and statementF is skipped;
otherwise, statementT is skipped and statementF is
executed.
Nested if Statements
if ( condition1 )
statement1
else if ( condition2 )
statement2
. . .
else if ( conditionn)
statementn
else
statemente
Example
/* increment num_pos, num_neg, or num_zero depending on x */
if (x > 0)
num_pos = num_pos + 1;
else if (x < 0)
num_neg = num_neg + 1;
else /* x equals 0 */
num_zero = num_zero + 1;
#define statements
• These are preprocessor directives that are used
define constants
BattleshipCruiserDestroyerFrigateNo match
When input is b
In Lab Practice Problem
The table below shows the normal boiling points of
several substances. Write a program that prompts the
user for the observed boiling point of a substance in °C
and identifies the substance if the observed boiling
point is within 5% of the expected boiling point. If the
data input is more than 5% higher or lower than any of
the boiling points in the table, the program should
output the message Substance unknown.
Substance Normal boiling point
(°C)
Water 100
Mercury 357
Copper 1187
Silver 2193
Gold 2660