0% found this document useful (0 votes)
7 views40 pages

Control Structures - II

Uploaded by

janirubro
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views40 pages

Control Structures - II

Uploaded by

janirubro
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

Programming

Fundamentals
Lecture 06 :
Control Structures

2
Relational Operators
Operator Sign Description Example
Greater than > Returns true if left num > 10
hand side value is
greater than right
hand side value
Less than < Return true if left num <10
hand side value is less
than right hand side
value
Relational Operators contd..
Operator Sign Description Example
Greater than or >= Returns true if left num >= 10
equal hand side value is
greater than or equal
right hand side value
Less than or <= Return true if left num <=10
equal hand side value is less
than or equal right
hand side value
Relational Operators contd..
Operator Sign Description Example
Equals == Returns true if left num = = 10
hand side value is
equal right hand side
value
Not equal != Return true if left num !=10
hand side value is not
equal right hand side
value
Activity - Code
#include<stdio.h>
int main()
{
int num1,num2,num3,tot=0;
float avg=0;
printf("Enter number 1:");
scanf("%d",&num1);
printf("Enter number 2:");
scanf("%d",&num2);
printf("Enter number 3:");
scanf("%d",&num3);
tot=num1+num2+num3;
avg=(float)tot/3; //converting int data type into float data type to calculate the average
printf("Total is %d\n",tot);
printf("Total is %.2f\n",avg);
return 0;
}
Control Structures
Control structures
➢Used to control the flow of execution in a program.

➢Allow a programmer to specify conditions that


determine which statements are executed and which
are skipped, to loop through statements until a
condition is met, or to jump to a different part of the
program.
Control structures
➢There are three types of control statements in C.
➢Selection structure / Conditional statements
➢Repetition structure / Loop statements
➢Jump statements
Selection structure
if statements
➢The primary tool of selection structure.

➢if is used when we want a single or a group of statements


to be executed if the condition is true.

➢If the condition is false, the statement is skipped, and


program continues by executing the first statement after
the if selection structure.
if statements – function illustrations

False
condition

True

True statement

Next statement
Selection structure
“ if ” Syntax
if ( condition )
{
// true statement to be executed
}
**If statement enable a programmer to execute a set of statements when the
result of a certain condition evaluates to be proved , the statements inside if
block will be executed only if the result of the condition is true.
Example
Write a C program to accept the age of a user and
display a message as below.

if age > =18 – “You are eligible to vote”


Example
#include<stdio.h>
int main() Output – when condition is true
{
int age;
printf("Enter your age :");
scanf("%d",&age);
if(age>=18) Output – when condition is false
{
printf("You are eligible to vote\n");
}
return 0;
}
Exercise - 1
Write a C program to accept the mark from the
student and display a message,
“Congratulations..!!You have passed the exam” if
the marks >=75.
Exercise – 1 - Code
#include<stdio.h>
int main()
{
int mark; Output – when condition is true
printf("Enter student's mark :");
scanf("%d",&mark);
if(mark>=75)
{
printf("Congratulations!! You have passed the exam\n");
}
return 0;
}
Exercise - 2
Write a C program to asks the user to input a
number between -5 to 10.
Then if user enter positive number display a
message as “Positive number.”
Exercise - 2
#include<stdio.h>
int main() Output – when condition is true
{
printf("...Enter a number between -5 t0 10...\n");
int num;
printf("Enter the number :");
scanf("%d",&num);
if(num>0)
{
printf("Positive number\n");
}
return 0;
}
Exercise - 3
Write a C program to accept the bill amount from the user
and calculate the discount amount and total bill as follows.
if bill amount >=10000 , 10% will be calculated;
discount amount = bill amount * 0.1
total bill = bill amount – discount amount
#include<stdio.h>
Exercise – 3 - Code
int main()
{
float bill_amount,dis_amount,total_bill=0;
printf("Enter bill amount :");
scanf("%f",&bill_amount);
if(bill_amount>=10000)
{
dis_amount=bill_amount*0.1;
} Output – when condition is true
total_bill=bill_amount-dis_amount;
printf("Your discount is %.2f\n",dis_amount);
printf("Your total bill amount is %.2f\n",total_bill);

return 0;
}
Selection structure
if – else statements
➢The if-else statement is used to perform two operations
for a single condition.
▪One is for the correctness of that condition,
▪Other is for the incorrectness of the condition.

➢Here, we must notice that if and else block cannot be


executed simultaneously.
Selection structure
“ if else ” Syntax
if ( condition )
{
// true statement to be executed
}
else
{
// false statement to be executed
}
if – else statements – function illustrations

True
condition True statement

False

False statement
Example
Write a C program to accept the age of a user and
display a message as below.

if age > =18 – “You are eligible to vote”


if age < 18 – “Try again next time”
#include<stdio.h>
int main()
Output – when condition is true
{
int age;
printf("Enter your age :");
scanf("%d",&age);
if(age>=18)
{
printf("You are eligible to vote\n");
}
else Output – when condition is false
{
printf("Try again next time\n");
}
return 0;
}
Exercise 1
Write a C program to accept the mark of a
student and display message as follows.

