PC Unit 2

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Sri Manakula Vinayagar Engineering College Unit – 2 Programming in C

UNIT II - DECISION MAKING

 DECISION MAKING AND BRANCHING

 RELATIONAL OPERATORS

 LOGICAL OPERATORS

 IF STATEMENT

 IF ELSE STATEMENT

 IF ELSE IF STATEMENT

 NESTED IF STATEMENT

 SWITCH-CASE STATEMENT

PART – A (2 Marks)

Two mark questions with answer

1. What is the control statement? Give examples?


„C‟ language provides all the standard control structure that is available in programming
languages. These structures are capable of processing any information.
These are all the following conditions statements
 if statement
 if _else statement
 nested if_ else statement
 if_ else ladder

2. What is if statement?
The simplest form of the control statements is the if statement. It is very frequently used in
decision making and allowing the flow of program execution.
Syntax
if ( condition)
{
Statements;
}
In this if statement, its test a condition. If the condition is true, the statement associated with if
is executed, otherwise the statements are not executed.

3. List out the rules for writing switch statement?


 No real numbers are used in expression
 The switch can be nested
 The case keyword must be terminate with colon (:)
Sri Manakula Vinayagar Engineering College Unit – 2 Programming in C

4. Compare switch and nested if statement?

Switch case Nested if


Switch can only test constant values. If can evaluate relational and logical
expressions.
In switch case statement nested if can be In nested if statement , switch 0 case
used. can be used.
Characters constants are automatically Character constants are automatically
converts to integers. converts to integer.

5. What are enumerated data types?


It is a user defined data types .It is provided by “c” language.

Syntax :
enum identifier { value 1,value 2, …value n } ;
enumday w_st, w_end;
w_st = mon ;
w_ end = sun;

The identifier follows with keyword enum is used to declare the variable.

6. Difference between break and continue statement? MAY 2015


Break Continue
Break statement takes the control to the Continue statement takes the control to
outside of the loop. the beginning of the loop.
It is also used in switch statement. It is used only in loop statement
It is always associated with if condition It is always associated with if condition
loops. loops.

7. Difference between nested if and switch case?


Switch ( ) case Nested if
It can test only constant values. It can evaluate relational or logical
expression
Character constant are automatically Character constant are automatically
converted to integers converted to integers.
Switch ( ) case statement nested if can In nested if statement, switch ( ) case
be used statement can be used.

8. Give two examples for logical and relational expressions.


Relational Expressions:
(i) (a>b)
(ii) (a==b)
Logical Expressions:
(i) if((a>b)&&(a>c))
(ii) if((a>b)||(a>c))

9. Define if else statement.


It is otherwise known as Two-way decisions. It is handled with if-else statements. The decision is
based on a „test expression or condition’ that evaluates to either true or false.
 If the test condition is true, the true block will be executed then control goes to the next
executable statement.
 If the test condition is false, the false block will be executed control goes to the next executable
statement.

10.Define nested if statement.


It is otherwise known as Two-way with sub-way decisions. If else statement is enclosed within
another if else structure. An if statement can be followed by an optional else if...else statement, which
is very useful to test various conditions using single if...else if statement.
Sri Manakula Vinayagar Engineering College Unit – 2 Programming in C

11. What is switch case statement? Give syntax.


A switch statement allows a variable to be tested for equality against a list of values. Each value is called a
case, and the variable being switched on is checked for each switch case.
Syntax

switch(expression)
{
case label1:
block 1;
break;
case label2:
block 2;
break;
……..
……..
default:
Default block;
break;
}

PART – B (5 Marks & 10 Marks)

1. DECISION MAKING AND BRANCHING


C has some kinds of statements that permit the execution of a single statement, or a block of
statements, based on the value of a conditional expression or selection among several statements
based on the value of a conditional expression or a control variable.

These are all the following conditional statements


(i) if statement
(ii) if-else statement
(iii) Nested if-else statement
(iv) if-else ladder (else if ladder)
(i)if statement:
It is otherwise known as One-way decisions. It is used to control the flow of execution of the
statements. The decision is based on a „test expression or condition’ that evaluates to either true or
false.
 If the test condition is true, the corresponding statement is executed.
 If the test condition is false, control goes to the next executable statement.

Syntax: Flowchart
if(condition is true)
{

Statement 1;
------------
------------
Statement n;
}

Next Statement;
Sri Manakula Vinayagar Engineering College Unit – 2 Programming in C

Program:
#include<stdio.h>
#include<conio.h>
main()
{
int m,n,a;
clrscr();
printf(“Enter 2 numbers:”);
scanf(“%d%d”,&m,&n);
if(m>n)
{
a=m;
m=n;
n=a;
}
printf(“The interchanged values are: “%d%d”,m,n);
getch();
}

(ii)if-else statement:
It is otherwise known as Two-way decisions. It is handled with if-else statements. The
decision is based on a „test expression or condition’ that evaluates to either true or false.
 If the test condition is true, the true block will be executed then control goes to the next
