Decision Control Statement

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

Decision Control Statement

1. If statement
2. Switch statement
3. Conditional operator statement
4. Goto statement
Decision Making with if statement
1. Simple if statement
2. if……..else statement
3. Nested if……..else statement
4. Else if ladder
1.Simple if statement
The syntax for simple if statement is as follows:
if(test_condition)
{
Statement_block; //single or multiple statements
}
Statement-xyz;
Here is the test_condition is true than the statement block will be executed.
If the test_condition is false; the statement block will be skipped and execution
will jump to the statement-xyz.
Question 1: WAP to give bonus marks to a student in a subject if marks
obtained by the student is greater than or equal to 80. (Consider bonus marks
=10).
#include<stdio.h> void
main()
{ int bonus=10, marks;
printf("Enter marks of student\n");
scanf("%d",&marks);
if(marks>=80)
{
marks=marks+bonus;
} printf("The marks obtained
are= %d",marks);
}
OUTPUT:
Enter marks of student
90
The marks obtained are= 100

2.if……..else statement
It is the extension of simple if statement.
The general syntax is:
if(test_condition)
{
True statement block(s)
}
else
{
False statement block(s)
}
Statement-xyz;
Question 2: WAP to check if the person is eligible for voting or not.
#include<stdio.h> void
main() { int age;
printf("Enter your age\n");
scanf("%d",&age);
if(age>=18)
{ printf("You are eligible for
voting");
} else { printf("You are not eligible for
voting");
}
}
OUTPUT:
Enter your age
56
You are eligible for voting.
Practice Question 1: WAP to check if the student has passed or failed
in a subject. (consider passing marks greater than equal to 40).
3. Nested if……else statement
When series of decision are involved, we may have to use more than one
if….else statement in nested form.
The syntax is as follows:

if(test_condition-1)
{
if(test condition-2)
{
Statement-1;
}
else
{
Statement-2;
}
}
else
{
Statement-3;
}
Statement-xyz;

Question 3: WAP to print the largest of three numbers using nested if…else
statement.

#include<stdio.h> void
main()
{ int A,B,C; printf("Enter
numbers\n");
scanf("%d%d%d",&A,&B,&C);
printf("\nThe largest Number is\n");
if(A>B) { if(A>C)

{
printf("A is greatest=%d",A);
}
else
{
printf("B is greatest=%d",B);
}
} else
{ if(C>
B)
{
printf("C is greatest%d",C);
}
else
{
printf("B is Greatest%d",B);
}

}
}

You might also like