Decision Making and Branching
Decision Making and Branching
Decision Making and Branching
Chapter 5
Decision Making and Branching
Programming in C Chapt.5 Decision Making and Branching
Introduction:
C program is a set of statements which are normally
executed sequentially in the order.
Simple If Statement
The simplest form of the control statement is the if statement.
Example:
The following program calculate the absolute value of the
integer inputted from keyboard.
# Sometime
include < stdio.hmore> than one statements must be executed if
main
test( )expression is true, a pair of braces must enclose them.
{
int num1,
num1,num2,
intif(test num2,num;
num;
expression)
printf(Type
printf (Typetwotwonumbers:);
numbers:);
{ (%d
scanf
scanf (%d%d,
%d, &num1, &num2);
if (num1 <Statement1;
num2)
{ num = num1, num1 = num2, num2 =num;
Statement2;
printf num
(%d,=%d\n,
num1;num1,num2);if (num1 < num2)
Statement3;
} num1 = num2; num = num1, num1 = num2, num2
...
num2 =num; =num;
} }
printf (%d, %d\n, num1,num2);
}
if(condition1&&condition2)
statement;
if(condition1)
If(condition2)
statement;
Example: Input
ten integers and calculate sum of all numbers
among range 0 to 100.
main()
main()
{
{
int i, num, sum;
printf(int i,Please
num, sum;
input ten integers );
printf(
for( i=0; Please
i<10; i++) input ten integers );
{ for( i=0; i<10; i++)
{scanf( %d , &num );
if( num %d
scanf( > 0 ,
&&&num
num );
< 100)
sum+=num;
if( num > 0)
}
printf( if(
%dnum < 100)
, sum );
} sum+=num;
}
printf( %d , sum );
}
03/13/17 Shandong Normal University 7
Programming in C Chapt.5 Decision Making and Branching
If (condition expression)
statement_block1; entry
else
statement_block2; false true
test expression?
statement-block2 statement-block1
next
next statement
statement
Example2:
Write a program to determines if a year is a leap year.
# include < stdio.h >
void main ( )
{
int year;
printf (Enter the year to be tested);
scanf (%d, &year);
false true
test exp2?
statement4
statement3
false true
test exp3?
statement2 statement1
next statement
if (num
scanf < 0) &num);
(%d,
printf (The number is negative);
else if (num > 0)
printf (The number is positive);
else
printf (The number is zero);
} 03/13/17 Shandong Normal University 16
Example
Develop a program for the following problem.
switch statement-
expression
1;
break;
expression = case value-
1
expression = case value-2 statement-2;
break;
expression = case value-n
statement-n;
break;
default:
next statement;
03/13/17 Shandong Normal University 22
Programming in C Chapt.5 Decision Making and Branching
case 0: printf("%d",i);break;
case 1: printf("%d",i);break;
case 2: printf("%d",i);break;
default: printf("%d",i);
}
}
0123
03/13/17 Shandong Normal University 27
main() 0000111223
{
int i;
for(i=0;i<=3;i++)
switch(i)
{
case 0: printf("%d",i);
case 1: printf("%d",i);
case 2: printf("%d",i);
default: printf("%d",i);
}
}
01233
03/13/17 Shandong Normal University 29
main()
{
int i;
for(i=0;i<=3;i++)
switch(i)
{
default: printf("%d",i);break;
case 0: printf("%d",i);break;
case 2: printf("%d",i);break;
case 1: printf("%d",i);break;
}
}
0123
03/13/17 Shandong Normal University 30
Programming in C Chapt.5 Decision Making and Branching
s = ( x < 0 ) ? -1 : x * x;
If x is less than zero then s = -1
If x is greater than or equal to zero then s = x * x
s = ( x < 0 ) ? -1 : x * x;
equal to
if ( x < 0 )
s = -1;
else
s = x * x;
03/13/17 Shandong Normal University 33
Programming in C Chapt.5 Decision Making and Branching
#include <stdio.h>
main()
{
int num1,num2,max ;
printf("please enter two numbers:");
scanf(%d%d, &num1, &num2 );
max=(num1>num2)?num1:num2 ;
printf(The maximum value is %d, max);
}
03/13/17 Shandong Normal University 34
Programming in C Chapt.5 Decision Making and Branching
max=(num1>num2)?num1:num2 ;
max=(max>num3)? max:num3 ;
printf(The maximum value is %d, max);
}
03/13/17 Shandong Normal University 35
Programming in C Chapt.5 Decision Making and Branching
Forward jump
goto label;
A label is a valid
goto label;
variable name
followed by a
label:
colon.
statement;
label:
Backward jump statement;
label:
statement;
goto label;
goto label;
03/13/17 Shandong Normal University 40
Programming in C Chapt.5 Decision Making and Branching
Example:
A program to calculate sum = 1+2+3+ + n using goto
statement
#include < stdio.h >
main ( )
{
int n, sum = 0, i = 0,
printf (Enter a number);
scanf (%d, &n);
loop: i ++;
sum + = i;
if (i < n)
goto loop;
printf (\n sum of %d natural numbers = %d, n, sum);
}
03/13/17 Shandong Normal University 42
Programming in C Chapt.5 Decision Making and Branching
END