executable statement.
 If the test condition is false, the false block will be executed control goes to the next executable
statement.
Syntax: Flowchart

if(condition is true)
{
True block;
}

else
{
false block;
}
Next statement;

Program:
#include<stdio.h>
main()
{
int a,b;
printf(“Enter two numbers:”);
scanf(“%d%d”,&a,&b);
if(a>b)
{
printf(“A is largest”);
}
else
{
printf(“B is largest”);
}
getch();
}
OUTPUT:
Enter two numbers:12 5
A is largest
Sri Manakula Vinayagar Engineering College Unit – 2 Programming in C

(iii) Nested if-else statement


It is otherwise known as Two-way with sub-way decisions. If else statement is enclosed
within another if else structure. An if statement can be followed by an optional else if...else statement,
which is very useful to test various conditions using single if...else if statement

Syntax: Flowchart
if(condition 1)
{
if(condition 2)
{
True statement 2
}
else
{
False statement 2;
}
}
else
{
False statement 1;
}
Next Statement;

Program:
#include<stdio.h>
main()
{
int a b c;
printf(“Enter the value for A, B and C:”);
scanf(“%d%d%d”,&a,&b,&c);
if((a>b)&&(a>c))
{
printf(“A is largest”);
}
else
{
if(b>c)
{
printf(“B is largest”);
}
else
{
printf(“C is largest”);
}
}
}
Output:
Enter the value for A,B and C:12 13 5
B is largest
Sri Manakula Vinayagar Engineering College Unit – 2 Programming in C

(iv) if – else ladder


It is otherwise known as Multi-way decisions. Each and every else block will have if statement.
Last else block cannot have if block. Last else will have default statement.

Syntax Flowchart

if(condition1)
statement 1;
else if(condition 2)
statement 2;
else if(condition 3)
statement 3;
--------
--------
else
default
statement;

Example:
#include<stdio.h>
#include<conio.h>
main()
{
char x;
clrscr();
printf("Enter character: ");
scanf("%c",&x);
if(x>='a' && x<='z')
printf("Small letter");
else if(x>='A' && x<='Z')
printf("Capital letter");
else if(x>='0' && x<='9')
printf("Digit");
else
printf("Special Symbol");
getch();
}

2. SWITCH CASE STATEMENT


It is an alternative solution for else-if ladder concept. Switch statement is used to execute a
particular group of statements from several groups of statements. It is a multi-way decision statement,
test the value of given variable or expression in a list of case values.

Rules of using switch case


 Values for „case must‟ be integer or character constants.
 Floating point values are not allowed as case label.
 Switch case should have one default label.(optional)
 Const Variable is allowed in switch Case Statement.
 The order of the „case‟ statements is unimportant.
 Case Label must be unique
 Case labels must have constants / constant expression
Sri Manakula Vinayagar Engineering College Unit – 2 Programming in C

 Case labels must end with (:) colon.


 Empty Switch case is allowed.
 Each compound statement of a switch case should contain break statement to exit from case.
 Two or more cases may share one break statement
 Comparison operators are not accepted
 Nesting ( switch within switch ) is allowed.

Advantages of Using Switch statement


 Easier to debug
 Faster execution potential
 Easier to read
 Easier to understand
 Easier to maintain

Syntax Flowchart

switch(expression)
{
case label1:
block 1;
break;
case label2:
block 2;
break;
……..
……..
default:
Default block;
break;
}

Example:1

#include<stdio.h>
void main()
{
int a=1;

switch(a)
{
case 1:
printf(“I am in case 1 \n”);
break;
case 2:
printf(“I am in case 2 \n”);
break;
default:
printf(“I am in case default\n”);
break;
}
}
Sri Manakula Vinayagar Engineering College Unit – 2 Programming in C

Output:
I am in case 1

Example:2

#include <stdio.h>
int main ()
{
/* local variable definition */
char grade = 'B';
switch(grade)
{
case 'A' :
printf("Excellent!\n" );
break;
case 'B' :
case 'C' :
printf("Well done\n" );
break;
case 'D' :
printf("You passed\n" );
break;
case 'F' :
printf("Better try again\n" );
break;
default :
printf("Invalid grade\n" );
}
printf("Your grade is %c\n", grade );
return 0;
}
When the above code is compiled and executed, it produces the following result:
Well done
Your grade is B

3. NESTED SWITCH STATEMENTS


It is possible to have a switch as part of the statement sequence of an outer switch. Even if
the case constants of the inner and outer switch contain common values, no conflicts will arise.

Syntax
The syntax for a nested switch statement is as follows:
switch(ch1) {
case 'A':
printf("This A is part of outer switch" );
switch(ch2) {
case 'A':
printf("This A is part of inner switch" );
break;
case 'B': /* case code */
}
break;
case 'B': /* case code */
}
Sri Manakula Vinayagar Engineering College Unit – 2 Programming in C

Example
#include <stdio.h>

