0% found this document useful (0 votes)
32 views32 pages

Decision Making

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views32 pages

Decision Making

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

Selection Structures and

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.

Decision –Making control statements

Conditional statements in C

Goto statements in C

Loop control statements in C


Decision Making control statements

01 Simple if
statements

02 If-else statements

03 Nested if-else

04 else-if ladder
01 Simple if statements

The statements under if blocks are executed when


02statements
condition is satisfied else under if block
If-else statements
are not executed

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

// true if number is less than 0


if (number < 0) {
printf("You entered %d.\n", number);
}

return 0;
}
Program to illustrate If statement
01 Simple if statements
#include <stdio.h>

int main()
{
int i = 10; Output:

if (i > 15) { I am Not in if


printf("10 is greater than 15");
}

printf("I am Not in if");


}
02 If-else statements
01 Simple if statements

The statements under if block is executed when condition is satisfied


otherwise else block is executed

Syntax of if else in C/C++02 If-else statements


if (condition)
{
// Executes this block if
// condition is true
}
else
{
// Executes this block if
// condition is false
}
02 If-else statements
01 Simple if statements

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>

int main() int main(void) {


{ int age;
int i = 20;
printf("Please enter your age: ");
02 If-else statements
if (i < 15) {
scanf("%i", &age);
printf("i is smaller than 15");
} if (age < 18) {
else { printf("You need to be over 18 years old to
continue\n");
printf("i is greater than 15"); } else {
} printf("You are over 18 so you can continue \n");
return 0; }
}
}
The nested if-else statements consist of another if
or else

Syntax of Nested if-else


if (condition1)
{ 02 If-else statements
// Executes when condition1 is true
if (condition2)
{
// Executes when condition2 is true
}
else
{
// Executes when condition2 is false
}
else
}
02 If-else statements
Program to illustrate Nested If - else
statement
#include
01 <stdio.h>
Simple if statements
int main() {
int number1, number2;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);

if (number1 >= number2)02 { If-else statements


if (number1 == number2) {
printf("Result: %d = %d",number1,number2);
}
else {
printf("Result: %d > %d", number1, number2);
}
}
else {
printf("Result: %d < %d",number1, number2);
}
return 0;
}
Program to illustrate Nested If - else
statement
01 Simple
#include if statements
<stdio.h>
int main () {
/* local variable definition */
int a = 100;
int b = 200;
/* check the boolean condition */ statements
02 If-else
if( a == 100 ) {
/* if condition is true then check the following */
if( b == 200 ) {
/* if condition is true then print the following */
printf("Value of a is 100 and b is 200\n" );
}
}
printf("Exact value of a is : %d\n", a );
printf("Exact value of b is : %d\n", b );
return 0;
}
04 else-if ladder

else-if ladder statements contain multiple


else-if, when either of the condition is true
the statements under that particular “if” will
Syntax: be executed
// any if-else ladder starts with an if statement only
if(condition) { 02 If-else statements
}
else if(condition) {
// this else if will be executed when condition in if is
false and
// the condition of this else if is true
}
.... // once if-else ladder can have multiple else if
else { // at the end we put else

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

//checks if both test expressions are false


else {
printf("Result: %d < %d",number1, number2);
}
return 0; }
Program to Calculate Grade According to marks
#include <stdio.h>
int main()
{
int marks = 91;
if (marks <= 100 && marks >= 90)
printf("A+ Grade");
else if (marks < 90 && marks >= 80)
printf("A Grade");
else if (marks < 80 && marks >= 70)
printf("B Grade");
else if (marks < 70 && marks >= 60)
printf("C Grade");
else if (marks < 60 && marks >= 50)
printf("D Grade");
else
printf("F Failed");
return 0;
}
Switch Statement
The switch statement in C tests the value of a The syntax of the switch
variable and compares it with multiple cases. Once statement:

the case match is found, a block of statements


associated with that particular case is executed.

Each case in a block of a switch has a different


name/number which is referred to as an identifier.
The value provided by the user is compared with all
the cases inside the switch block until the match is
found.
Switch Statement
• Switch case statements follow a selection-control mechanism and allow a value to
change control of execution.

• They are a substitute for long if statements that compare a variable to several
integral values.

• The switch statement is a multiway branch statement. It provides an easy way to

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.

• There can be one or N number of cases.

• The values in the case must be unique.

• Each statement of the case can have a break statement. It is optional.

• The default Statement is also optional


How switch Statement Work?

Step 1: The switch variable is evaluated.


Step 2: The evaluated value is matched against all the present cases.
Step 3A: If the matching case value is found, the associated code is
executed.
Step 3B: If the matching code is not found, then the default case is
executed if present.
Step 4A: If the break keyword is present in the case, then program control
breaks out of the switch statement.
Step 4B: If the break keyword is not present, then all the cases after the
matching case are executed.
Step 5: Statements after the switch statement are executed.
Program to illustrate the use of switch statement
#include <stdio.h>
int main()
{
// variable to be used in switch statement
int var = 2;
// declaring switch cases
switch (var) {
case 1:
printf("Case 1 is executed");
break;
case 2:
printf("Case 2 is executed");
break;
default:
printf("Default Case is executed");
break; }
return 0;
}
Program to illustrate the use of switch statement
#include <stdio.h>
int main() {
int num = 8;
switch (num) {
case 7:
printf("Value is 7");
break;
case 8:
printf("Value is 8");
break;
case 9:
printf("Value is 9");
break;
default:
printf("Out of range");
break;
}
return 0;
}
Program to illustrate the use of switch statement

#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 find whether a given year is a leap year or


not.
• Write a C program to check whether a given number is positive or negative.

• Write a C program to accept two integers and check whether they are equal
or not.

You might also like