0% found this document useful (0 votes)
6 views13 pages

ComputingLab DroneTraining2

Uploaded by

krishan pal
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)
6 views13 pages

ComputingLab DroneTraining2

Uploaded by

krishan pal
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/ 13

Control Statements

Exercise From Previous Classes


• Write a program (WPA) to calculate the simple
interest of a bank customer who fix deposited Rs.
500000 (p) for 10 years (n) with an simple interest
rate of 7% (r). Formula for simple interest is, simple
interest = p*n*r / 100
• Vertical speed of a drone is 4 metre / sec and
horizontal speed of a drone is 5 meter / sec. A drone
first moves vertically 50 metres and then move 200
metre horizontally to reach its target. Calculate the
time needed to reach the target from the staring
point. (Soln: vertical time + horizontal time = 50/4+
200/5)
Calculate Simple Interest
#include<stdio.h>
void main()
{
int p = 500000;
int year = 10;
int rate = 7;
float interest;
interest = p*year*rate / 100;
printf("interest after 10 year is = %f \n",interest);
}
Drone Travel Time
#include<stdio.h>
void main()
{
int v_speed = 4;
Int h_speed = 5;
int v_dist = 50, h_dist = 200;
int v_time;
int h_time;
int total_time;
Drone Travel Time
v_time = 50/4;
h_time = 200/5;
total_time = v_time + h_time ;

printf("v_time=%d seconds\n h_time=%d


seconds\n",v_time,h_time);

printf("Time to reach the target is %d


seconds",total_time);
}
Control Structure
• There may be situations where the programmer
requires to alter normal flow of execution of
program or to perform the same operation a
number of times.
• Ex. If the drone has completed 50 metres of travel in
vertical direction, the movement of the drone need
to be changed into horizontal direction.
• Types of control statements:
– Decision control
– Loop control
Decision Control Statements
• To alter the normal sequential execution of the
statements of a program decision control
statements are used.
• Depending upon the test condition at a particular
point in program the flow changes.
• Types of decision control statements:
– if
– if - else
– if - else - if
– switch
If Statements
• Very simple but useful decision control
statement.
• It executes a statement or block of statements
only if the condition is true.
• Syntax:
if (test condition)
{
Satement1 ;
Satement2 ;
}
Satement3 ;
Different operator Used in C
• Arithmetic operator: +, -, *, /, %
• Assignment operator: =
• Relational operator: ! , <, >, <=, >=, ==, !=
• Logical operator: AND (&&) and LOGICAL OR (||)

• Exercise:
Enter English, History and Economics subject marks of a
student from key board. Find out the average mark of
the student. Write a program to evaluate the results for
a student applying the rule: pass marks (30+), 2nd class
(40+), 1st class(60+)
/* Program to check whether a no. is even */

Flowchart # include<stdio.h>
void main()
False
Condition {
int num;
True

Block of if printf(“enter the number”);


scanf(“%d”,&num)
Next statement
if (num % 2 == 0)

STOP {
printf(“\n Number is even”);
}
}
if – else statement
• If specific statements are to be executed in both
cases (either condition is true or false) then if –
else statement is used.
• Syntax: if (condition)
{
statement 1;
False
statement 2; Test
Condition
}
else True

{ Block of if Block of else

statement 3; Next statement


}
STOP
if- else - if
• Useful for a program involving multiple conditions.
• Syntax: Start

• if (condition1)
statement1; false
condition 1
else if(condition2) false
condition 2
statement2; true
false
else if(condition3) Statement 1 true condition 3

statement 3; Statement 2 true


else Statement 3

default statement;
Default statement

Next statement
Switch statement
• Switch is a multi-way decision making statement which
selects one of the several alternatives based on the
value of single variable or expression.
• It is mainly used to replace multiple if-else-if statement.
• Syntax:
switch (expression)
{ case constant1 :
statement(s);
break;
case constant2 :
statement(s);
break;
… …. …
default: statement(s);
}

You might also like