5. decision making and conditional operator (1)
TRANSCRIPT
-
7/30/2019 5. Decision Making and Conditional Operator (1)
1/15
Decision making
Logical operators
Conditional operators
Decision Making in C language
31 August 20131
-
7/30/2019 5. Decision Making and Conditional Operator (1)
2/15
if statement
Selection statement which performs an
indicated action only when the condition is
true
Syntax
if ()
{
.
}
31 August 20132
-
7/30/2019 5. Decision Making and Conditional Operator (1)
3/15
Example
main( )
{ int age;
scanf(%d,&age);
if (age>=18)
{
printf(\n You can cast your vote !!);
}
}
31 August 20133
-
7/30/2019 5. Decision Making and Conditional Operator (1)
4/15
if else statement
Selection statement that allows the programmer
to specify different actions to be performed when
condition is true than when the condition is false.
31 August 20134
-
7/30/2019 5. Decision Making and Conditional Operator (1)
5/15
Example
main( ){ int age;scanf(%d,&age);if (age>=18)
{printf(\n You can cast your vote !!);
}else
{printf(\n You cannot cast your vote !!);
}}
31 August 20135
-
7/30/2019 5. Decision Making and Conditional Operator (1)
6/15
Nested if . else statement
Test for multiple cases by placing if else inside
other if else
31 August 20136
-
7/30/2019 5. Decision Making and Conditional Operator (1)
7/15
Example
main( ){ int grade;
scanf(%d,&grade);
if (grade>=90){
if(grade>=95)
printf(\n A+);else
printf(\n A);
}31 August 20137
-
7/30/2019 5. Decision Making and Conditional Operator (1)
8/15
If-else-if ladder
In certain cases multiple conditions are to betested.
In such cases these conditions and their associated
statements can take following form:
if(condition 1)
statement 1;
else if(condition 2)
statement 2;else if(condition 3)
statement 3;
else
statement; 31 August 20138
-
7/30/2019 5. Decision Making and Conditional Operator (1)
9/15
Example
main( ){ int grade;
scanf(%d,&grade);
if (grade>=90) printf(\n A);
else if (grade>=80) printf(\n B);
else if (grade>=70) printf(\n C);
else if (grade>=60) printf(\n D);
else printf(\n F);}
31 August 20139
-
7/30/2019 5. Decision Making and Conditional Operator (1)
10/15
Exercises
Obtain two numbers from the keyboard and
determine and display which is larger
Obtain a number and print whether the
number is even or odd Obtain three numbers from the keyboard and
determine and display which is larger
Obtain two numbers from user and determine
and print if the first is multiple of the second
31 August 201310
-
7/30/2019 5. Decision Making and Conditional Operator (1)
11/15
Logical Operators
&& (AND)
|| (OR)
! (NOT)
Bitwise operators
& (Bitwise AND)
| (Bitwise OR)
31 August 201311
-
7/30/2019 5. Decision Making and Conditional Operator (1)
12/15
Conditional Operators
Expression 1?expression 2:expression 3
If expression 1 is true then the value returned
will be expression 2 otherwise expression 3
Ex. Y=(x>5?3:4)
if(x>5)
Y=3;
else
Y=4;
31 August 201312
-
7/30/2019 5. Decision Making and Conditional Operator (1)
13/15
Example: Conditional Operators
main( ){
int age;
scanf(%d,&age);
printf( You are );
printf(age
-
7/30/2019 5. Decision Making and Conditional Operator (1)
14/15
The switch construct
Instead of using if-else-if ladderthe switchconstruct can be used to handle multiple choices
such as MENU choice.
sw itch(contro l var iable)
case constant-1:
statements(s);
break;
case constant-2:statements(s);
break;
default :
statements; 31 August 201314
-
7/30/2019 5. Decision Making and Conditional Operator (1)
15/15
Example
31 August 201315