Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 23

18CSS26 & ARTIFICIAL INTELLIGENCE

Topics Covered

▪ DECISION MAKING AND BRANCHING


• The Switch statement
• The ?: operator (Conditional Operator)
• The goto Statement

SKASC 1
18CSS26 & ARTIFICIAL INTELLIGENCE

Switch Statement

• Switch statement is a multi-way decision statement.


• The switch statement tests the value of a given variable
against a list of case values and when the match is
found a block of statements associated with that case is
executed.

• The expression is an integer expression or characters.

• value1, value2 … are constants or constant expressions

and are known as case labels.


SKASC 2
18CSS26 & ARTIFICIAL INTELLIGENCE

Switch Statement
• Each of these values should be unique within a switch
statement.
• Block1, block2 are statement list and may contain zero
or more statements.
• The break statement at the end of each block signal the
end of a particular case and causes an exit from the
switch statement, transferring the control to the
statement x following switch.

SKASC 3
18CSS26 & ARTIFICIAL INTELLIGENCE

Switch Statement
• The default is an optional case, when present it will be
executed if the value of the expression does not match
with any of the case values.
• When the switch is executed,
– The value of the expression is compared against the
values and
– If a case is found whose value matches with the value
of the expression,
– Then the block of statements that follows the case are
executed. SKASC 4
18CSS26 & ARTIFICIAL INTELLIGENCE

Switch Statement
Syntax:
switch (expression)
{
case value1 : block1;
break;
case value2: block2;
break;
…….
default:
default block;
break;
}

SKASC 5
18CSS26 & ARTIFICIAL INTELLIGENCE

Switch Statement

SKASC 6
18CSS26 & ARTIFICIAL INTELLIGENCE

Rules for Switch Statement

• The Switch expression must be an integral type.


• Case labels must be constants or constant
expressions.
• Case labels must be unique.
• No two labels can have the same value.
• Case labels must end with colon.
• The break statement is optional.
SKASC 7
18CSS26 & ARTIFICIAL INTELLIGENCE

Rules for Switch Statement

• The break statement transfers the control out of


the switch statement.
• If present, it will be executed when the
expression does not find a matching case label.
• The default may be placed anywhere but usually
placed at the end.
• It is permitted to nest switch statements.
SKASC 8
18CSS26 & ARTIFICIAL INTELLIGENCE

Switch Statement - Example


