[go: up one dir, main page]

0% found this document useful (0 votes)
24 views24 pages

PL1 Lecture 5 Conditional Statement and Loops

Uploaded by

safwanxd93
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)
24 views24 pages

PL1 Lecture 5 Conditional Statement and Loops

Uploaded by

safwanxd93
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/ 24

Conditional Statement and Loops

Course Title: Programming language 1

Dept. of Computer Science


Faculty of Science and Technology

Lecturer No: 05 Week No: 6 Semester: Fall 23-24


Lecturer: Shaikat Das Joy; skdas@aiub.edu
Lecture Outline

 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.

There are 3 types of decision-making control


statements in C language. They are,

 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.

Sample input and output:

Input: Enter an integer: -2


Output: You entered -2. It is a negative number.
The if statement is easy.

Input: Enter an integer: 5


Output: You entered 5. It is a positive number.
The if statement is easy.
Solution
#include <stdio.h>
int main()
{
int number;

printf("Enter an integer:
"); scanf("%d", &number);

// Test expression is true if number is less


than 0
if (number < 0)
{
printf("You entered %d. It is a negative
number. ", number);
}
else
{
printf("You entered %d. It is a positive
number. ", 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.

Sample input and output:

Input: Enter an integer: 7


Output: 7 is an odd integer.

Input: Enter an integer: 10


Output: 10 is an odd integer.
Solution
#include <stdio.h>
int main()
{
int number;
printf("Enter an integer: ");
scanf("%d",&number);

// 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.

Sample input and output:

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;

printf("Enter three numbers: ");


scanf("%d%d%d", &num1, &num2, &num3);

if(num1 > num2)


{
if(num1 > num3)
{
maximum = num1;
}
else
{
maximum = num3;
}
}
else
{
if(num2 > num3)
{
maximum = num2;
}
else
{
maximum = num3;
}
}

printf("Maximum among all three numbers = %d", maximum);


return 0;
}
LOTS OF EXAMPLES!!!!!!!!!!!!!!!!! 

http://www.codeforwin.in/2015/05/if-else-programming-
practice.html
PROGRAMMING WITH DECISION CONTROL
STATEMENT

Write a program to check whether a given year is leap year or not.

Sample input and output:


Input
Input year: 2004 Output
2004 is leap year..

Hint:

• If the year is exactly divisible by


4 and not divisible by 100, then
it is leap year.
• Else if the year is exactly
divisible by 400 then it is leap
PROGRAMMING WITH DECISION CONTROL
STATEMENT
#include <stdio.h>
int main(){
int year;
/* Read year from user */
printf("Enter year : ");
scanf("%d", &year);

Solution: /* Check for leap year conditions */


if(((year % 4 == 0) && (year % 100 !=0)) || (year % 400==0))
{
printf("LEAP YEAR");
}
else
{
printf("COMMON YEAR");
}
return 0;
}
PROGRAMMING WITH DECISION CONTROL
STATEMENT
Write a program that will take the total marks as input (marks must be between
1 and 100) and output the corresponding Grade. Only the following grades are
considered:

80 -100: A+
79 – 60: A
59 – 40: B
39 – 30: C
<30: F

Sample input and output:


input: Enter your marks: 89
output: Your grade is: A+

input: Enter your marks:


120
LOOP CONTROL STATEMENT

Loop control statements in C are used to perform looping operations until


the given condition is true. Control comes out of the loop statements once
condition becomes false.

There are 3 types of loop control statements in C language.They are,


for
while
do-while
LOOP CONTROL STATEMENT
Loop Name Syntax Example
for (variable_initialization; condition_checking; for(int i = 0; i < 5; i++)
varibale_increment / variable_decrement) {
for { printf(“this is for loop”);
statements; printf(“i= %d”, i);
} }
varibale_initialization; int i=0;
while (condition_checking) while(i < 5)
{ {
while statements; printf(“this is while loop”);
varibale_increment / variable_decrement; printf(“i= %d”, i);
} i++;
}
int i = 0;
varibale_initialization; do{
do { printf(“this is do while loop”);
do while statements; printf(“i= %d”, i);
varibale_increment / variable_decrement; i++;
}while (condition_checking); } while(i < 5);
FOR LOOP
#include <stdio.h>

int main()
{
int i;

for(i=0; i<10; i++)


{
printf("%d \t",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

You might also like