Decision making and looping

anuroop123

NANDYALA ANUROOP

Posted on May 11, 2021

Decision making and looping

A normal program is not a sequential execution of expressions or statements one after the other. It will have certain conditions to be checked or it will have certain number of iterations.

When we have to execute particular set of instructions multiple times or known number of times (iterations), then we use FOR loops or DO/WHILE loops.

If… Else Statements
For Loops
While and Do/While Loops
The general syntax for while and do/while loops are given below :
Enter fullscreen mode Exit fullscreen mode




If… Else Statements

These are the decision making statements. It is used for checking certain condition to decide which block of code to be executed. syntax for if..else statement is

if (condition / s){
expressions / statements;
} else {
expressions / statements;
}

if (condition / s){
expressions / statements;
} else if {
expressions / statements;
} else {
expressions / statements;
}

example :
if (intDivisor == 0) {
printf ("Warning!: Divisor is Zero!!\n Please re-enter the divisor :");
scanf ("%d", &intDivisor);
}

intResult=intDividend / intDivisor;

If statement can have expressions to be executed when the condition is false. Those expressions are added in the else part of the ‘if’ statement.

if (condition / s) {
expressions / statements;
} else {
expressions / statements;
}

Example :
if (intVal >= 0)
printf ("You entered a Positive Number!");
else
printf ("You entered a Negative Number!");

We can have if statement within another if statement, i.e.; we can have nested if statements.
e.g.: –
if (intVal >= 0)
if (intVal == 0)
printf ("You entered a Zero!");
else
printf ("You entered a Positive Number!");
else
printf ("You entered a Negative Number!");

For Loops:

A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.

Syntax:

for (intial_value; condition; increment_factor) {
statement/s;
}

Example:

void main () {
int intNum;
printf ("\n First 15 natural numbers are: \n");
for (intNum = 0; intNum < 15; intNum++) {
printf ("%d ", intNum);
}
}

While loop:

A while loop in C programming repeatedly executes a target statement as long as a given condition is true.
Syntax:
while (condition/s){
Expression / statements;
}

Example:

void main() {
int intNum = 0;
printf("\n Example of WHILE Loop\n");
printf("First 15 natural numbers are: \n");
while (intNum < 15){
printf("%d ", intNum);
intNum++;

Do/While Loops:

The body of do...while loop is executed once. · If the test expression is true, the body of the loop is executed again and the test expression is evaluated
Syntax:

do{
Expression / statements;
} while (condition/s);

Example:
{
printf("\n Example of DO/WHILE Loop\n");
printf("First 15 natural numbers in descending order is: \n");
while (intNum >=0){
printf("%d ", intNum--);

}

💖 💪 🙅 🚩
anuroop123
NANDYALA ANUROOP

Posted on May 11, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Decision making and looping
loops Decision making and looping

May 11, 2021