0% found this document useful (0 votes)
2 views6 pages

Decision Making in C Programming

The document outlines an assignment for F.Y. B.C.A. students focusing on decision-making statements in programming, including if statements, if-else statements, switch cases, and conditional operators. It provides examples and explanations of each structure, along with typecasting and practical programming assignments for students to complete. The assignment includes tasks related to ASCII values, checking even or odd numbers, determining voting eligibility, and grading systems.

Uploaded by

Abhay Gupta
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)
2 views6 pages

Decision Making in C Programming

The document outlines an assignment for F.Y. B.C.A. students focusing on decision-making statements in programming, including if statements, if-else statements, switch cases, and conditional operators. It provides examples and explanations of each structure, along with typecasting and practical programming assignments for students to complete. The assignment includes tasks related to ASCII values, checking even or odd numbers, determining voting eligibility, and grading systems.

Uploaded by

Abhay Gupta
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

B.C.A: 2025-2026 F.Y. B.C.A.

(SCIENCE)

Assignment No. 2

Decision Making Statements (if and if-else, Switch Case)

THEORY:

Normally, your program flows along line by line in the order in which it appears in
your source code. But, it is sometimes required to execute a particular portion of code
only if certain condition is true; or false i.e. you have to make decision in your
program. There are three major decision making structures. Four decision making
structures:

1. If statement
2. If-else statement
3. Switch case
4. Conditional Operator (Rarely used)

The if statement:
The if statement enables you to test for a condition (such as whether two
variables are equal) and branch to different parts of your code, depending on
the result. The simplest form of an if statement is:
if (expression)statement;

The expression may consist of logical or relational operators like (> >= < <=&& || )
An understanding of if statement is demonstrated with the following example:

int main()
{
int var;
printf(“Enter any number;”);
scanf(“%d”,&var);
if(var==10)
printf(“The user entered number is Ten”);
}

The if-else statement:


Often your program will want to take one branch if your condition is true, another
if it is false.
The keyword else can be used to perform this functionality:

if (expression)

statement;

else
statement;
B.C.A: 2025-2026 F.Y. B.C.A. (SCIENCE)

Note: To execute multiple statements when a condition is true or false,


parenthesesare used.

Consider the following example that checks whether the input character is an upper case or
lower case:

#include<stdio.h>
int main()
{
char ch;
printf (“Enter any character”);
scanf("%c", &ch);
if(ch >= 'A' && ch <= 'Z')

printf("%c is uppercase character", ch);

else
printf ("%c is a lowercase character",ch);
return 0;
}

Conditional (Ternary) Operator


The conditional operator (?:) is C’s only ternary operator; that is, it is the only operator to take three
terms.
The conditional operator takes three expressions and returns a value: (expression1)? (expression2):
(expression3);

This line is read as "If expression1 is true, return the value of expression2; otherwise, return the value
of expression3." Typically, this value would be assigned to a variable.
An Example:
#include<stdio.h>
int main()
{
float per;
printf("\n Enter your percentage ");
scanf("%f",&per);
printf("\n you are ");
printf("%s", per >= 60 ?"Passed": "Failed");
return 0;
}

Typecasting
Typecasting allow a variable of one type to act like another for a single operation. In C typecasting is
performed by placing, in front of the value, the type name in parentheses.

Eg. int a =5;


int b=2;
float c=(float)a/b;
B.C.A: 2025-2026 F.Y. B.C.A. (SCIENCE)

The Switch Statement:


Unlike if, which evaluates one value, switch statements allow you to branch
on any of a number of different values.
The general form of the switch statement is:

switch (expression)
{
case constant1:
// statements
break;

case constant2:
// statements
break;
.
.
default:
// default statements
}
How does the switch statement work?
The expression is evaluated once and compared with the values of each case label.
If there is a match, the corresponding statements after the matching label are executed. For example,
if the value of the expression is equal to constant2, statements after case constant2: are executed until
break is encountered.
If there is no match, the default statements are executed.

Notes:
 If we do not use the break statement, all statements after the matching label are also executed.
The default clause inside the switch statement is optional.
 The expression in switch case must evaluates to return an integer, character or enumerated
type.
An Example:
#include<stdio.h>
int main(){
int grade;
printf("\n Enter your Grade:");
scanf("%c",&grade);
switch(grade)
{
case 'A':
case 'a':
printf("\n Your percentage is 80 or above 80 ");
break;
case 'B':
case 'b':
printf("\n Your percentage is in 70-80 ");
break;
default:
printf("\n Your percentage is below 70 ");
}
return 0;}
B.C.A: 2025-2026 F.Y. B.C.A. (SCIENCE)

Assignment:
SET A

1. WAP to print the ASCII value of any given character.


2. WAP to print character value corresponding to any ASCII value.
3. WAP that takes a number as input from user and checks whether the number is even or odd
a) Using if-else
b) Using conditional operator:

4 WAP that takes three numbers as input from user and finds minimum
a) Using if-else
b) Using conditional operator:

SET B

1. WAP to take input student age and check whether given student can vote or not.
2. WAP to check whether a given year is leap year or not.
3. WAP to print grade of a student using If Else Ladder Statement.
The grades areassigned as followed:
a. Marks Grade
b. marks<50 F
c. 50≤marks< 60 C
d. 60≤marks<70 B
e. 70≤marks<80 B+
f. 80≤marks<90 A
g. 90≤mars≤ 100 A+

4. WAP to print grade of a student using switch case


5. WAP to accept the week day as number from user and display Monday to Sunday.

SET C
1. WAP to create a simple calculator using switch case.
2. WAP to check white space (new line, tab and space) using switch case.

Assignment Evaluation
0: Not Done [ ] 1: Incomplete [ ] 2: Late Complete [ ]
3: Needs Improvement [ ] 4: Complete [ ] 5: Well Done [ ]
B.C.A: 2025-2026 F.Y. B.C.A. (SCIENCE)

You might also like