By
Dr. Kanchan V. Shende
Module 2 : Decision Making and Loop
Contents:
Decision Making structure
– if statement
– if-else statement
– Nested if-else statement
– conditional operator
– switch statements
Loop control structures
– while loop
– do-while loop
– for loop
– Nested loops
Jump statements -break, continue, goto, exit
05.09.23 Module 2 2
Decision Making in C
• There some situations in real life when we need to make some decisions and
based on these decisions, we decide what should we do next.
• Similar situations arise in programming also where we need to make some
decisions and based on these decisions we will execute the next block of
code.
• For example:
– if x occurs then execute y else execute z.
– if x occurs then execute p, else if condition y occurs execute q, else
execute r.
05.09.23 Module 2 3
Conti..
05.09.23 Module 2 4
if statement in C
● Most simple decision-making statement.
● Used to decide whether a certain statement or block of statements will be
executed or not
i.e if a certain condition is true then a block of statement is executed otherwise
not.
05.09.23 Module 2 5
Example: C program to illustrate ‘if statement’
05.09.23 Module 2 6
if-else in C
† The if statement alone tells us that if a condition is true it will execute a
block of statements and if the condition is false it won’t.
† What if we want to do something else if the condition is false.
† Here comes the C else statement.
† We can use the else statement with the if statement to execute a block of
code when the condition is false.
05.09.23 Module 2 7
Example: C program to illustrate if-else statement
05.09.23 Module 2 8
nested-if in C
† A nested if in C is an if statement that is the target of another if statement.
† Nested if statements mean an if statement inside another if statement.
† Yes, both C and C++ allow us to nested if statements within if statements,
i.e, we can place an if statement inside another if statement.
05.09.23 Module 2 9
Example: C program to illustrate nested-if statement
05.09.23 Module 2 10
if-else-if in C
† Here, a user can decide among multiple options.
† The C if statements are executed from the top down.
† As soon as one of the conditions controlling the if is true, the statement
associated with that if is executed, and the rest of the C else-if ladder is
bypassed.
† If none of the conditions is true, then the final else statement will be executed.
05.09.23 Module 2 11
Example: C program to illustrate if-else-if statement
05.09.23 Module 2 12
Switch Statement
† The switch statement allows us to execute one code block among many alternatives.
† You can do the same thing with the if...else..if ladder. However, the syntax of the
switch statement is much easier to read and write.
05.09.23 Module 2 13
Rules for switch statement
◦ An expression must always execute to a result.
◦ Case labels must be constants and unique.
◦ Case labels must end with a colon ( : ).
◦ A break keyword must be present in each case.
◦ There can be only one default label.
◦ We can nest multiple switch statements.
05.09.23 Module 2 14
Why do we need a Switch case?
† There is one potential problem with the if-else statement which is the
complexity of the program increases whenever the number of alternative path
increases.
† If you use multiple if-else constructs in the program, a program might
become difficult to read and comprehend. Sometimes it may even confuse the
developer who himself wrote the program.
† The solution to this problem is the switch statement.
05.09.23 Module 2 15
Example: Simple Calculator
// Program to create a simple calculator
#include <stdio.h>
int main() {
char operation;
double n1, n2;
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &operation);
printf("Enter two operands: ");
scanf("%lf %lf",&n1, &n2);
switch(operation)
case '+':
printf("%.1lf + %.1lf = %.1lf",n1, n2, n1+n2);
break;
05.09.23 Module 2 16
Conti....
case '-':
Output
printf("%.1lf - %.1lf = %.1lf",n1, n2, n1-n2); Enter an operator (+, -, *, /): -
break; Enter two operands: 32.5
12.4
case '*': 32.5 - 12.4 = 20.1
printf("%.1lf * %.1lf = %.1lf",n1, n2, n1*n2);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf",n1, n2, n1/n2);
break;
// operator doesn't match any case constant +, -, *, /
default:
printf("Error! operator is not correct");
return 0;
}
05.09.23 Module 2 17
Switch vs if-else
05.09.23 Module 2 18
Loop Control Structure
† Repeating the same process multiple times until a specific condition
satisfies.
Why use loops in C language?
† The looping simplifies the complex problems into the easy ones.
† It enables us to alter the flow of the program so that instead of writing the
same code again and again, we can repeat the same code for a finite number
of times.
• For example, if we need to print the first 10 natural numbers then, instead of
using the printf statement 10 times, we can print inside a loop which runs up
to 10 iterations.
05.09.23 Module 2 19
Advantage of loops in C
† Provides code reusability.
† Using loops, we do not need to write the same code again and again.
† U sing loops, we can traverse over the elements of data structures (array or
linked lists).
Types of C Loops
There are three types of loops in C language that is given below:
1. do while
2. while
3. for
05.09.23 Module 2 20
do-while
The do-while loop continues until a given condition satisfies.
• It is also called post tested loop.
• It is used when it is necessary to execute the loop at least once (mostly menu
driven programs).
Syntax:
do{
//code to be executed
}while(condition);
05.09.23 Module 2 21
Conti....
Example:
#include<stdio.h>
#include<stdlib.h>
void main ()
char c;
int choice,dummy;
do{
printf("\n1. Print Hello\n2. Print Javatpoint\n3. Exit\n");
scanf("%d",&choice);
switch(choice)
{
05.09.23 Module 2 22
Conti...
case 1 :
printf("Hello");
break;
case 2:
printf("Javatpoint");
break;
case 3:
exit(0);
break;
default:
05.09.23 Module 2 23
Conti..
printf("please enter valid choice"); Output
1. Print Hello
} 2. Print Javatpoint
3. Exit
printf("do you want to enter more?"); 1
Hello
do you want to enter more?
scanf("%d",&dummy);
y
1. Print Hello
scanf("%c",&c); 2. Print Javatpoint
3. Exit
}while(c=='y'); 2
Javatpoint
} do you want to enter more?
n
05.09.23 Module 2 24
while loop
The while loop in c is to be used in the scenario where we don't know the
number of iterations in advance.
• The block of statements is executed in the while loop until the condition
specified in the while loop is satisfied.
• It is also called a pre-tested loop.
Syntax:
while(condition){
//code to be executed
}
05.09.23 Module 2 25
Example:
#include<stdio.h>
int main(){
int i=1;
while(i<=10){
printf("%d \n",i);
i++;
return 0;
}
05.09.23 Module 2 26