Computer Programming & Problem Solving (CPPS-1) : Chapter # 4
Computer Programming & Problem Solving (CPPS-1) : Chapter # 4
Decisions
Chapter # 4
If statement
If–else statement
Switch statement
Conditional Operator
Computer Languages too must be able to perform different sets of actions
depending on the circumstances. C Language has three major decision making
structures: i.e
1. If Statement
2. If-Else Statement
3. Switch Statement
If Statement
Like most Languages, C Language uses the keyword if to introduce the basic
decision making statement.
Program
{
char ch;
clrscr( );
ch = getche( );
if ( ch = = ‘ y ‘ )
printf ( “ \n You typed y. “ );
Output:
y
You typed y.
If you type ‘ y ‘, the program will print “ You typed y “. If you type some other
character, such as ‘ n ‘ the program does not do anything.
Structure of IF Statement
Enter
Test
Body of the
IF Statement
Exit
Program that’s counts not only the number of characters, but also the number of
words as well.
Program
{
int charcnt = 0 ;
int wordcnt = 0 ;
char ch ;
clrscr( );
while ( ( ch = getche ( ) ) ! = ‘ \r ’ )
{
charcnt ++ ;
if ( ch = = ‘ ‘ )
wordcnt ++ ;
}
printf ( “ \n “ );
printf ( “ \n Character count is %d “, charcnt ) ;
printf ( “ \n Word count is %d “, wordcnt + 1 ) ;
Output:
Character count is 15
Word count is 3
The body of the IF statement may consist of either a single statement terminated
by a semi colon or by a number of statements enclosed in the braces. i.e
delimiters.
Program
{
char ch;
clrscr( );
ch = getche( );
if ( ch = = ‘ y ‘ )
{
printf ( “ \n You typed y. “ );
printf ( “ \n Not some other letter. “ );
}
}
Output:
y
You typed y.
Not some other letter.
Nested IF Statements
Nested IF statements means that one if statement is a part of the body of another
if statement.
Program
{
clrscr( );
if ( getche ( ) = = ‘ n ’ )
if ( getche ( ) = = ‘ o ’ )
Output:
no
You typed no.
In the above program, the inner if statement will not be reached unless the outer
one is true, and the printf ( ) statement will not be executed unless both if
statements are true.
Enter
Test 1
Test 2
Body of the
IF Statement
Exit
Can we execute a group of statements if and only if the test expression is not
true?
Program
{
char ch;
clrscr( );
ch = getche( );
if ( ch = = ‘ y ‘ )
printf ( “ \n You typed y. “ );
else
printf ( “ \n You did not typed y. “ );
Output:
a
You did not typed y.
y
You typed y.
Enter
Test
Exit
Program
{
int temp ; clrscr ( ) ;
if ( temp < 80 )
if ( temp > 60 )
printf ( “ Nice Day.“ ) ;
else
printf ( “ Sure is Hot! “ ) ;
}
Output:
Suppose temp is 32. What will be printed when its program is executed ? Would
you guess nothing at all ? That’s seems to be reasonable: because the first if
condition ( temp < 80 ) will be true, so the second if condition ( temp > 60 ) will be
evaluated. It will be false so it looks as if the program will exit from the entire
nested if-else construct.
The problem is that the else is actually associated with the if immediately
preceding it, not the first if, as the indentation would lead you to believe. The rule
is that an else is associated with the last if that does not have it own else.
Program
{
int temp ;
clrscr ( ) ;
if ( temp < 80 )
if ( temp > 60 )
printf ( “ Nice Day.“ ) ;
else
printf ( “ Sure is Chilly! “ ) ;
Output:
Here the inner else is paired with the inner if, and the outer else is paired with the
outer if. The indentation in this case is not misleading.
“ An else is associated with the last if that does not have its own else.”
Surrounding the if with braces makes it invisible to the else, which then matches
up with the earliest non-braced if statement.
For Example:
if ( temp < 80 )
{
if ( temp > 60 )
printf ( “ Nice Day.“ ) ;
}
else
printf ( “ Sure is Hot! “ ) ;
Logical Operators
Logical Operators are powerful way to condense and clarify complicated if-else
structured.
Program
{
int x ; clrscr ( ) ;
if ( x = = 5 | | x = = 6 )
Output:
2
Sorry wrong number.
5
Yes You have typed the right number.
The logical operator OR i.e ( || ) means if either the expression on the right side of the
operator, or the expression on the left is true, then the entire expression is valid or true.
There are 3 types of Logical Operators or sometimes called the Boolean Operators in C
Language.
1. | | Logical OR
2. & & Logical AND
3. ! Logical NOT
Program
{
char x , y ; clrscr( );
if ( x = = ‘ n ’ && y = = ‘ o ’ )
Output:
no
You typed no.
Program
{
int charcnt, digitcnt ; clrscr( );
Output:
Character count is 25
Digit count is 2
The key to this program is the logical AND operator (&&). This operator says: if
both the expression on the left ( ch > 47 ) and ( ch < 58 ) are true, then the entire
expression ( ch > 47 && ch 58 ) is true. This will only be true if ch is between 48
and 57; these are the ASCII codes for the digits from 0 to 9.
Note:
The Logical Operator ( ! ) i.e NOT, is represented the exclamation print ( ! ) and
sometimes called the “ Bang Operator “. This operator reverse the logical values
of the expression it operates on, it makes a true expression false and a false
expression true.
The NOT Operator is a unary operator on, that is it takes only one operand.
This means that “ not x less than 5 “. In other words, if x is less than 5, the
expression will be false, since ( x < 5 ) is true.
Program
/* 4 – Function Calculator */
{
float num1 = 1.0 , num2 = 1.0 ;
ch op ;
clrscr ( ) ;
{
printf ( “ Type number , operator , number : \n “ ) ;
scanf ( “ %f %c %f “, &num1, &op, &num2 ) ;
if ( op = = ‘ + ’ )
printf ( “ = %f “, num1 + num2 ) ;
else
if ( op = = ‘ - ’ )
printf ( “ = %f “, num1 - num2 ) ;
else
if ( op = = ‘ * ’ )
printf ( “ = %f “, num1 * num2 ) ;
else
if ( op = = ‘ / ’ )
printf ( “ = %f “, num1 / num2 ) ;
printf ( “ \n \n “ );
}
getch ( );
}
Output:
Entering zero values for both numbers terminates the program. This is handled
by the logical expression in the while statement.
if ( op = = ‘ + ’ )
printf ( “ = %f “, num1 + num2 ) ;
else if ( op = = ‘ - ’ )
printf ( “ = %f “, num1 - num2 ) ;
else if ( op = = ‘ * ’ )
printf ( “ = %f “, num1 * num2 ) ;
Structurally, the statement starts out with the keyword switch, followed by the
parenthesis containing an integer or character variable which we will call the
switch variable.
switch ( op )
case ‘ a ‘ :
statement ;
break ;
case ‘ b ‘ :
statement ;
break ;
default :
statement ;
}
Break command is often useful when a condition suddenly occurs that makes
it necessary to leave a loop before the loop expression becomes false.
Default command is used if the switch variable does not match any of the
case constants.
/* 4 – Function Calculator */
{
float num1 = 1.0 , num2 = 1.0 ;
ch op ;
clrscr ( ) ;
{
printf ( “ Type number , operator , number : \n “ ) ;
scanf ( “ %f %c %f “, &num1, &op, &num2 ) ;
switch ( op )
case ‘ + ‘ :
printf ( “ = %f “, num1 + num2 ) ;
break ;
case ‘ - ‘ :
printf ( “ = %f “, num1 - num2 ) ;
break ;
case ‘ * ‘ :
printf ( “ = %f “, num1 * num2 ) ;
break ;
case ‘ / ‘ :
printf ( “ = %f “, num1 / num2 ) ;
break ;
default :
printf ( “ Unknown Operator “ ) ;
}
printf ( “ \n \n “ );
}
getch ( );
}
Enter
Break
Case 1 Body 1
Break
Case 2 Body 2
Exit Body 4
3
Break
CE-102: CPPS-1 ( Chapter # 4 ) Batch 2001, Section: A, B, C, D
The conditional operator consists of both the question mark and the colon.
Condition is a logical expression that evaluates to either true or false, while
expression1 and expression2 are either values or expression that evaluates to
values.
Note that the entire conditional expression – the three expressions and two
operators takes on a value and can therefore be used in a assignment statement.
In short, Conditional Operator returns one or the other of the two values,
depending on whether a condition is true or false.