KEMBAR78
Conditional Statements in C.pptx
Conditional
Statements in C
Dr. Surbhi Gupta
Objectives
In this lecture you will:
 Learn about control structures
 Discover how to use the selection control structures
 if,
 if...else,
 and switch in a program
2
Control Structures
 A computer can proceed:
 In sequence
 Selectively (branch) - making a choice
 Repetitively (iteratively) - looping
 Some statements are executed only if certain conditions are
met
 A condition is represented by a logical (Boolean)
expression that can be true or false
 A condition is met if it evaluates to true
3
One-Way (if) Selection
 The syntax of one-way selection is:
if (expression)
statement
 Statement is executed if the value of the expression is
true
 Statement is bypassed if the value is false; program
goes to the next statement
5
Example 3.1: While purchasing certain
items, a discount of 10% is
offered if the quantity purchased is
more than 1000. If quantity and
price per item are input through the
keyboard, write a program to
calculate the total expenses
Example
/* Calculation of total expenses */
# include <stdio.h>
int 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. %fn", tot ) ;
return 0 ;
}
Two-Way (if…else) Selection
 Two-way selection takes the form:
if (expression)
statement1
else
statement2
 If expression is true, statement1 is executed otherwise
statement2 is executed
 statement1 and statement2 are any C++ statements
 else is a reserved word
11
Example 3.3: In a company an
employee is paid as under:
If his basic salary is less than Rs. 1500,
then HRA = 10% of basic salary
and DA = 90% of basic salary. If his
salary is either equal to or above Rs.
1500, then HRA = Rs. 500 and DA =
98% of basic salary. If the employee's
salary is input through the keyboard
write a program to find his gross
salary.
/* Calculation of gross salary */
# include <stdio.h>
int main( )
{
float bs, gs, da, hra ;
printf ( "Enter basic salary " ) ;
scanf ( "%f", &bs ) ;
if ( bs < 1500 )
{
hra = bs * 10 / 100 ;
da = bs * 90 / 100 ;
}
else
{
hra = 500 ;
da = bs * 98 / 100 ;
}
gs = bs + hra + da ;
printf ( "gross salary = Rs. %fn", gs ) ;
return 0 ;
}
Conditional Operator (?:)
 Conditional operator (?:) takes three arguments
(ternary)
 Syntax for using the conditional operator:
expression1 ? expression2 : expression3
 If expression1 is true, the result of the conditional
expression is expression2. Otherwise, the result is
expression3
16
Example
int x, y ;
scanf ( "%d", &x ) ;
y = ( x > 5 ? 3 : 4 ) ;
This statement will store 3 in y if x is greater than 5, otherwise it will
store 4 in y.
The equivalent if-else form would be,
if ( x > 5 )
y = 3 ;
else
y = 4 ;
Nested if
 Nesting: one control statement in another
 An else is associated with the most recent if that has
not been paired with an else
18
Example
The marks obtained by a student in 5 different subjects are
input through the keyboard. The student gets a division as per the
following 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
Write a program to calculate the division obtained by the student.
Switch Structures
 switch structure: alternate to if-else
 switch expression is evaluated first
 Value of the expression determines which
corresponding action is taken
 Expression is sometimes called the selector
22
Switch Structures (continued)
 Expression value can be only integral
 Its value determines which statement is selected for
execution
 A particular case value should appear only once
23
Switch Structures (continued)
 One or more statements may follow a case label
 Braces are not needed to turn multiple statements into
a single compound statement
 The break statement may or may not appear after each
statement
 switch, case, break, and default are reserved words
25
26
Summary
 Two selection structures: one-way selection and two-
way selection
 The expression in an if or if...else structure is usually a
logical expression
 Switch structure handles multiway selection
29

Conditional Statements in C.pptx

  • 1.
  • 2.
    Objectives In this lectureyou will:  Learn about control structures  Discover how to use the selection control structures  if,  if...else,  and switch in a program 2
  • 3.
    Control Structures  Acomputer can proceed:  In sequence  Selectively (branch) - making a choice  Repetitively (iteratively) - looping  Some statements are executed only if certain conditions are met  A condition is represented by a logical (Boolean) expression that can be true or false  A condition is met if it evaluates to true 3
  • 5.
    One-Way (if) Selection The syntax of one-way selection is: if (expression) statement  Statement is executed if the value of the expression is true  Statement is bypassed if the value is false; program goes to the next statement 5
  • 9.
    Example 3.1: Whilepurchasing certain items, a discount of 10% is offered if the quantity purchased is more than 1000. If quantity and price per item are input through the keyboard, write a program to calculate the total expenses
  • 10.
    Example /* Calculation oftotal expenses */ # include <stdio.h> int 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. %fn", tot ) ; return 0 ; }
  • 11.
    Two-Way (if…else) Selection Two-way selection takes the form: if (expression) statement1 else statement2  If expression is true, statement1 is executed otherwise statement2 is executed  statement1 and statement2 are any C++ statements  else is a reserved word 11
  • 14.
    Example 3.3: Ina company an employee is paid as under: If his basic salary is less than Rs. 1500, then HRA = 10% of basic salary and DA = 90% of basic salary. If his salary is either equal to or above Rs. 1500, then HRA = Rs. 500 and DA = 98% of basic salary. If the employee's salary is input through the keyboard write a program to find his gross salary.
  • 15.
    /* Calculation ofgross salary */ # include <stdio.h> int main( ) { float bs, gs, da, hra ; printf ( "Enter basic salary " ) ; scanf ( "%f", &bs ) ; if ( bs < 1500 ) { hra = bs * 10 / 100 ; da = bs * 90 / 100 ; } else { hra = 500 ; da = bs * 98 / 100 ; } gs = bs + hra + da ; printf ( "gross salary = Rs. %fn", gs ) ; return 0 ; }
  • 16.
    Conditional Operator (?:) Conditional operator (?:) takes three arguments (ternary)  Syntax for using the conditional operator: expression1 ? expression2 : expression3  If expression1 is true, the result of the conditional expression is expression2. Otherwise, the result is expression3 16
  • 17.
    Example int x, y; scanf ( "%d", &x ) ; y = ( x > 5 ? 3 : 4 ) ; This statement will store 3 in y if x is greater than 5, otherwise it will store 4 in y. The equivalent if-else form would be, if ( x > 5 ) y = 3 ; else y = 4 ;
  • 18.
    Nested if  Nesting:one control statement in another  An else is associated with the most recent if that has not been paired with an else 18
  • 20.
    Example The marks obtainedby a student in 5 different subjects are input through the keyboard. The student gets a division as per the following 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 Write a program to calculate the division obtained by the student.
  • 22.
    Switch Structures  switchstructure: alternate to if-else  switch expression is evaluated first  Value of the expression determines which corresponding action is taken  Expression is sometimes called the selector 22
  • 23.
    Switch Structures (continued) Expression value can be only integral  Its value determines which statement is selected for execution  A particular case value should appear only once 23
  • 25.
    Switch Structures (continued) One or more statements may follow a case label  Braces are not needed to turn multiple statements into a single compound statement  The break statement may or may not appear after each statement  switch, case, break, and default are reserved words 25
  • 26.
  • 29.
    Summary  Two selectionstructures: one-way selection and two- way selection  The expression in an if or if...else structure is usually a logical expression  Switch structure handles multiway selection 29