Decision Making
Decision Making
Decision-Making
Control Statement in C
The control statements used in the C language help a user to specify a program
control’s flow. In simpler words, the control statements help users specify the order of
execution of the instructions present in a program. These make it possible for the
program to make certain decisions, perform various tasks repeatedly, or even jump
from any one section of the code to a different section.
Why Do We Use Control Statements in C?
Conditional statements are used in the C programming language for making certain
decisions on the basis of the available conditions. These conditional statements get
executed sequentially in case no condition is present around the statements. Whenever we
put a condition for the block statements, the flow of execution may get altered on the basis
of the result that is evaluated by the condition in the program. The process mentioned here
Critical
is known as Thinking
decision-making in the C language. Problem Solving
Decision making is the process of Decision making is crucial for problem
identifying and selecting solving. It helps us to evaluate and weigh
alternatives based on critical different options to make the best choice.
thinking, logic, and reasoning.
Types of Control Statements in C
Control statements in C help to control the flow of program execution. It is used to direct
the execution of statements under certain conditions.
Conditional statements in C
Goto statements in C
01 Simple if
statements
02 If-else statements
03 Nested if-else
04 else-if ladder
01 Simple if statements
Syntax of if Statement
if(condition)
{
// Statements to execute if
// condition is true
}
Simple if statements
01 Simple if
statements
02 If-else statements
Program to illustrate If statement
#include <stdio.h>
int main()
{ 01 Simple if
statements
int x = 20;
int y = 22;
if (x<y) 02 If-else statements
{
printf("Variable x is less than y");
}
return 0;
}
Program to display a number if it is negative
#include <stdio.h>
int main() {
01number;
int Simple if
statements
printf("Enter an integer: ");
scanf("%d", &number); 02 If-else statements
return 0;
}
Program to illustrate If statement
01 Simple if statements
#include <stdio.h>
int main()
{
int i = 10; Output:
02 If-else statements
Program: Check whether an integer is odd
or even
01 Simple if statements
#include <stdio.h>
int main() {
int number;
printf("Enter an integer: ");
scanf("%d", &number);
02 If-else statements
// True if the remainder is 0
if (number%2 == 0) {
printf("%d is an even integer.",number);
}
else {
printf("%d is an odd integer.",number);
}
return 0;
}
Program to illustrate If - else statement
01 Simple if statements
#include <stdio.h> #include <stdio.h>
}
04 else-if ladder FlowChart
02 If-else statements
Program to check whether a number is positive,
negative using else-if ladder
#include <stdio.h>
int main()
{
int n;
printf("Enter integers: ");
scanf("%d", &n);
if (n > 0) {
printf("Positive");
}
else if (n < 0) {
printf("Negative");
}
// if a number is neither Positive nor Negative
else {
printf("Zero");
}
return 0;}
Program to relate two integers using =, > or < symbol
#include <stdio.h>
int main() {
int number1, number2;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
//checks if the two integers are equal.
if(number1 == number2) {
printf("Result: %d = %d",number1,number2);
}
//checks if number1 is greater than number2.
else if (number1 > number2) {
printf("Result: %d > %d", number1, number2); }
• They are a substitute for long if statements that compare a variable to several
integral values.
dispatch execution to different parts of code based on the value of the expression .
Flowchart of Switch Statement
Rules of the switch case statement
• In a switch statement, the “case value” must be of “char” and “int” type.
#include <stdio.h>
int main() {
int language = 10;
switch (language) {
case 1:
printf("C#\n");
break;
case 2:
printf("C\n");
break;
case 3:
printf("C++\n");
break;
default:
printf("Other programming language\n");}}
• Write a C program to find the largest of three numbers.
• Write a C program to accept two integers and check whether they are equal
or not.