0% found this document useful (0 votes)
8 views12 pages

Lec 5

The document discusses the conditional operator in programming, explaining its syntax, usage, and precedence compared to other operators. It also covers branching structures using if and if-else statements, providing examples of how to implement conditional logic in programs. Additionally, it highlights important considerations regarding data types and precision in numerical computations.

Uploaded by

vvishalsingh156
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views12 pages

Lec 5

The document discusses the conditional operator in programming, explaining its syntax, usage, and precedence compared to other operators. It also covers branching structures using if and if-else statements, providing examples of how to implement conditional logic in programs. Additionally, it highlights important considerations regarding data types and precision in numerical computations.

Uploaded by

vvishalsingh156
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

FUNDAMENTALS OF COMPUTER

PROGRAMMING (DSC-3)
Dr. Sangeeta Yadav

https://sites.google.com/view/sangeetayadav/home

Operators (Continued),
Programs with Branching Structure
The Conditional Operator
▪ The conditional operator is of the form

Expression 1 ? Expression 2 :
Expression
▪ Meaning: Evaluate 3 1, if it is true (non-zero), evaluate
expression
expression 2, otherwise evaluate expression 3
▪ The operator generates the value of expression 2 or expression 3
▪ Often, we assign the result to another variable (a = exp1 ? exp2 : exp3)
▪ Data type of generated value ? Whichever of exp2 or exp3 is of higher type
▪ Precedence of cond. operator is just above assignment operators
▪ Associativity of cond. operator is right to left

2
The Conditional Operator: Some
Examples
▪ a = (i>0) ? 100 : 10; /* a will be 100 or 10 depending on i */

▪ a = (i>0)? 10.0 : 5; /* RHS result will be a float */

▪ A sophisticated example (expression 1 consisting of multiple


operators)
▪ c += ( a>0 && a<=10 ) ? ++a : a/b;
▪ The above will first evaluate a>0 && a<=10 and then choose ++a or a/b
▪ Result from RHS will be added to c (c = c + result)

3
Now our table is..
Note: Precedence of brackets () is above every other operator
HIGH Operators Description Associativity

(unary) + -, ! Unary plus/minus, logical NOT Right to left

*/% Multiply, divide, remainder Left to right

+- Add, subtract Left to right


Precedenc

< > >= <= Relational operators Left to right

== != Equal, not equal Left to right


e

&& Logical And Left to right

|| Logical Or Left to right

?: Conditional Right to left

= Assignment Right to left


LOW 4
Note: Ensure Your Expressions Say
What You Mean
0 <= 10 && 10 <= 4
0 <= 10 <= 4
(0 <= 10) && (10 <= 4)
(0 <= 10) <= 4
(1) && (10 <= 4)
1 <= 4
1 && (0)
1 /* True */
0 /*False*/
5
Some Useful Tips on using correct Data
Types
● Double and float are both happy with %f for printf
● However, in scanf, double insists on %lf (%f gives junk)
● Don’t use a float/double for long integers
● When you say long a = 3213213210, since the number is within range
of long, the system will preserve every digit of it carefully
● When you say float a = 3213213210, the system will store
3213213184.00
● The number is like 3.2 x 109 and my error was just 26. Don’t blame me!
● Choice between float or double: If you don’t want your digits after
decimal to be rounded off, use double instead of float

6
Precision
• There are infinite real numbers between any two real numbers
• We can represent only 232 numbers in 32 bits
• So we can store only a vanishingly small number of decimal
numbers precisely
• All others are approximated to 8 (float) or 16 (double) decimal
places

7
Programs with Conditional
Statements
If condition true
do abc
Otherwise
do xyz

But didn’t you just


teach me about Yes, but they are usually for small
conditional expressions. For more complex
operators ? programs, I have some something
different (and better) for you ☺ 8
Branching using if statement
int main(){
int salary, loan = 0; // 0 means not approved, 1 means approved (initialize with 0)
float interest_rate; Testing condition is an expression
that gives 0 or 1 value
scanf(“%d”,& salary);
if (salary >= 400000) { Braces required only when there are multiple
statements within the if block
loan = 1; // 1 means loan approved
interest_rate = 10.0;
} Will execute this block of code only if the
condition (salary > 400000) is true (1)
// other stuff in the program..
}

9
Branching using if-else statement
int main(){
int salary, loan_amount;
float interest_rate;
The if block (can have one or
scanf(“%d”,& salary); more statements)
if (salary > 400000) {
loan_amount = 1000000;
interest_rate = 10.0;
printf(“Congratulations! Your loan amount is %d, interest rate is %d”,loan_amount,interest_rate);
} The else block (can have one
else { or more statements)

printf(“Sorry! Your loan cannot be approved”);


}
// do other stuff in the program
10
}
Various ways of using if and else
if (condition) {
if (condition-1) { if (condition-1) {
}
} if (condition-2) {
if (condition) { }
else if (condition-2) { “nested” if
} else {
} }
else {
else if (condition-3) { }
}
} else {
if (condition-1) { if (condition-3) {
}
} else if (condition-N) { else {
else if (condition-2) { } }
} else { }
else { }
Note: Each else must have a matching
if (also, number of if must be equal to
} 11
or more than number of else)
Be Careful with Braces when using
if-else
• If you do not put curly braces, Mr. C will try to put them for you (and
maybe in a way that you don’t want him to)
If you write like this…. Mr. C will treat it like this internally

if((a != 0) && (b != 0)) if((a != 0) && (b != 0)){


if(a * b >= 0) if(a * b >= 0){
printf(“Positive product”); printf(“Positive product”);
else }else{
printf(“One number is zero”); printf(“One number is zero”);
If you do not put brackets, I }
will match else to closest if But that is not
} what I meant
I will not care how you did indentation 12

You might also like