int main () {

/* local variable definition */


int a = 100;
int b = 200;

switch(a) {

case 100:
printf("This is part of outer switch\n", a );

switch(b) {
case 200:
printf("This is part of inner switch\n", a );
}
}

printf("Exact value of a is : %d\n", a );


printf("Exact value of b is : %d\n", b );

return 0;
}

When the above code is compiled and executed, it produces the following result

This is part of outer switch


This is part of inner switch
Exact value of a is : 100
Exact value of b is : 200

4. RELATIONAL OPERATOR

A relational operator is mainly used to compare two or more operands.


Following table shows all the relational operators supported by C language. Assume variable A holds 10
and variable B holds 20, then:

Operator Description Example

== Checks if the values of two (A == B) is not true.


operands are equal or not, if
yes then condition becomes
true.
!= Checks if the values of two (A != B) is true.
operands are equal or not, if
values are not equal then
condition becomes true.
> Checks if the value of left (A > B) is not true.
operand is greater than the
value of right operand, if yes
then condition becomes true.
< Checks if the value of left (A < B) is true.
operand is less than the
value of right operand, if yes
then condition becomes true.
Sri Manakula Vinayagar Engineering College Unit – 2 Programming in C

>= Checks if the value of left (A >= B) is not true.


operand is greater than or
equal to the value of right
operand, if yes then
condition becomes true.
<= Checks if the value of left (A <= B) is true
operand is less than or equal
to the value of right operand,
if yes then condition
becomes true.

Try the following example to understand all the relational operators available in C programming
language:
Example: 1
#include <stdio.h>
main()
{
int a = 21;
int b = 10;
int c ;
if( a == b )
{
printf("Line 1 - a is equal to b\n" );
}
else
{
printf("Line 1 - a is not equal to b\n" );
}
if ( a < b )
{
printf("Line 2 - a is less than b\n" );
}
else
{
printf("Line 2 - a is not less than b\n" );
}

if ( a > b )
{
printf("Line 3 - a is greater than b\n" );
}
else
{
printf("Line 3 - a is not greater than b\n" );
}
/* Lets change value of a and b */
a = 5;
b = 20;
if ( a <= b)
{
printf("Line 4 - a is either less than or equal to b\n" );
}
if ( b >= a )
{
printf("Line 5 - b is either greater than or equal to b\n" );
}
}
Sri Manakula Vinayagar Engineering College Unit – 2 Programming in C

When you compile and execute the above program, it produces the following result:
Line 1 - a is not equal to b
Line 2 - a is not less than b
Line 3 - a is greater than b
Line 4 - a is either less than or equal to b
Line 5 - b is either greater than or equal to b

Example:2

OPERATOR MEANING EXAMPLE RETURN


< Less than 2<9 VALUE
1
> Greater than 2>9 0
== Equal to 2==2 0
!= Not equal to 2!=3 0
#include<stdio.h>
#include<coio.h>
main( )
{
clrscr( );
printf(“\n Condition: Return values\n”);
printf(“\n 5!=5 :%5d”,5!=5);
printf(“\n 5==5: %5d”,5==5);
}
Output:
Condition : Return Values
5! =5 0
5==5 1

5. LOGICAL OPERATOR

Logical operators are used to combine the results of two or more conditions.
Following table shows all the logical operators supported by C language. Assume variable A holds 1
and variable B holds 0, then:

Operator Description Example


&& Called Logical AND operator. (A && B) is false.
If both the operands are non-
zero, then condition becomes
true.
|| Called Logical OR Operator. (A || B) is true.
If any of the two operands is
non-zero, then condition
becomes true.
! Called Logical NOT Operator. !(A && B) is true.
Use to reverses the logical
state of its operand. If a
condition is true, then Logical
NOT operator will make false.
Sri Manakula Vinayagar Engineering College Unit – 2 Programming in C

Example:1

#include<stdio.h>
main()
{
printf("\n Condition : return values\n");
printf("\n5>3 && 5<10: %5d",5>3 && 5<10);
printf("\n8>5||8<2 : %5d",8>5||8<2):
printf("\n!(8==8) : %5d",!(8==8));
}

Output:
Condition : Return Values
5>3 && 5>10: 1
8>5 II 8<2 : 0
!(8==8) : 0

Example:2

#include <stdio.h>
main()
{
int a = 5;
int b = 20;
int c ;
if ( a && b )
{
printf("Line 1 - Condition is true\n" );
}
if ( a || b )
{
printf("Line 2 - Condition is true\n" );
}
/* lets change the value of a and b */
a = 0;
b = 10;
if ( a && b )
{
printf("Line 3 - Condition is true\n" );
}
else
{
printf("Line 3 - Condition is not true\n" );
}
if ( !(a && b) )
{
printf("Line 4 - Condition is true\n" );
}
}
When you compile and execute the above program, it produces the following result:
Line 1 - Condition is true
Line 2 - Condition is true
Line 3 - Condition is not true
Line 4 - Condition is true
13

You might also like