if mark> =50 – “Good luck..You have passed


the exam”
if mark < 50 – “Bad luck..Try again next
time”
#include<stdio.h>
Exercise 1 - Code
int main()
{
int mark;
printf("Enter mark :");
scanf("%d",&mark);
if(mark>=50)
{
printf("Good luck..You have passed the exam\n");
}
else
{
printf("Bad luck..Try again next time\n");
}
return 0;
}
Exercise 2
Write a C program to accept the a number from
the user and display whether it is positive or
negative.
Exercise 2 - Code
#include<stdio.h>
int main()
{
int num;
printf("Enter the number :");
scanf("%d",&num);
if(num>0)
{
printf("Positive number\n");
}
else
{
printf("Negative number\n");
}
return 0;
}
Exercise 3
Write a C program to accept two numbers from
the user and display highest among them.
#include<stdio.h>
Exercise 3 - Code
int main()
{
int num1,num2;
printf("Enter first number :");
scanf("%d",&num1);
printf("Enter second number :");
scanf("%d",&num2);
if(num1>num2)
{ Output
printf("%d is the highest value\n",num1);
}
else
{
printf("%d is the highest value\n",num2);
}
return 0;
}
Exercise 4
Write a C program to accept the basic salary and no of OT
hours from the user to calculate and display gross salary and
net salary based on following.
If no of OT hours>=30 ; rate per OT hour - 1500
If no of hours<30 ; rate per OT hour – 1000
Gross salary = Basic Salary+(OT hours*rate)
Net salary = Gross Salary-(Gross Salary*0.1)
#include<stdio.h>
int main()
{
int OT;
float b_slry,g_slry=0,n_slry=0;
printf("Enter your basic salary:");
scanf("%f",&b_slry);
printf("Enter OT hours:");
scanf("%d",&OT);
if (OT>=30)
{
g_slry=b_slry+(OT*1500);
}
else
{
g_slry=b_slry+(OT*1000);
}
n_slry = g_slry-(g_slry*0.1);
printf("The gross salary :%.2f\n",g_slry);
printf("The net salary :%.2f\n",n_slry);
return 0;
}
#include<stdio.h>
Exercise 4 - Code
int main()
{
int OT;
float basic_slry,gross_slry,net_slry;
printf("Enter basic salary :");
scanf("%f",&basic_slry);
printf("Enter OT hours worked :");
scanf("%d",&OT);
if(OT>=30)
{
gross_slry=basic_slry+(OT*1500);
}
else
{ Output
gross_slry=basic_slry+(OT*1000);
}
net_slry=gross_slry-(gross_slry*0.1);
printf("Your gross salary is %.2f\n",gross_slry);
printf("Your net salary is %.2f\n",net_slry);
return 0;
}
if-else-if statement
➢If-else-if statements are used when it is required to execute
statements belongs to only one condition from multiple
conditions.
➢The statements will start to test the condition from top.
➢The condition after first condition will be tested only if the
result of the previous condition is false.
➢Therefore if-else-if is considered as a ladder statement.
➢If non of the conditions evaluated to be true , the else block
at the end of the statement will be executed.
“ if – else - if ” Syntax
if(condition_1)
{
// statement to be excuted when condition_1 is true
}
else if(condition_2)
{
// statement to be excuted when condition_2 is true
}
else if(condition_3)
{
// statement to be excuted when condition_3 is true
}
else
{
//statement to be executed when all the conditions are false
}
#include<stdio.h>
Example
int main()
{
int num;
printf("Enter a number :");
scanf("%d",&num);
if(num==0)
{
printf("You have entered 0\n");
}
else if (num>0)
{
printf("You have entered a positive number\n");
}
else
{
printf("You have entered a negative number\n");
}

return 0;
}
Exercise
Write a C program to take the marks of IT , English
and Maths of a student and calculate the total and
average.
If average is ;
> = 75 – A
>=65 – B
>=55 – C
>=40 – S
<40 - F
Exercise - Code
#include<stdio.h> if (avg>=75)
{
int main()
printf("A\n");
{
}
int IT,Maths,English,tot=0; else if (avg>=65)
float avg=0; {
printf("Enter IT marks :"); printf("B\n");

scanf("%d",&IT); }
else if (avg>=55)
printf("Enter Maths marks :");
{
scanf("%d",&Maths);
printf("C\n");
printf("Enter English marks :"); }
scanf("%d",&English); else if (avg>=40)
tot=IT+Maths+English; {

avg=(float)tot/3; printf("S\n");
}
printf("Total marks = %d\n",tot);
else
printf("Average = %.2f\n",avg);
{
printf("F\n");
}
return 0;
}
Control structures
• Any questions before finish?
• Are you sure?
• Thank you for listening.

40

You might also like