0% found this document useful (0 votes)
11 views

CSC 183 Chap-6

Chapter 6 of CSC-183 covers decision making and branching in C programming, detailing various control statements such as if, if-else, nested if-else, else-if ladder, switch, and goto statements. It explains the syntax and provides examples for each type of statement, emphasizing their usage in altering the flow of program execution based on conditions. Additionally, it includes programming exercises to reinforce the concepts discussed.

Uploaded by

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

CSC 183 Chap-6

Chapter 6 of CSC-183 covers decision making and branching in C programming, detailing various control statements such as if, if-else, nested if-else, else-if ladder, switch, and goto statements. It explains the syntax and provides examples for each type of statement, emphasizing their usage in altering the flow of program execution based on conditions. Additionally, it includes programming exercises to reinforce the concepts discussed.

Uploaded by

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

CSC – 183

Programming C
Chapter - 6

Decision Making and Branching

31 January 2023 CSC-183 1


Today’s Outline:
• Introduction
• Decision Making statements in C
• Simple if statement
• if_else statement
• else_if ladder
• nested if_else statement
• switch statement
• goto statement

31 January 2023 CSC-183 2


Decision Making and Branching

31 January 2023 CSC-183 3


Introduction :
• C statements are normally executed sequentially in order
they appear.
• In practice, depending on situation may have to change the
order of execution of statements based on certain conditions,
or repeated a group of statements until met certain
conditions.

• In this kinds of situation need to take decision, and according


to the decision the flow or execution order of statements are
changed.

• That’s why it is called Decision Making and Branching.


31 January 2023 CSC-183 4
Decision Making statements in C :

1) if statement

2) switch statement

3) Conditional operator statement (?:)

4) goto statement

31 January 2023 CSC-183 5


Simple if Statement :
• Format:

if ( test condition )
statements;

• Example:

