0% found this document useful (0 votes)
104 views18 pages

Computer Programming & Problem Solving (CPPS-1) : Chapter # 4

The document discusses decision making structures in C language, including if statements, if-else statements, and switch statements. It provides examples of each type of statement and how they work. Key points covered include: using if to check a condition and execute code if true; if-else allowing execution of alternate code if false; nested if statements where one is contained within another; and logical operators like || that allow combining multiple conditions.

Uploaded by

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

Computer Programming & Problem Solving (CPPS-1) : Chapter # 4

The document discusses decision making structures in C language, including if statements, if-else statements, and switch statements. It provides examples of each type of statement and how they work. Key points covered include: using if to check a condition and execute code if true; if-else allowing execution of alternate code if false; nested if statements where one is contained within another; and logical operators like || that allow combining multiple conditions.

Uploaded by

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

CE-102: CPPS-1 ( Chapter # 4 ) Batch 2001, Section: A, B, C, D

Computer Programming & Problem Solving ( CPPS-1 )

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

void main (void)

{
char ch;
clrscr( );

ch = getche( );

if ( ch = = ‘ y ‘ )
printf ( “ \n You typed y. “ );

Output:

y
You typed y.

Complied By: Muzammil Ahmad Khan Page 1 of 18


CE-102: CPPS-1 ( Chapter # 4 ) Batch 2001, Section: A, B, C, D

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

The if keyword is followed by the parenthesis, which contain a conditional


expression using a relational operator. Following this, there is the body of the
statement, consisting of either a single statement terminated by a semicolon or
multiple statements enclosed by braces.

The if statement is similar to the while statement in operation as well as format.


In both cases the statements making up the body of the statement will not be
executed at all, if the condition is false. However, in the while statement, if the
condition is true, the statement or statements in the body of the loop will be
executed over and over until the condition becomes false; whereas in the if
statement they will be executed only once.

Operation of the If Statement


Let us follow the operation of the If Statement, as depicted in the figure given below.

Enter

Test

Body of the

IF Statement

Exit

Complied By: Muzammil Ahmad Khan Page 2 of 18


CE-102: CPPS-1 ( Chapter # 4 ) Batch 2001, Section: A, B, C, D

A Word Counting Program

Program that’s counts not only the number of characters, but also the number of
words as well.

Program

void main (void)

{
int charcnt = 0 ;
int wordcnt = 0 ;
char ch ;
clrscr( );

printf ( “ Type in a phrase : “ );

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:

Type in a phrase : I Love Pakistan

Character count is 15
Word count is 3

The statement wordcnt ++ ; causes the variable wordcnt to be incremented every


time a space is detected in the input stream. There is always be one word than
there are spaces between them, so we add 1 to the variable wordcnt before
printing it out.

Complied By: Muzammil Ahmad Khan Page 3 of 18


CE-102: CPPS-1 ( Chapter # 4 ) Batch 2001, Section: A, B, C, D

Multiple Statements in IF Statement

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

void main (void)

{
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.

Complied By: Muzammil Ahmad Khan Page 4 of 18


CE-102: CPPS-1 ( Chapter # 4 ) Batch 2001, Section: A, B, C, D

Nested IF Statements

Nested IF statements means that one if statement is a part of the body of another
if statement.

Program

void main (void)

{
clrscr( );

if ( getche ( ) = = ‘ n ’ )

if ( getche ( ) = = ‘ o ’ )

printf ( “ \n You typed no. “ )

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.

Complied By: Muzammil Ahmad Khan Page 5 of 18


CE-102: CPPS-1 ( Chapter # 4 ) Batch 2001, Section: A, B, C, D

Operation of the Nested - If Statement


Let us follow the operation of the Nested - If Statement, as depicted in the figure given
below.

Enter

Test 1

Test 2

Body of the

IF Statement

Exit

Complied By: Muzammil Ahmad Khan Page 6 of 18


CE-102: CPPS-1 ( Chapter # 4 ) Batch 2001, Section: A, B, C, D

The IF – Else Statement

The if statement by itself will execute a single statement, or a group of


statements, when the test expression is true. It does nothing when it is false.

Can we execute a group of statements if and only if the test expression is not
true?

Of Course, this is the purpose of the if-else statement.

Program

void main (void)

{
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.

Complied By: Muzammil Ahmad Khan Page 7 of 18


CE-102: CPPS-1 ( Chapter # 4 ) Batch 2001, Section: A, B, C, D

Operation of the If - Else Statement


Let us follow the operation of the If – Else Statement, as depicted in the figure given below.

Enter

Test

Body of the Body of the

IF Statement Else Statement

Exit

Which IF gets the else ?

Program

void main (void)

{
int temp ; clrscr ( ) ;

printf ( “ Type today’s temperature. “ ) ; scanf ( “ %d “, &temp ) ;

if ( temp < 80 )
if ( temp > 60 )
printf ( “ Nice Day.“ ) ;
else
printf ( “ Sure is Hot! “ ) ;

}
Output:

Complied By: Muzammil Ahmad Khan Page 8 of 18


CE-102: CPPS-1 ( Chapter # 4 ) Batch 2001, Section: A, B, C, D

Type today’s temperature. 32

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.

You have been misleading by altering the INDENTATION.

Type today’s temperature. 32


Sure is Hot!

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

void main (void)

{
int temp ;
clrscr ( ) ;

printf ( “ Type today’s temperature. “ ) ;


scanf ( “ %d “, &temp ) ;

if ( temp < 80 )

if ( temp > 60 )
printf ( “ Nice Day.“ ) ;
else
printf ( “ Sure is Chilly! “ ) ;

Output:

Type today’s temperature. 32

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.”

Complied By: Muzammil Ahmad Khan Page 9 of 18


CE-102: CPPS-1 ( Chapter # 4 ) Batch 2001, Section: A, B, C, D

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

void main (void)

{
int x ; clrscr ( ) ;

printf ( “ Press the number 5 or 6 : “ ) ;


scanf ( “ %d “, &x ) ;

if ( x = = 5 | | x = = 6 )

printf ( “Yes You have typed the right number. “ ) ;


else
printf ( “ Sorry wrong number. “ ) ;

Output:

2
Sorry wrong number.
5
Yes You have typed the right number.

Complied By: Muzammil Ahmad Khan Page 10 of 18


CE-102: CPPS-1 ( Chapter # 4 ) Batch 2001, Section: A, B, C, D

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

void main (void)

{
char x , y ; clrscr( );

scanf ( “ %c “, &x ); scanf ( “ %c “, &y );

if ( x = = ‘ n ’ && y = = ‘ o ’ )

printf ( “ You typed no. “ )


}

Output:

no
You typed no.

Program

void main (void)

{
int charcnt, digitcnt ; clrscr( );

printf ( “ Type in a phrase : “ ) ;


while ( ( ch = getche ( ) ) ! = ‘ \r ‘ “ ) ;
{
charcnt + + ;
if ( ch > 47 && ch < 58 )
digitcnt + + ;
}

printf ( “ \n Character count is %d “ , charcnt ) ;


printf ( “ \n Digit count is %d “ , digitcnt ) ;

Complied By: Muzammil Ahmad Khan Page 11 of 18


CE-102: CPPS-1 ( Chapter # 4 ) Batch 2001, Section: A, B, C, D

Output:

Type in a phrase : Ali has 1 bike and 2 cars

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:

“ Logical Operator have a lower precedence than the Relational Operator. ”

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.

Example of the NOT operator applied to a relational expression: !(x<5)

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.

Complied By: Muzammil Ahmad Khan Page 12 of 18


CE-102: CPPS-1 ( Chapter # 4 ) Batch 2001, Section: A, B, C, D

The Else – If Statement Construct

Program

/* 4 – Function Calculator */

void main (void)

{
float num1 = 1.0 , num2 = 1.0 ;
ch op ;
clrscr ( ) ;

while ( ! ( num1 = = 0.0 && num2 = = 0.0 ) )

{
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:

Type number , operator , number :


2+3=5

Complied By: Muzammil Ahmad Khan Page 13 of 18


CE-102: CPPS-1 ( Chapter # 4 ) Batch 2001, Section: A, B, C, D

Entering zero values for both numbers terminates the program. This is handled
by the logical expression in the while statement.

Creation of a sort of imaginary construct called else if.

if ( op = = ‘ + ’ )
printf ( “ = %f “, num1 + num2 ) ;

else if ( op = = ‘ - ’ )
printf ( “ = %f “, num1 - num2 ) ;

else if ( op = = ‘ * ’ )
printf ( “ = %f “, num1 * num2 ) ;

The Switch Statement

The Switch statement provides an alternative to the else – if statement construct.


It is similar to the else – if statement, but has more flexibility and a clearer
format.

Structure of the Switch Statement

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 ;
}

Complied By: Muzammil Ahmad Khan Page 14 of 18


CE-102: CPPS-1 ( Chapter # 4 ) Batch 2001, Section: A, B, C, D

 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.

Complied By: Muzammil Ahmad Khan Page 15 of 18


CE-102: CPPS-1 ( Chapter # 4 ) Batch 2001, Section: A, B, C, D

Program using Switch Statement

/* 4 – Function Calculator */

void main (void)

{
float num1 = 1.0 , num2 = 1.0 ;
ch op ;
clrscr ( ) ;

while ( ! ( num1 = = 0.0 && num2 = = 0.0 ) )

{
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 ( );
}

Complied By: Muzammil Ahmad Khan Page 16 of 18


CE-102: CPPS-1 ( Chapter # 4 ) Batch 2001, Section: A, B, C, D

Operation of the Switch Statement


Let us follow the operation of the Switch Statement, as depicted in the figure given below.

Enter

Break

Case 1 Body 1

Break

Case 2 Body 2

The Conditional Operator

Complied By: Muzammil Ahmad Khan4


Case 3 Page 17 of 18

Exit Body 4
3
Break
CE-102: CPPS-1 ( Chapter # 4 ) Batch 2001, Section: A, B, C, D

In C Language conditional operator is also called the Decision-making operator.


It consists of two symbols used on three different expressions, and thus it has the
distinction of being the only ternary operator in C. ( Ternary operators works on
three variables, as opposed to the more common binary operators, such as ( + ),
which operates on two expressions. The conditional operator has the form:

Condition ? expression1 : expression2.

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.

The condition is evaluated. If it is true, then the entire conditional expression


takes on the value of expression1. If it is false, the conditional expression takes
on the value of expression2.

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.

Here is an example: max = ( num1 > num2 ) ? num1 : num2 ;

This statement is equivalent to the if-else statement:

If ( num1 > num2 )


max = num1;
else
max = num2;

In short, Conditional Operator returns one or the other of the two values,
depending on whether a condition is true or false.

Complied By: Muzammil Ahmad Khan Page 18 of 18

You might also like