L3-Managing Input - If Else - Looping - PGD!
L3-Managing Input - If Else - Looping - PGD!
AND
CONTROL STATEMENTS
Md. Ariful Islam Khandaker
Assistant Professor, IICT
Khulna University of Engineering & Technology.
INPUT / OUTPUT OPERATOR
❑ All input/output operations are carried out through functions called as
printf and scanf.
❑ There exist several functions that have become standard for input and
output operations in C. These functions are collectively known as
standard i/o library.
Each program that uses standard I/O function must contain the statement
6/27/2024
Md. Ariful Islam Khandaker, Assistant Professor, IICT, KUET.
SAMPLE EXAMPLE
Problem: Create a program which will
calculate monthly salary of an employee
according to the following conditions.
1. Basic Salary
2. House Rent(45% of Basic Salary)
3. Medical Allowance(10% of Basic Salary)
4. Technical Allowance(5% of Basic Salary)
5. Tax payment(5% of Basic Salary)
6/27/2024
Md. Ariful Islam Khandaker, Assistant Professor, IICT, KUET.
SOLUTION OF THE PROBLEM
#include <stdio.h>
#include <conio.h>
int main( ) {
float basic, hr, ma, ta, tax, salary;
printf (“ Enter Basic Salary\n”);
scanf ( “%f”, &basic);
hr = basic * .45;
ma = basic * .10;
ta = basic * .05;
tax = basic * .05;
salary = (basic + hr + ma + ta) – tax;
printf ( “ Monthly Salary is = %f “, salary );
return 0;
}
6/27/2024
Md. Ariful Islam Khandaker, Assistant Professor, IICT, KUET.
READING AND WRITING A CHARACTER
❑ Reading a single character can be done by using the function getchar( ). The
getchar ( ) takes the following form:
variable_name = getchar( );
/*Reading a character from terminal*/
#include<stdio.h>
int main( )
{
char ans;
ans = getchar( );
putchar (variable_name);
/*A program to read a character from keyboard and then prints it in reverse case*/
#include<stdio.h>
OUTPUT
#include<ctype.h> Enter An alphabet
int main() { a
char alphabet; A
printf(“Enter an alphabet”); Enter An alphabet
putchar(‘\n’); Q
alphabet = getchar(); q
if ( islower ( alphabet ) ) { Enter An alphabet
putchar ( toupper ( alphabet ) ); } z
Z
else {
putchar ( tolower ( alphabet ) ); }
return 0; }
Md. Ariful Islam Khandaker, Assistant Professor, IICT, KUET. 6/27/2024
FORMATTED INPUT
Formatted input refers to an input data that has been arranged in a particular
format.
The general form of scanf is
%wd
Unlike integer numbers, the field width of real numbers is not to be specified.
%ws or %wc
%wd
Output of Real Numbers
The output of real numbers may be displayed in decimal notation using the
following format specification:
%w.p f
The integer w indicates the minimum number of positions that are to be
used for the display of the value and the integer p indicates the number of
digits to be displayed after the decimal point.
We can also display real numbers in exponential notation by using the
specification
%w.p e
Md. Ariful Islam Khandaker, Assistant Professor, IICT, KUET. 6/27/2024
FORMATTED OUTPUT
Format Output
printf ( “ %d “ , 9876 ); 9 8 7 6
printf ( “ %6d “ , 9876 ); 9 8 7 6
printf ( “ %2d “ , 9876 ); 9 8 7 6
printf ( “ %-6d “ , 9876 ); 9 8 7 6
Format Output
printf ( “ %7.4f “ , 98.7654 ); 9 8 . 7 6 5 4
printf ( “ %7.2f “ , 98.7654 ); 9 8 . 7 7
printf ( “ %-7.2f “ , 98.7654); 9 8 . 7 7
6/27/2024
Md. Ariful Islam Khandaker, Assistant Professor, IICT, KUET.
CONDITIONAL STATEMENTS
IF STATEMENT
The if statement is used to control the flow of execution of statements and is
of the form
if(test expression)
It allows the computer to evaluate the expression first and then, depending on
whether the value of the expression is ‘true’ or ‘false’, it transfers the control to
a particular statement.
Entry
Test
Exprs
Flase
True
6/27/2024
Md. Ariful Islam Khandaker, Assistant Professor, IICT, KUET.
CONDITIONAL STATEMENTS (CONT…)
The if statement may be implemented in different forms depending on the
complexity of conditions to be tested.
1. Simple if statement
2. if…..else statement
3. Nested if…..else statement
4. elseif ladder
❑ SIMPLE IF STATEMENT
If the test expression is true, the statement-block will be executed;
otherwise the statement-block will be skipped and the execution will jump
to the statement-x.
if(category = = SPORTS)
if(test exprn)
{
{
marks = marks + bonus_marks;
statement-block;
}
}
printf(“%f “,marks);
statement-x; ……….
……….
6/27/2024
Md. Ariful Islam Khandaker, Assistant Professor, IICT, KUET.
CONDITIONAL STATEMENTS (CONT…)
❑ THE IF…ELSE STATEMENT
The if….else statement is an extension of simple if statement. The general form is
if ( test expression )
{
True-block statement(s)
}
else
{
False-block statement(s)
}
statement-x
If the test expression is true, then the true block statements are executed;
otherwise the false block statement will be executed.
6/27/2024
Md. Ariful Islam Khandaker, Assistant Professor, IICT, KUET.
EXAMPLE OF IF AND IF…ELSE STATEMENT
#include <stdio.h> #include <stdio.h>
6/27/2024
Md. Ariful Islam Khandaker, Assistant Professor, IICT, KUET.
NESTING OF IF…..ELSE STATEMENTS
When a series of decisions are involved, we may have to use more than one
if….else statements, in nested form as follows.
if ( test condition 1) {
if ( test condition 2) {
statement 1;
}
else {
statement 3;
}
}
else {
statement 2;
}
6/27/2024
Md. Ariful Islam Khandaker, Assistant Professor, IICT, KUET.
EXAMPLE OF NESTED IF…ELSE
/*Selecting the largest of three values*/
int main( ) {
float A, B, C;
printf(“Enter three values \n”); OUTPUT
scanf( “ %f %f %f ”, &A, &B, &C);
printf(“\nLargest value is\n:”); Enter three values:
if(A > B) False
{ 5 8 24
if (A > C)
printf(“%f \n”,A); Largest value is
else
printf(“%f \n”,C); 24
}
else True
{
if (C > B) True
printf(“%f \n”,C);
else
printf(“%f \n”,B);
}
return 0; }
6/27/2024 Md. Ariful Islam Khandaker, Assistant Professor, IICT, KUET.
ELSE IF LADDER
The general form is
if ( condition 1) {
statement 1;
}
else if ( condition 2) {
statement 2;
}
else if ( condition 3) {
statement 3;
}
…………………………….
……………………………..
else if ( condition n) {
statement n;
}
else {
default statement;
}
statement-x;
6/27/2024
Md. Ariful Islam Khandaker, Assistant Professor, IICT, KUET.
COMPARISON BETWEEN IF…ELSE AND ELSEIF LADDER
#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
int main( ) { int main(){
int mark; float mark;
printf(“ Enter Mark\n”);
scanf(“%d”, &mark); printf("Enter Mark\n");
if(mark>=80 && mark<=100) { scanf("%f",&mark);
printf( “Your Grade is=%s”, “A+”); } if(mark>=80 && mark<=100){
if(mark>=70 && mark<=79) { printf("Your Grade is=%s", "A+"); }
printf( “Your Grade is=%s”, “B+”); } else if(mark>=70){
if(mark>=60 && mark<=69) { printf("Your Grade is=%s", "B+"); }
printf( “Your Grade is=%s”, “B”); } else if(mark>=60){
if(mark>=50 && mark<=59) { printf("Your Grade is=%s", "B"); }
printf( “Your Grade is=%s”, “C+”); } else if(mark>=50){
if(mark>=0 &&mark<=49) { printf("Your Grade is=%s", "C+"); }
printf( “Your Grade is=%s”, “F”); } else {
return 0; printf("Your Grade is=%s", "F"); }
} return 0;
}
The goto requires a label in order to identify the place where the branch is to
be made.
A label is any valid variable name and must be followed by a colon.
A program loop therefore consists of two segments, one known as the body
of the loop and the other known as the control statements.
The C language provides for three loop constructs for performing loop
operations. They are
for loop and while loop are entry controlled loop and do…while loop is exit
controlled loop.
#include<stdio.h> OUTPUT
Now it is your task to make it dynamic that is, how many time
“KUET” will print, it will depend on users.
Md. Ariful Islam Khandaker, Assistant Professor, IICT, KUET. 6/27/2024
SPOT TEST
Create a program that will print n number to m numbers with increment
by 5.
Example:
Enter start number
100
Enter end number
120
It will display
100
105
110
115
120
Idea:
for(i=1; i<=n;i++)
{
f = f * i;
}
printf("Factorial of %d is %d",n, f);
return 0;
}
❑ 12 + 22 + 32 + 42 +……………………………………………….. + n2
❑ 1/12 + 1/22 + 1/32 + 1/42 + …………………………………….. + 1/n2
Example