0% found this document useful (0 votes)
2 views

Decision Statement

The document provides an overview of various decision statements in programming, including if statements, if-else statements, switch statements, and the conditional (ternary) operator. It includes pseudocode examples for checking voting eligibility, divisibility, character types, temperature classification, Armstrong numbers, and grade computation. Additionally, it presents C code snippets for checking if a number is within a certain range and determining if a character is a vowel or consonant.

Uploaded by

NAKUL VTV
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Decision Statement

The document provides an overview of various decision statements in programming, including if statements, if-else statements, switch statements, and the conditional (ternary) operator. It includes pseudocode examples for checking voting eligibility, divisibility, character types, temperature classification, Armstrong numbers, and grade computation. Additionally, it presents C code snippets for checking if a number is within a certain range and determining if a character is a vowel or consonant.

Uploaded by

NAKUL VTV
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

DECISION STATEMENTS

• 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

If temperature < 21 Then


Output "Cold"
Else If temperature >= 21 AND temperature < 35 Then
Output "Normal"
Else
Output "High"
End If
End
• To check if the given number is Armstrong’s number.
An Armstrong number (or Narcissistic number) is a
number that is equal to the sum of its digits each
raised to the power of the number of digits.
For example:
• 153=13+53+33153 = 1^3 + 5^3 + 3^3153=13+53+33 (3
digits)
• 9474=94+44+74+449474 = 9^4 + 4^4 + 7^4 +
4^49474=94+44+74+44 (4 digits)
Start // Calculate the sum of digits raised to the
power of the number of digits
1. Output "Enter a number:"
12. While temp > 0 Do
2. Input number 13. digit = temp % 10
3. temp = number 14. sum = sum + pow(digit,digits)
4. sum = 0 15. temp = temp / 10
5. digits = 0 16. End While
6. // Count the number of
digits 17. // Check if the number is an Armstrong
number
7. While temp > 0 Do
18. If sum == number Then
8. digits = digits + 1 19. Output "Armstrong number"
9. temp = temp / 10 20. Else
10. End While 21. Output "Not an Armstrong number"
11. temp = number 22. End If
To compute grades of students based on a given end
semester grade rule using if else adder
Start
Output "Enter the marks:"
Input marks
If marks >= 90 AND marks <= 100 Then
Output "Grade: A"
Else If marks >= 80 AND marks < 90 Then
Output "Grade: B"
Else If marks >= 70 AND marks < 80 Then
Output "Grade: C"
Else If marks >= 60 AND marks < 70 Then
Output "Grade: D"
Else
Output "Grade: F"
End If
End
#include <stdio.h> else if (marks >= 80 && marks < 90) {
void main() { printf("Grade: B\n");
int marks; clrscr(); }
// Input the marks else if (marks >= 70 && marks < 80) {
printf("Enter the marks (0-100): "); printf("Grade: C\n");
scanf("%d", &marks); }
// Validate marks range else if (marks >= 60 && marks < 70) {
if (marks < 0 || marks > 100) { printf("Grade: D\n");
printf("Invalid marks. Please enter }
a number between 0 and 100.\n"); else {
} else { printf("Grade: F\n");
// Check grade using if-else ladder }
if (marks >= 90 && marks <= 100) { }
printf("Grade: A\n"); getch();
} }
To check a given integer and return true if it is within 10 of
100 or 200 .
• A number is within 10 of 100 if it lies in the range [90,110][90, 110]
[90,110].
• A number is within 10 of 200 if it lies in the range [190,210][190, 210]
[190,210].
#include <stdio.h>

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();

You might also like