KEMBAR78
Computer Science Unit-2 - 2 Sem1 | PDF | Software Engineering | Computing
0% found this document useful (0 votes)
30 views6 pages

Computer Science Unit-2 - 2 Sem1

The document discusses different types of conditional statements in programming including if, if else, if else if else, and switch case statements. Syntax and examples are provided for each type of conditional statement.

Uploaded by

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

Computer Science Unit-2 - 2 Sem1

The document discusses different types of conditional statements in programming including if, if else, if else if else, and switch case statements. Syntax and examples are provided for each type of conditional statement.

Uploaded by

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

Question-1

What do you mean by conditional statement or decision making statement?


Explain all the conditional statement with proper example.
Or,
Write the syntax of following:
(1) if statement
(2) if else statement
(3) if else if else statement
(4) switch case statement

ANSWER-1
Conditional statements or Decision control statements:

Conditional statements are used whenever we have requirement to make a


decision. These are of following types-
1. if statement
2. if else statement
3. if else if else statement
4. switch case statement

if statement:
syntax:

if (condition)
{
----------------
---------------- Block of statements
----------------
}

For example: WAP to check if the given number is odd.

#include<stdio.h>
void main( )
{
int num;
printf(“Enter the number”);
scanf(“ %d ”, &num);
if ((num%2) != 0)
{
printf(“Odd Number”);
}
}
if else statement:
syntax:

if (condition)
{
---------------
--------------- Block of statements
---------------
}
else
{
--------------
-------------- Block of statements
--------------
}

For example: WAP to check whether the given number is odd or even.

#include<stdio.h>
void main( )
{
int num;
printf(“Enter the number”);
scanf(“ %d”, &num);
if ((num%2) == 0)
{
printf(“Even Number”);
}
else
{
printf(“Odd Number”);
}
}
if else if else statement:
syntax:

if (condition1)
{
---------------
--------------- Block of statements
---------------
}
else if (condition2)
{
--------------
-------------- Block of statements
--------------
}
else if (condition3)
{
--------------
-------------- Block of statements
--------------
}
-
-
-
-
-
-
else if (conditioN-1)
{
--------------
-------------- Block of statements
--------------
}
else
{
--------------
-------------- Block of statements
--------------
}
For example: WAP to find the greatest among three integers.

#include<stdio.h>
void main( )
{
int a, b, c;
printf(“ Enter three values ”);
scanf(“ %d %d %d”, &a, &b, &c);
if (a>b && a>c)
{
printf(“a is greatest”);
}
else if (b>a && b>c)
{
printf(“b is greatest”);
}
else
{
printf(“C is greatest”);
}
}

switch case statement:


syntax:
switch(expression)
{
case value1: -------------- Block of statements
--------------

case value2: -------------- Block of statements


--------------

case value3: -------------- Block of statements


--------------
-
-
-
case valueN: -------------- Block of statements
--------------

default: -------------- Block of statements


--------------
}
For example: WAP to calculate addition, subtraction, multiplication and
division according to user’s choice or switch case statement.

#include<stdio.h>
void main( )
{
int ch;
float a, b, r;
clrscr( );
printf(“1: Addition\n2: Subtraction\n3: Multiplication\n4: Division ”);
printf(“\n\nEnter your choice ”);
scanf(“ %d ”, &ch);

switch(ch)
{
case 1: printf(“\nEnter two numbers”);
scanf(“ %f %f ”, &a, &b);
r = a + b;
printf(“ Sum= %f ”, r);
break;

case 2: printf(“\nEnter two numbers”);


scanf(“ %f %f ”, &a, &b);
r = a - b;
printf(“ Difference= %f ”, r);
break;

case 3: printf(“\nEnter two numbers”);


scanf(“ %f %f ”, &a, &b);
r = a * b;
printf(“ Multiplication= %f ”, r);
break;

case 4: printf(“\nEnter two numbers”);


scanf(“ %f %f ”, &a, &b);
r = a / b;
printf(“ Division= %f ”, r);
break;

default: printf(“ You have pressed wrong key ”);


}
}
Question-2
What is nested if statement? Explain with an example.

Answer-2

Nested if statement:

if statement within another if statement is called nested if statement.

For Example: Program to find the largest number among three numbers
without using logical operators.

#include<stdio.h>
void main( )
{
int a, b, c;
printf(“Enter three numbers”);
scanf(“%d %d %d”, &a, &b, &c);

if(a>b)
{
if(a>c)
printf(“A is largest”);
else
printf(“C is largest”);

}
else
{
if(b>c)
printf(“B is largest”);
else
printf(“C is largest”);

}
}

You might also like