0% found this document useful (0 votes)
0 views37 pages

Lecture 5

The document covers selection and decision control structures in programming, including if statements, if-else statements, nested ifs, and switch statements. It emphasizes the importance of understanding the problem and planning before coding, as well as using logical operators and conditional operators effectively. Additionally, it provides examples and flowcharts to illustrate the concepts discussed.

Uploaded by

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

Lecture 5

The document covers selection and decision control structures in programming, including if statements, if-else statements, nested ifs, and switch statements. It emphasizes the importance of understanding the problem and planning before coding, as well as using logical operators and conditional operators effectively. Additionally, it provides examples and flowcharts to illustrate the concepts discussed.

Uploaded by

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

Selection/Decision Control

• The if statement
• The if-else statement
• The else-if construct
• The switch statement
• Decision depend upon conditions
– The conditional operators
• Equality
• Relational
Programming Methodology
• Before writing a program
– Have a thorough understanding of problem
– Carefully plan your approach for solving it
• While writing a program
– Know what “building blocks” are available
– Use good programming principles
If Statement
if ( expression/this condition is true )
execute this statement ;

Char ch;
Ch=getche();
If (ch == ‘y’)
Printf(“you typed y”);
Note: there is no “then” keyword in C
Flow chart
If Statement Flowchart
If Statement Flowchart..
If Statement Flowchart..
Coding the Flowchart..
/* Calculation of total expenses */
main( )
{
int qty, dis = 0 ;
float rate, tot ;
printf ( "Enter quantity and rate " ) ;
scanf ( "%d %f", &qty, &rate) ;
if ( qty > 1000 )
dis = 10 ;
tot = ( qty * rate ) - ( qty * rate * dis / 100 ) ;
printf ( "Total expenses = Rs. %f", tot ) ;
}
Compound Statements
• Compound statement
– Set of statements within a pair of braces
if ( grade >= 60 )
printf( "Passed.\n“ );
else {
printf ( "Failed.\n“ );
printf ( "You must take this course again.\n“);
}
– Without braces,
Printf ( "You must take this course again.\n“);
always executed
• Block
– Set of statements within braces
Compound Statements..
Nested if
• One if statement is part of the body of
another if statement.
• Inner ‘if’ will not be reached until outer ‘if’
is true.
• If (getche()==‘n’)
• if (getche()==‘o’)
• printf(“you typed no”);
Nested If- Flow chart
The if-else Statement
• if/else
– Different actions if conditions true or false
• Pseudocode
if student’s grade is greater than or equal to 60
print “Passed”
else
print “Failed”
• Code
if ( grade >= 60 )
printf ( "Passed“ );
else
printf ( "Failed“ );
If-else flow chart
The if-else Statement..
• Ternary conditional operator (?:)
– Three arguments (condition, value if true,
value if false)
• Code could be written:
( grade >= 60 ? “Passed” : “Failed” );
Nested if-else
• Nested if/else
– One inside another, test for multiple cases
– Once condition met, other statements skipped

if student’s grade is greater than or equal to 90


Print “A”
else
if student’s grade is greater than or equal to 80
Print “B”
else
if student’s grade is greater than or equal to 70
Print “C”
else
if student’s grade is greater than or equal to 60
Print “D”
else
Print “F”
Nested if-else..
/* A quick demo of nested if-else */
main( )
{
int i ;
printf ( "Enter either 1 or 2 " ) ;
scanf ( "%d", &i ) ;
if ( i == 1 )
printf ( "You would go to heaven !" ) ;
else
{
if ( i == 2 )
printf ( "Hell was created with you in mind" ) ;
else
printf ( "How about mother earth !" ) ;
}
}
Logical Operators
• && (logical AND)
– true if both conditions are true
if ( gender == 1 && age >= 65 )
++seniorFemales;
• || (logical OR)
– true if either of condition is true
if ( semesterAverage >= 90 || finalExam >= 90 )
printf( "Student grade is A“);
• ! (logical NOT, logical negation)
– Returns true when its condition is false, &
vice versa
if ( !( grade == sentinelValue ) )
printf( "The next grade is “, grade);
Logical Operators..

1
Sample Program
• To calculate the division
• Input: marks of 2 different subjects
• Rules
– Percentage above or equal to 60 - First division
– Percentage between 50 and 59 - Second division
– Percentage between 40 and 49 - Third division
– Percentage less than 40 – Fail
• Solution
– Nested if-else
– Logical operators
Sample Program..
if ( per >= 60 ) if ( per >= 60 )
printf ( "First division ") ; printf ( "First division" ) ;
else if ( ( per >= 50 ) && ( per < 60 ) )
{ printf ( "Second division" ) ;
if ( per >= 50 ) if ( ( per >= 40 ) && ( per < 50 ) )
printf ( "Second division" ) ; printf ( "Third division" ) ;
else if ( per < 40 )
{ printf ( "Fail" ) ;
if ( per >= 40 )
printf ( "Third division" ) ;
else
printf ( "Fail" ) ;
}
}
Else-if Statement
if ( per >= 60 )
printf ( "First division" ) ;
else if ( per >= 50 )
printf ( "Second division" ) ;
else if ( per >= 40 )
printf ( "Third division" ) ;
else
printf ( "fail" ) ;
Using Logical Operators
• A company insures its drivers in the
following cases:
– If the driver is married
– If the driver is unmarried, male & above 30
years of age
– If the driver is unmarried, female & above 25
years of age
Using Logical Operators..
if ( ms == 'M' ) if ( ( ms == 'M') || ( ms == 'U' && sex == 'M' &&
printf ( "Driver is insured" ) ; age > 30 ) ||
else ( ms == 'U' && sex == 'F' && age > 25 ) )
{ printf ( "Driver is insured" ) ;
if ( sex == 'M' ) else
{ printf ( "Driver is not insured" ) ;
if ( age > 30 )
printf ( "Driver is insured" ) ;
else
printf ( "Driver is not insured" ) ;
}
else
{
if ( age > 25 )
printf ( "Driver is insured" ) ;
else
printf ( "Driver is not insured" ) ;
}
}
Salary Calculation Example
Salary Calculation Example..
char g ;
int yos, qual, sal ;
printf ( "Enter Gender, Years of Service and Qualifications ( 0 = G, 1 = PG ):" ) ;
scanf ( "%c%d%d", &g, &yos, &qual ) ;
if ( g == 'm' && yos >= 10 && qual == 1 )
sal = 15000 ;
else if ( ( g == 'm' && yos >= 10 && qual == 0 ) ||
( g == 'm' && yos < 10 && qual == 1 ) )
sal = 10000 ;
else if ( g == 'm' && yos < 10 && qual == 0 )
sal = 7000 ;
else if ( g == 'f' && yos >= 10 && qual == 1 )
sal = 12000 ;
else if ( g == 'f' && yos >= 10 && qual == 0 )
sal = 9000 ;
else if ( g == 'f' && yos < 10 && qual == 1 )
sal = 10000 ;
else if ( g == 'f' && yos < 10 && qual == 0 )
sal = 6000 ;
Revised Hierarchy
Confusion
• Between assignment /equality operator
if ( i = 5 )
printf ( "You entered 5" ) ;
else
printf ( "You entered something other than 5" );
• Null statement
if ( i == 5 ) ;
printf ( "You entered 5" )
Conditional Operator (?:)
• conditional operator (?:), closely related
to the if...else statement
• ternary operatorit takes three operands.
• The syntax of the conditional operator is
• [Relational expression] ? [statement if true]
:
• [statement if false]
• ( grade >= 60 ? "Passed" : "Failed" );
• Similar to:

If (grade >= 60 )
Printf(“Passed”);
Else
Printf(“Failed”);
• int main(void)
• {
• int num;
• Printf( "Enter a whole number: “);
• Scanf(“%d” &num);
• if ( num % 2 == 0 )
• printf( "The number is even“);
• else
• printf("The number is odd“);
• return 0;}
• int main(void)
• {
• int num;
• printf( "Enter a whole number: “);
• scanf(“%d” &num);
• printf("The number is " num % 2 == 0 ?
"even" : "odd“);
• return 0;
• }
Switch statement
• selection-statement:
– switch ( expression ) statement
• labeled-statement:
– case constant-expression : statement
– default : statement
• switch ( expression )
• {
• declarations
• .
• .
• .
• case constant-expression :
• statements executed if the expression equals the
• value of this constant-expression
• .
• .
• .
• break;
• default :
• statements executed if expression does not equal
• any case constant-expression
• }
Switch statement
• int main(void)
• { char grade;
• Printf( "Enter your grade: “);
• scanf(“%c” &grade);
• switch (grade)
• { case 'A':
• Printf( "Your average must be between 90 - 100“);
• break;
• case 'B':
• Printf( "Your average must be between 80 - 90“);
• break;
• default:
• Printf( "Your average must be below 60" );
• } return 0;}
• The default statement is executed if no case constant-
expression is equal to the value of switch ( expression ).
If the default statement is omitted, and no case match is
found, none of the statements in the switch body are
executed. There can be at most one default statement.
Thedefault statement need not come at the end; it can
appear anywhere in the body of the switchstatement.
A case or default label can only appear inside
a switch statement.

You might also like