scanf( “ %f “ , &marks );
if( marks >= 80 )
printf( "Yes, the student get A+“ );

31 January 2023 CSC-183 6


Simple if Statement :
• Format:

if ( test condition )
statements;

• Example:

if( category == SPORTS )


{
marks = marks + bonus_marks;
}
printf( " Marks = %f " , marks );
31 January 2023 CSC-183 7
Simple if Statement : (flowchart)

Entry

Test True
Expression
?

Statement-block
False

Statement - x

Next Statement

31 January 2023 CSC-183 8


Simple if Statement : (Program)
• If category is SPORTS, then add bonus_mark with marks.

• Count number of boys weight is less than 50 kg and height is


greater than 170 cm.

• Check divide by 0 ‘Zero’ condition in division operation.


• Write C program to print the square of a number if it is less
than 10.
• Check a number is greater, smaller or equal to another
number.

31 January 2023 CSC-183 9


if_else Statement :
• Format:
if ( test condition )
statements;
else
statements;

• Example:
if( num % 2 == 0 )
printf( “Even number’’ );
else
printf( “Odd number” );

31 January 2023 CSC-183 10


if_else Statement : (flowchart)

Entry

True Test False


Expression
?
True-block False-block
statement statement

Statement - x

31 January 2023 CSC-183 11


If-else Statement : (Program)
• Find out larger number between two numbers.
• Check a number even or odd.
• Check a number is Numbers are divisible by any other
numbers.
• Check two values are equal or not.
• Check a marks is valid or invalid.
• Check a candidate is eligible or not. (age and gpa)

31 January 2023 CSC-183 12


nested if_else Statement
• When a series of decisions are involved, we may have to use
more than one if…else statement in nested form.

• Nested means one if statement inside of another if_else


statement.

• So, when one if statement appears inside of another if


statement for decision making is called nested if…else

• In nested if…else, nested if (inside one)may appear inside if


or else statement.

31 January 2023 CSC-183 13


nested if_else Statement
Format: Example:

if(a>b)
if ( condition ) {
if ( condition ) if(a>c)
printf("a is the largest:%d",a);
statement1; else
else printf("c is the largest:%d",c) ;
statement2; }
else
else {
statement3 ; if(c>b)
printf("c is the largest:%d",c);
else
printf("b is the largest:%d",b) ;
}
31 January 2023 CSC-183 14
nested if_else Statement (Example)
int main()
{
float salary, bonus;
if(gender == FEMALE)
{
if(salary > 10000)
bonus = salary * 0.5;
else
bonus = salary *0.2;
}
else
{
bonus = salary * 0.3;
}
salary = salary + bonus;
return 0;
}

31 January 2023 CSC-183 15


nested if_else Statement

31 January 2023 CSC-183 16


else_if ladder Statement
• This is another way of putting if’s together what multiple
decision are involved.
• A multipath decision is a chain of if’s in which the statement
associated with each else is an if.
• If any of the conditional expression evaluates to true, then it
will execute the corresponding code block and exits whole if-
else ladder.
• The conditions are evaluated from the top to downwards.
• If all conditions are false, then the final else containing
statement(s) will be executed as a default.

31 January 2023 CSC-183 17


else_if ladder Statement
Format: Example:
if(condition 1)
statements; if( marks > 79 )
printf( " Honours " );
else if(condition 2)
else if( marks > 59 )
statements; printf( " First Division " );
else if(condition 3) else if( marks > 49 )
statements; printf( " Second Division " );
................ else if( marks > 39 )
else if(condition n) printf( " Third Division " );
else
statements;
printf(" Fail ");
else
default statement;
31 January 2023 CSC-183 18
else_if ladder Statement

31 January 2023 CSC-183 19


Program using if-else ladder
• Write a program to determine the period of life from age.
– If age above 70: Your are too old.
– Age 51-70 : You are old.
– Age 36-50: You are a mid age person.
– Age 19-35: Your are young person.
– Age 12-18: You are a teenage.
– Age 0-11: You are a child.

• Write a program to check a character is vowel or not.


• Exercise program: 5.5 (E. Balagurusamy)

31 January 2023 CSC-183 20


switch Statement
• When in a program need to select so many alternatives option,
we can use an if statement to control the selection.

• But if number of alternatives increases, then complexity of a


program also increases and it difficult to understand the
program.

• C has a built-in multiway decision statement known as a switch.

• The switch statement tests the value of a given variable


(expression) against a list of case values and when a match is
found, a block of statements associated with that case is
executed.
31 January 2023 CSC-183 21
switch Statement Example:

Format: index=marks/10;
switch (index)
{
switch ( expression ) case 10:
case 9:
{
case 8:
case value-1: printf(" Honours ");
statements; break;
case 7:
break; case 6:
case value-2: printf(“ First Division ");
statements; break;
case 5:
break; printf(“ Second Division ");
............... break;
case 4:
...............
printf(“ Third Division ");
default: break;
statements; default:
printf( “ Fail ");
break; break;
} }
31 January 2023 CSC-183 22
switch Statement (cont.)
• The expression is an integer expression or characters.
• Each case value should be unique within a switch statement.
• case labels end with a colon (:).
• When switch is executed, the value of the expression is
compared against the value value-1, value-2 …. If a case
matched then the block of statements are executed.
• value in case should be constant, error if variable use.
• The break statement at the end of each block indicates end of
particular case and exit from switch statement.
• The switch also permitted to use nested switch statement.
• The default is an optional case.
– If present: executed if other cases value are not matched.
– If not present: no action if all cases are failed.
31 January 2023 CSC-183 23
switch Statement (flowchart)

31 January 2023 CSC-183 24


The ?: Operator (Conditional Operator)
• The C has an unusual operator, useful for making two-way
decisions. This operator is a combination of ? and :
• It has three parts and takes three operands.
• Format:
conditional_expression ? expression-1 : expression-2
max = (a>b?a:b)
• The conditional expression is evaluated first.
• If the result is nonzero, expression-1 is evaluated.
• Otherwise expression-2 is evaluated.
• Condition ? True output /1st expression : false output/2nd expr

31 January 2023 CSC-183 25


• Condition ? True output /1st expression : false output/2nd expr
• If(condition){true output/1st expression}else{false output/2nd
expression}
• If(a>b){print a}else {print b}
• a>b ? a : b

31 January 2023 CSC-183 26


goto Statement
• To jump one point to another point in a program goto statement is
used.
• The goto statement is an unconditional branching statement.
• Although maximum programmers usually try to avoid goto.
• The goto requires a label in order to identify the place where the
program execution is transferred.
• Label can be anywhere in the program, follow by a colon.
• The goto statement break the normal sequential execution of the
program.
• Forward Jump and Backward Jump
• Use of goto statement is highly discouraged in any programming
language because it makes difficult to trace the control flow of a
program, making the program hard to understand and hard to
modify.
31 January 2023 CSC-183 27
goto Statement
Format 1: Example:
goto add;
......... main()
......... {
add: double x,y;
a = b + c; read:
scanf("%lf",&x);
Format 2: if(x<0)
label: goto read;
statements; y=sqrt(x);
......... printf("%lf",y);
......... goto read;
goto label; } CSC-183
31 January 2023 28
goto Statement

Forward Jump Backward Jump

31 January 2023 CSC-183 29


Programming Exercise:
• Exercise: 5.1, 5.2, 5.4, 5.5, 5.10, 5.11, 5.12
• Write a program to determine square of a number which is
divisible by 6 but not divisible by 4.

31 January 2023 CSC-183 30


31 January 2023 CSC-183 31
Thank You All

31 January 2023 CSC-183 32

You might also like