conditional statements in c - wordpress.com...the switch differs from the else-if in that switch can...
TRANSCRIPT
![Page 1: CONDITIONAL STATEMENTS IN C - WordPress.com...The switch differs from the else-if in that switch can test only for equality, whereas the if conditional expression can be of a test](https://reader033.vdocuments.us/reader033/viewer/2022052800/5f10080e7e708231d4471a5e/html5/thumbnails/1.jpg)
CONDITIONAL STATEMENTS IN C
![Page 2: CONDITIONAL STATEMENTS IN C - WordPress.com...The switch differs from the else-if in that switch can test only for equality, whereas the if conditional expression can be of a test](https://reader033.vdocuments.us/reader033/viewer/2022052800/5f10080e7e708231d4471a5e/html5/thumbnails/2.jpg)
Introduction
Selection Statements
• if
• if-else
• switch
Iteration Statements
• for
• while
• do-while
Jump Statements
• goto
• break
• continue
• return
![Page 3: CONDITIONAL STATEMENTS IN C - WordPress.com...The switch differs from the else-if in that switch can test only for equality, whereas the if conditional expression can be of a test](https://reader033.vdocuments.us/reader033/viewer/2022052800/5f10080e7e708231d4471a5e/html5/thumbnails/3.jpg)
Relational Operators
To Specify Symbol Used
less than
<
greater than
>
less than or equal to
greater than or
equal to
<=
>=
To Specify
Symbol Used
Equal to
==
Not equal to
!=
Logical AND
&&
Logical OR
||
Negation !
Equality and Logical Operators
![Page 4: CONDITIONAL STATEMENTS IN C - WordPress.com...The switch differs from the else-if in that switch can test only for equality, whereas the if conditional expression can be of a test](https://reader033.vdocuments.us/reader033/viewer/2022052800/5f10080e7e708231d4471a5e/html5/thumbnails/4.jpg)
Points to Note
If an expression, involving the relational operator, is true, it is given a
value of 1. If an expression is false, it is given a value of 0. Similarly, if a numeric expression is used as a test expression, any non-zero value (including negative) will be considered as true, while a zero value will be considered as false.
Space can be given between operand and operator (relational or logical) but space is not allowed between any compound operator like <=, >=, ==, !=. It is also compiler error to reverse them.
a == b and a = b are not similar, as == is a test for equality, a = b is an assignment operator. Therefore, the equality operator has to be used carefully.
The relational operators have lower precedence than all arithmetic operators.
![Page 5: CONDITIONAL STATEMENTS IN C - WordPress.com...The switch differs from the else-if in that switch can test only for equality, whereas the if conditional expression can be of a test](https://reader033.vdocuments.us/reader033/viewer/2022052800/5f10080e7e708231d4471a5e/html5/thumbnails/5.jpg)
Conditional Execution and Selection
• Selection Statements
• The switch Statement
• The Conditional Operator
![Page 6: CONDITIONAL STATEMENTS IN C - WordPress.com...The switch differs from the else-if in that switch can test only for equality, whereas the if conditional expression can be of a test](https://reader033.vdocuments.us/reader033/viewer/2022052800/5f10080e7e708231d4471a5e/html5/thumbnails/6.jpg)
Selection Statements
1. One-way decisions using if statement
2. Two-way decisions using if-else statement
3. Multi-way decisions using if - else if -else
4. Dangling else Problem i.e. else without if
![Page 7: CONDITIONAL STATEMENTS IN C - WordPress.com...The switch differs from the else-if in that switch can test only for equality, whereas the if conditional expression can be of a test](https://reader033.vdocuments.us/reader033/viewer/2022052800/5f10080e7e708231d4471a5e/html5/thumbnails/7.jpg)
One-way decisions using if statement
Flowchart for if construct
if(TestExpr)
{
stmtT; T F
}
Next-stmt;
TestExpr
stmtT
Next-stmt
![Page 8: CONDITIONAL STATEMENTS IN C - WordPress.com...The switch differs from the else-if in that switch can test only for equality, whereas the if conditional expression can be of a test](https://reader033.vdocuments.us/reader033/viewer/2022052800/5f10080e7e708231d4471a5e/html5/thumbnails/8.jpg)
Write a program that prints the largest among three numbers.
Algorithm C Program
1. START #include <stdio.h> int main() { int a, b, c, max; printf(“\nEnter 3 numbers”); scanf(“%d %d %d”, &a, &b, &c); max=a; if(b>max) max=b; if(c>max) max=c; printf(“Largest No is %d”, max); return 0; }
2. PRINT “ENTER THREE NUMBERS”
3. INPUT A, B, C
4. MAX=A
5. IF B>MAX THEN MAX=B
6. IF C>MAX THEN MAX=C
7. PRINT “LARGEST NUMBER IS”, MAX
8. STOP
![Page 9: CONDITIONAL STATEMENTS IN C - WordPress.com...The switch differs from the else-if in that switch can test only for equality, whereas the if conditional expression can be of a test](https://reader033.vdocuments.us/reader033/viewer/2022052800/5f10080e7e708231d4471a5e/html5/thumbnails/9.jpg)
Two-way decisions using if-else statement
The form of a two-way decision is as follows:
if(TestExpr) { stmtT; } else { stmtF; } Next-stmt;
T F
Flowchart of if-else construct
TestExpr
stmtT stmtF
Next-stmt
![Page 10: CONDITIONAL STATEMENTS IN C - WordPress.com...The switch differs from the else-if in that switch can test only for equality, whereas the if conditional expression can be of a test](https://reader033.vdocuments.us/reader033/viewer/2022052800/5f10080e7e708231d4471a5e/html5/thumbnails/10.jpg)
If else-if ladder
if(TestExpr1)
stmtT1;
else if(TestExpr2)
stmtT2;
else if(TestExpr3)
stmtT3;
.. .
else if(TestExprN)
stmtTN;
else
stmtF;
Next-Stmt;
![Page 11: CONDITIONAL STATEMENTS IN C - WordPress.com...The switch differs from the else-if in that switch can test only for equality, whereas the if conditional expression can be of a test](https://reader033.vdocuments.us/reader033/viewer/2022052800/5f10080e7e708231d4471a5e/html5/thumbnails/11.jpg)
Flowchart of an if-else-if Construct
• F
T
TestExpr
TestExpr2
TestExprN
TestExpr3
stmtT2
stmtTN
stmtT3 stmtTF
stmtT1
![Page 12: CONDITIONAL STATEMENTS IN C - WordPress.com...The switch differs from the else-if in that switch can test only for equality, whereas the if conditional expression can be of a test](https://reader033.vdocuments.us/reader033/viewer/2022052800/5f10080e7e708231d4471a5e/html5/thumbnails/12.jpg)
switch(expr)
{
case constant1: stmtList1;
break;
case constant2: stmtList2;
break;
case constant3: stmtList3;
break;
………………………….
………………………….
default: stmtListn;
}
The switch statement
![Page 13: CONDITIONAL STATEMENTS IN C - WordPress.com...The switch differs from the else-if in that switch can test only for equality, whereas the if conditional expression can be of a test](https://reader033.vdocuments.us/reader033/viewer/2022052800/5f10080e7e708231d4471a5e/html5/thumbnails/13.jpg)
switch vs nested if
The switch differs from the else-if in that switch can test only for equality, whereas the if conditional expression can be of a test expression involving any type of relational operators and/or logical operators.
A switch statement is usually more efficient than nested ifs.
The switch statement can always be replaced with a series of else-if statements.
![Page 14: CONDITIONAL STATEMENTS IN C - WordPress.com...The switch differs from the else-if in that switch can test only for equality, whereas the if conditional expression can be of a test](https://reader033.vdocuments.us/reader033/viewer/2022052800/5f10080e7e708231d4471a5e/html5/thumbnails/14.jpg)
Practice Questions
• WAP to accept 3 sides of a triangle and check whether a triangle can be formed or not
• WAP to accept SP and CP of the product and state, whether, it reflects profit or loss and also calculate the profit and the loss amount
• WAP to accept a character from the user and check whether it is an uppercase alphabet or lowercase alphabet or digit or space or any other character
![Page 15: CONDITIONAL STATEMENTS IN C - WordPress.com...The switch differs from the else-if in that switch can test only for equality, whereas the if conditional expression can be of a test](https://reader033.vdocuments.us/reader033/viewer/2022052800/5f10080e7e708231d4471a5e/html5/thumbnails/15.jpg)
Practice Questions
Calculate the electricity bill of the customer as follows:
No. of units Cost per Unit
Less than 100 1.2
Greater than 100 less than 300
1.7
Greater than 300 2.3
Greater than 500 4.8
![Page 16: CONDITIONAL STATEMENTS IN C - WordPress.com...The switch differs from the else-if in that switch can test only for equality, whereas the if conditional expression can be of a test](https://reader033.vdocuments.us/reader033/viewer/2022052800/5f10080e7e708231d4471a5e/html5/thumbnails/16.jpg)
Practice Questions
• WAP to accept two numbers from the user and an operation symbol from the user and perform the required operation using menu driven program