CSDT BLOG

DISCOVER COLLECTIONS AND BLOGS THAT MATCH YOUR INTERESTS.




Share ⇓




Why control flow or decision making statements are important in programming language?

Bookmark

Why control flow or decision making statements are important in programming language?

Describe Control flow statement

About control flow statements:- 

The statements inside your source files or programs are generally executed from top to bottom, in the order that they appear. Control flow statements, however, break up the flow of execution by employing decision making, looping, and branching, enabling your program to conditionally execute particular blocks of code.

1. if else statement
The else statement specifies a block of code to be executed if the condition is false: 
if (condition) 
{ // block of code to be executed if the condition is true. } 
else 
{ // block of code to be executed if the condition is false.}

for example:- 

#include<stdio.h>
#include<conio.h>

void main()
{
int num;
printf("Enter a number for checking Even or odd number\n");
scanf("%d",&num);
if(num%2==0)
{
printf("Number is even:%d",num);
}
else
{
printf("Number is Odd:%d",num);
}
getch();
}

2. if else ladder
The if-else-if Ladder is a common programming construct that is based upon nested ifs is the if-else-if ladder. It looks like this. The conditional expressions are evaluated from the top downward. As soon as a true condition is found, the statement associated with it is executed, and the rest of the ladder is bypassed.
 
Syntax:- 
if(condition 1)
         {//true Statements}
else if(condition 2)
{
// true Statements
}
else if(condition 3)
{
// true Statements
}
else if(condition 4)
{
// true Statements
}
else if(condition 5)
{
// true Statements
}
else
{
//False Statements
}

For Example:

#include<stdio.h>
#include<conio.h>

void main()
{
int eng,math,hindi,total,per;
printf("Enter a 3 subjects marks\n");
scanf("%d%d%d",&eng,&math,&hindi);
total=eng math hindi;
per=total/3;
if(per>=75)
{
printf("Grade A and percentage is:%d",per);
}
else if(per>=60)
{
printf("Grade B and percentage is:%d",per);
}
else if(per>=40)
{
printf("Grade C and percentage is:%d",per);
}
else
{
printf("Sorry Fail bcz percentage is:%d",per);
}
getch();
}

3.switch statement
Switch statement is a control statement that allows us to choose only one choice among the many given choices. The expression in switch evaluates to return an integral value, which is then compared to the values present in different cases. It executes that block of code which matches the case value.

Syntax:

switch(expression)
{
case value1:
statement1;
break;

case value2:
statement2;
break;

case value3:
statement3;
break;

case value4:
statement4;
break;

case value5:
statement5;
break;

:
:
:
:
:
:
case valueN:
statementN;
break;

default:
statement;

}
1

Our Recent Coment