#include<stdio.h>
int main( ) Output
{ Enter a number :0
int c; Invalid
printf("Enter a number :");
scanf("%d",&c);
switch(c) Output
{ Enter a number :1
case 1: Blue
printf(“Blue");
break; Output
case 2: Enter a number :2
printf(“Green"); Green
break;

default:
printf(“Invalid");
break;
}
return 0;
} SKASC 9
Switch Statement - Example
18CSS26 & ARTIFICIAL INTELLIGENCE

#include <stdio.h>
int main () { Output
/* local variable definition */ Enter a Grade A,B,C,D in CAPS:
char grade; A
printf ("Enter a Grade A,B,C,D in CAPS: \n"); Excellent!
scanf("%c",&grade);
switch(grade) {
Your grade is A
case 'A' :
printf("Excellent!\n" ); Output
break;
Enter a Grade A,B,C,D in CAPS:
case 'B' :
case 'C' : B
printf("Well done\n" ); Well done
break; Your grade is B
case 'D' :
printf("You passed\n" );
break; Output
case 'F' : Enter a Grade A,B,C,D in CAPS:
printf("Better try again\n" );
break;
D
default : You passed
printf("Invalid grade\n" ); Your grade is D
}
printf("Your grade is %c\n", grade );
return 0;
} SKASC 10
18CSS26 & ARTIFICIAL INTELLIGENCE

The Conditional Operator

• The C language has an unusual operator, useful for


making two-way decisions.
• This operator is a combination of ‘?’ and ‘:’ and takes
three operands.
• This operator is popularly known as the conditional
operator.

SKASC 11
18CSS26 & ARTIFICIAL INTELLIGENCE

The Conditional Operator

SKASC 12
18CSS26 & ARTIFICIAL INTELLIGENCE

The Conditional Operator

Syntax:
Conditional expression ? expression 1: expression 2;
• The conditional expression is evaluated first.
• If the result is nonzero, expression 1 is evaluated and
is returned as the value of the conditional expression.
• Otherwise expression 2 is evaluated and its value is
returned.

SKASC 13
18CSS26 & ARTIFICIAL INTELLIGENCE

The Conditional Operator


#include <stdio.h>
int main() {
int y;
int x = 2;
y = (x >= 6) ? 6 : x;
/* This is equivalent to: if (x >= 5) y = 5; else y = x; */
printf("y =%d ",y);
return 0;}

Output
y =2

SKASC 14
18CSS26 & ARTIFICIAL INTELLIGENCE

The Conditional Operator


#include <stdio.h>
int main()
{
// variable declaration
int n1, n2, max;
printf ("Enter a integer number: \n");
scanf("%d%d",&n1,&n2);
// Largest among n1 and n2
max = (n1 > n2) ? n1 : n2;
// Print the largest number
printf("Largest number between %d and %d is %d. ", n1, n2, max);
return 0;
}

Output
Enter a integer number:
112 56
Largest number between 112 and 56 is 112.
SKASC 15
18CSS26 & ARTIFICIAL INTELLIGENCE

THE GOTO STATEMENT

• The ‘goto’ statement helps to branch unconditionally from


one point to another in the program.
• The goto requires a label in order to identify the place
where the branch is to be made.
• A label is any valid variable name and must be followed
by a colon.
• The label is placed immediately before the statement
when the control is to be transferred.

SKASC 16
18CSS26 & ARTIFICIAL INTELLIGENCE

THE GOTO STATEMENT

SKASC 17
18CSS26 & ARTIFICIAL INTELLIGENCE

THE GOTO STATEMENT

• The label: can be anywhere in the program


before or after the goto label; statement.
• When a goto statement is encountered, the flow
of control will jump to the statement
immediately following the label in the goto
statement.

SKASC 18
18CSS26 & ARTIFICIAL INTELLIGENCE

THE GOTO STATEMENT

Backward jump:
– If the label is before the statement goto label; a loop
will be formed and some statements will be executed
repeatedly. Such a jump is known as a backward jump.

Forward jump:

– If the label: is placed after the goto label; some

statements will be skipped and the jump is known as a

forward jump.
SKASC 19
18CSS26 & ARTIFICIAL INTELLIGENCE

THE GOTO STATEMENT


#include <stdio.h>
int main()
{
int num,i=1;
printf("Enter the number whose table you want to print?");
scanf("%d",&num);
table: Output
Enter the number whose table
printf("%d x %d = %d\n",num,i,num*i);
you want to print?12
i++; 12 x 1 = 12
if(i<=10) 12 x 2 = 24
goto table; 12 x 3 = 36
return 0; 12 x 4 = 48
} 12 x 5 = 60
12 x 6 = 72
12 x 7 = 84
12 x 8 = 96
12 x 9 = 108
SKASC 12 x 10 = 120 20
18CSS26 & ARTIFICIAL INTELLIGENCE

THE GOTO STATEMENT


/*To print numbers from 1 to 10 using goto statement*/
#include <stdio.h>
int main()
{
int number;
number=1; Output
repeat: 1
2
printf("%d\n",number); 3
number++; 4
5
if(number<=10) 6
goto repeat; 7
8
return 0; 9
} 10

SKASC 21
18CSS26 & ARTIFICIAL INTELLIGENCE

Summary

• The switch statement tests the value of a given variable


against a list of case values and when the match is found a
block of statements associated with that case is executed.
• Conditional operator is combination of ‘?’ and ‘:’ and takes
three operands.
• The ‘goto’ statement helps to branch unconditionally from
one point to another in the program. The goto requires a
label in order to identify the place where the branch is to be
made.
SKASC 22
18CSS26 & ARTIFICIAL INTELLIGENCE

SKASC 23

You might also like