Decision Statement
Decision Statement
• IF STATEMENT
if (condition)
{ // Code to execute if the condition is true }
Example:
int num = 10;
if (num > 5)
printf("Number is greater than 5.\n");
• IF – ELSE STATEMENT
if (condition)
{ // Code to execute if the condition is true }
else
{ // Code to execute if the condition is false }
Example:
int num = 3;
if (num > 5)
{ printf("Number is greater than 5.\n"); }
else
{ printf("Number is 5 or less.\n"); }
• IF-ELSE IF-ELSE Statement Example
if (condition1) int num = 10;
{ // Code to execute if if (num < 5)
condition1 is true } { printf("Number is less than 5.\n");
else if (condition2) }
{ // Code to execute if else if (num == 5)
condition2 is true } { printf("Number is equal to 5.\n");
else }
{ // Code to execute if none else
of the conditions are true } { printf("Number is greater than 5.\
n"); }
• SWITCH Statement Example:
switch (variable) char grade = 'B’;
{ case value1: switch (grade)
// Code to execute if variable == value1 { case 'A’: printf("Excellent!\n");
break; break;
case 'B’: printf("Well done!\n");
case value2:
break;
// Code to execute if variable == value2
case 'C’: printf("Good effort!\n");
break;
break;
// Additional cases as needed
default: printf("Invalid grade.\n");
default:
}
// Code to execute if no cases match
}
• Conditional (Ternary) Operator
condition? expression1 : expression2;
Example:
int num = 5;
int result = (num > 10) ? 1 : 0; // result is 0
printf("Result: %d\n", result);
To check whether a given age is eligible to vote or not.
Flowgorithm Pseudocode:
Start
Output "Enter your age:“
Input age
If age >= 18
Then Output "You are eligible to vote.“
Else
Output "You are not eligible to vote.“
End If
End
Check that the given number is divisible by 3 and 6.
Start
Output "Enter a number: "
Input num
If (num % 3 == 0) AND (num % 6 == 0) Then
Output "The number is divisible by both 3 and 6."
Else
Output "The number is not divisible by both 3 and 6."
End If
End
To check if a given character is a vowel or consonant.
Start
Output "Enter a character:"
Input char
If char == 'a' OR char == 'e' OR char == 'i' OR char == 'o' OR char == 'u' OR
char == 'A' OR char == 'E' OR char == 'I' OR char == 'O' OR char == 'U' Then
Output "Vowel"
Else If (char >= ‘a' AND char <= 'z') OR (char >= ‘A' AND char <= 'Z') Then
Output "Consonant"
Else
Output "Invalid input"
End If
End
To check if the given temperature is below 21 display as cold, <21 and <35 as
normal, otherwise hot for all other values.
Start
Output "Enter the temperature:"
Input temperature
void main() {
int number;
printf("Enter an integer: "); // Input the number
scanf("%d", &number);
// Check if the number is within 10 of 100 or 200
if ((number >= 90 && number <= 110) || (number >= 190 && number <= 210))
{
printf("True: %d is within 10 of 100 or 200.\n", number);
} else {
printf("False: %d is not within 10 of 100 or 200.\n", number);
}
getch();
}
To find whether a given character is consonant or vowel using switch statement .
#include <stdio.h>
void main() {
char ch; clrscr();
printf("Enter a character: "); // Input a character
scanf("%c", &ch);
// Check using switch
switch (ch) {
case 'a': case 'e': case 'i': case 'o': case 'u': // Lowercase vowels
case 'A': case 'E': case 'I': case 'O': case 'U': // Uppercase vowels
printf("%c is a vowel.\n", ch);
break;
default:
printf("%c is a consonant.\n", ch);
}
getch();