PL1 Lecture 5 Conditional Statement and Loops
PL1 Lecture 5 Conditional Statement and Loops
List of topics,
Control statement
if else
Nested if else
Loops
DECISION CONTROL STATEMENT
In decision control statements (if-else and nested
if),
group of statements are executed when
condition is true.
If condition is false, then else part
statements are executed.
if statements
if else statements
nested if statements
DECISION CONTROL STATEMENT
Decision control Syntax/Description
statements
if Syntax:
if (condition)
{ Statements; }
Description: In these type of statements, if condition is true, then respective block
of code is executed.
if…else Syntax:
if (condition) { Statement1; }
else { Statement2;}
Description: In these type of statements, group of statements are executed when
condition is true. If condition is false, then else part statements are executed.
nested if Syntax:
if (condition1) { Statement1; }
else if(condition2) { Statement2; }
else { Statement 3; }
Description: If condition 1 is false, then condition 2 is checked and statements are
executed if it is true. If condition 2 also gets failure, then else part is executed.
IF STATEMENT
int main()
{
int m=40,n=40;
if (m == n)
{ Output:
printf("m and n are m and n are equal
equal\n"); after if statement
}
printf(“after if
statement”);
}
IF-ELSESTATEMENT
#include <stdio.h>
int main()
{
int m=40,n=20;
if (m == n)
{
Output:
printf("m and n are equal\n");
m and n are not equal
}
after if-else block
else
{
printf("m and n are not equal\n");
}
printf(“after if-else block”);
}
NESTED IF-ELSESTATEMENT
int main()
{
int m=40,n=20;
if (m>n)
{
printf("m is greater than n\n");
}
else if(m<n) Output:
{ m is greater than n
printf("m is less than n\n"); after if-else block
}
else
{
printf("m is equal to n\n");
}
printf(“after if-else block”);
}
Class Task
Write a program to test whether a number is negative or positive.
printf("Enter an integer:
"); scanf("%d", &number);
printf("The if statement is
easy.");
Class Task
Write a program to check whether an integer entered by the user is
odd or even.
// True if remainder is 0
if( number%2 == 0 )
{
printf("%d is an even integer.",number);
}
else
{
printf("%d is an odd integer.",number);
}
return 0;
}
Class Task
Write a program to find the maximum among three integers.
Input:
Input num 1: 10
Input num 2: 20
Input num 3: 15
Output:
Maximum is: 20
Solution
#include <stdio.h>
int main()
{
int num1, num2, num3, maximum;
http://www.codeforwin.in/2015/05/if-else-programming-
practice.html
PROGRAMMING WITH DECISION CONTROL
STATEMENT
Hint:
80 -100: A+
79 – 60: A
59 – 40: B
39 – 30: C
<30: F
int main()
{
int i;
Output: 0 1 2 3 4 5 6 7 8 9
WHILE LOOP
#include <stdio.h>
int main()
{
int i=3;
while(i<10)
{
printf("%d \t",i);
i++;
}
return 0;
}
Output: 3 4 5 6 7 8 9
DO WHILE LOOP
#include <stdio.h>
int main()
{
int i=1;
Output:
do Value of i is 1
{ Value of i is 2
printf("Value of i is %d\n",i); Value of i is 3
i++;
}while(i<=4 && i>=2);
Value of i is 4
}
DO WHILE LOOP
while do while
Loop is executed for first
Loop is executed only time irrespective of the
when condition is true.
condition. After executing
while loop for first time,
then condition is checked.
NESTED FOR LOOP
Loop inside a loop, is called nested loops…
#include <stdio.h>
How many nested for loop can you have?
int main() Only one
{
int i,j; Which of the loops in the nested for loop will
be executed most?
for(i=0;i<=5;i++)
The inner loop
{
printf("i= %d -----------\n", i); i= 0 ---------------------------------------
j= 5 j= 4 j= 3 j= 2 j= 1 j= 0
for(j=5;j>=0;j--) i= 1 ---------------------------------------
{ j= 5 j= 4 j= 3 j= 2 j= 1 j= 0
printf("j= %d \t", j); i= 2 ---------------------------------------
} j= 5 j= 4 j= 3 j= 2 j= 1 j= 0
printf("\n"); i= 3 ---------------------------------------
} j= 5 j= 4 j= 3 j= 2 j= 1 j= 0
i= 4 ---------------------------------------
} j= 5 j= 4 j= 3 j= 2 j= 1 j= 0