Professional Documents
Culture Documents
PC Unit 2
PC Unit 2
PC Unit 2
RELATIONAL OPERATORS
LOGICAL OPERATORS
IF STATEMENT
IF ELSE STATEMENT
IF ELSE IF STATEMENT
NESTED IF STATEMENT
SWITCH-CASE STATEMENT
PART – A (2 Marks)
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.
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.
switch(expression)
{
case label1:
block 1;
break;
case label2:
block 2;
break;
……..
……..
default:
Default block;
break;
}
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
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
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();
}
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
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 () {
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 );
}
}
return 0;
}
When the above code is compiled and executed, it produces the following result
4. RELATIONAL OPERATOR
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
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:
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