You are on page 1of 24

ME-303

MECHATRONICS
Dr. Fakhre Alam Khan
• Micro controller 8051 Pins Description
• Arduino Board Introduction
• Arduino Programming (LED Blinking Examples)

2
Structured
 A programming technique in which a program is divided in small units called
Programming
modules or subprogram.
 Each module consists of different instructions to perform a particular task.
 This module is executed when the main program calls it.
 When main program calls a module, the control moves to the called module
temporarily.
 The control moves back to the main program after executing the
instructions of the module.

1) Sequential Structure
2) Conditional / Selective Structure
3) Iterative / Repetitive Structure
4) Function call
3
Control
Structure
 A statement used to control the flow of execution in a program is called control
structure.
 The instructions in a program can be organized in three kinds of control
structures to
control execution flow.
 The control structures are used to implement the program logic.
Types of Control Structures
a) Sequence
 In sequential structure, the statements are executed in the same order in which they
are specified in program.

Example:

Execution of Sequential Statements


Control
Structure
Types of Control Structures
b) Selection
 A selection structure selects a statement or set of statements to execute on the basis
of a condition.
 In this structure, statement or set of statements is executed when a particular
condition is true and ignored when the condition is false.
 There are different types of selection structures in C++.
 These are if, if-else, and switch.

Example:

Execution of Conditional Statements


Control
c) Repetition Structure
Types of Control Structures

 Repetition structure executes a statement or set of statements repeatedly.


 It is also known as iteration structure or loop.
 There are different types of repetition structures in C++.
 These are while, do-while and for.

Example:

Execution of Iterative Statements


Control
d) Function call Structure
Types of Control Structures

 Function call is a type of statement that moves the control to another block of code.
 The control returns back after executing all statements in the block.
 The remaining statements are executed immediately after the function call when the
control is returned.

Example:

Execution of Function Call Statements


Sequential
Programming
Write a program that prints following statements on the screen. “ Mechanical
Engineering is the mother of all technologies. ”
“ Proud to be a Pakistani. ”
Solution:
#include <iostream.h> #include
<conio.h> void main()
{
clrscr();
cout<<" Mechanical Engineering is the mother of all technologies. \n";
cout<<" Proud to be a Pakistani. ";
getch();
}

8

Relational Operators
The relational operators are used to specify conditions in programs.
 A relational operator compares two values.
 It produces result as true or false.
 The relational operators are sometimes called the conditional operators or comparison operators as they test
conditions that are either true or false.
Operator Description
> Greater than operator returns true if the value on left side of > is greater than
the value on the right side. Otherwise returns false.
< Less than operator returns true if the value on left side of < is less than the
value on right side. Otherwise returns false.
== Equal to operator returns true if the values on both sides of == are equal. Otherwise
returns false.
>= Greater than or equal to operator returns true if value on left side of >= is
greater than or equal to the value on right side. Otherwise returns false.
<= Less than or equal to operator returns true if the value on left side of <= is
less than or equal to the value on right side. Otherwise returns false.
!= The not equal to operator returns true if the value on the left side of != is not
equal to the value on the right. Otherwise returns false. 9
‘ if ’ Statement
 if is a keyword in C++ language.
 if statement is a decision-making statement.
 It is the simplest form of selection constructs.
 It is used to execute or skip a statement or set of statements by checking a condition.
 The condition is given as a relational expression.
 lf the condition is true, the statement or set of statements after if statement is
 executed.
If the condition is false, the statement or set of statements after if statement is not
Syntaxexecuted.
if (condition)
statement;

For compound (more than 1)


statements:
if (condition) Limitation:
• if statement is the simplest selection structure but it is very
{
limited in its use.
statement 1; • The statement or set of statements is executed if the condition
statement 2; is
statement N; • But
true.if the condition is false then nothing happens. 10
Write a program that inputs two numbers and finds if second number is square of first.
Solution:
#include <iostream.h>
#include <conio.h>
void main()
{
int a, b;

clrscr();
cout<<"Enter first number: ";
cin>>a;
cout<<"Enter second number: ";
cin>>b;
if(a*a== b)
cout<<"2nd number is square of
1st number.";
getch();
}
Write a program that inputs three numbers and displays the maximum number.
Solution:
#include <iostream.h>
#include <conio.h>
void main()
{
int a, b, c, max;
cout<<"Enter first number: ";
cin>>a;
cout<<"Enter second number: ";
cin>>b;
cout<<"Enter third number: ";
cin>>c;
max = a;
if(b > max)
max = b;
if(c > max)
max = c;
cout<<"Th
e
maximum
number is
‘ if-else ’ Statement
 if else statement is another type of if statement.
 It executes one block of statement(s) when the condition is true and the other when it is
false.
 In any situation, one block is executed and the other is skipped.
 In if else statement: Both blocks of statements can never be executed nor skipped.
Syntax For compound (more than 1) statements
if (condition) if (condition)
statement; {
else statement 1;
statement; statement 2;
statement
N;
}
else
{
statement 1;
statement 2;
statement N;
}
Write a program that inputs a number and finds whether it is even or odd using if-else structure.
Solution:
#include <iostream.h>
#include <conio.h>
void main(}
{
int n;
clrscr();
cout<<"Enter a number: ";
cin>>n;
if(n%2 == 0)
cout<<n<<" is even.";
else
cout<<n<<" is odd.";
getch();
}
Multiple ‘if-else-if’ Structure
 if-else-if statement can be used to choose one block of statements from many blocks of statements.
 It is used when there are many options and only one block of statements should be executed on the
basis of a condition.
Syntax
if (condition)
{
block 1;
}
else if (condition)
{
block 2;
}
.
.
else
{
block N;
}
Write a program that inputs test score of a student and displays his grade according to the following criteria:
Solution:
#include <iostream.h> Test Score Grade
#include <conio.h> >= 90 A
void main() 80-89
{ 70- 79 B
clrscr(); 60-69
Below 60 C
int score;
cout<<"Enter your test score: ";
D
cin>>score;
if(score>=90) F
cout<<"Your grade is A.";
else if(score>=80)
cout<<"Your grade is B.";
else if(score>=70)
cout<<"Your grade is C .";
else if (score>=60)
cout<<"Your grade is D.";
else
cout<<"Your grade is F.";
getch();
}
Write a program that inputs radius and user's choice. It calculates area of circle if user enters 1 as choice.
It calculates circumference if the user enters 2 as choice.
Solution:
#include <iostream.h>
#include <conio.h>
#include <iomanip.h> else if(choice == 2)
{
void main()
circumference= 2.0 * 3.141 * radius;
{
cout<<"Circumference of circle:
clrscr(); "<<circumference;
float area, radius, }
circumference; else
int choice; cout<<"lnvalid choice.";
cout<<"Enter radius: "; getch();
cin>>radius; }
cout<<"Enter 1 for
area and 2 for
circumference:" ;
cin>>choice;
if(choice == 1)
{
area = radius * radius
Nested ‘if ’
Structure
 An if statement within an if statement is called nested if statement.
 In nested structure, the control enters into the inner if only when the outer condition is true.
 Only one block of statements are executed and the remaining blocks are skipped automatically.
 The user can use as many if statements inside another if statement as required.
 The increase in the level of nesting increases the complexity of nested if statement.
Syntax
if (condition)
if (condition)
{
Outer if condition
statement(s);
}
else
{ Inner if condition
statement(s);
}
else
{
statement(s);
}
Write a program that inputs three numbers and displays the number is identical or different by using nested
if condition.
Solution:
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int a, b, c;
cout<<"Enter three
number:";
cin>>a>>b>>c;
if(a==b)
if(a==c)
cout<<"AII numbers are equal.";
else
cout<<"Numbers are different.";
else
cout<<"Numbers are different.";
getch();
}
Compound Condition
 A type of comparison in which more than one conditions are evaluated is called compound
condition.
 It is used to execute a statement or set of statements by testing many conditions.
 Compound condition is executed by using logical operators.

Logical Operators
 Logical operators are used to evaluate compound conditions. There are three logical operators in C++.

a) AND operator(&&) : It produces true result only, if both conditions are true. Otherwise false.

b) OR operator ( || ) : It produces false result only, if both conditions are false. Otherwise true.

c) NOT operator (!) : It is used to reverse the result of a condition. It produces true result if the
condition is false and vice versa.
Write a program that inputs three numbers and displays the maximum number by using logical operators.
Solution:
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int a, b , c;
cout<<"Enter three numbers: ";
cin>>a>>b>>c;
if(a>b && a>c)
cout<<"Maximum number is "<<a;
else if(b>a && b>c)
cout<<"Maximum number is "<<b;
else
cout<<"Maximum number is "<<c;
getch();
}
Write a program that inputs a character and displays whether it is a vowel or not.
Solution:
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
char
ch;
cout<<"Enter any character: ";
cin>>ch;
if(ch=='A' || ch=='a' || ch=='E'
|| ch=='e' || ch=='I' || ch=='i' ||
ch=='O' || ch=='o' || ch=='U' ||
ch=='u')
cout<<"You entered a vowel: "<<ch;
else
cout<<"You did not enter a vowel:
"<<ch;
getch();
}
‘switch’ Structure
 The switch statement is another conditional structure.
 It is a good alternative of nested if-else.
 It can be used easily when there are many choices available and only one should be
executed.
 Nested if becomes very difficult in such situation
Syntax
switch (expression)
{
case constant 1:
statement(s); First case body
break;
case constant 2:
statement(s); Second case body
break;

case constant N:
statement(s); Nth case body
break;
default:
statement(s); Default case body
}
Write a program that inputs a floating point number, an operator and another floating point number. It displays the result
by performing the operation on the given numbers. If the operator is a division, it should check to make sure that the
divisor is not equal to zero. If the operator is not a +, -, *, or / then the program should print an error message.
Solution:
cout<<a+b<<endl;
#include <iostream.h> break;
#include <conio.h> Case'-':
void main() cout<<a-b<<endl;
{ break;
clrscr(); case'*':
float a , b; cout<<a*b<<endl;
char op; break;
cout<<"Enter a floating point number: "; case '/':
cin>>a; if (b == 0)
cout<<"Enter an operator: "; cout<<"Division by zero!"<<endl;
cin>>op; else
cout <<"Enter second cout<<a/b<<endl;
floating point number: " ; break;
cin>>b; default:
switch(op) cout<<"lnvalid operator!“<<endl;
{ }
case'+': getch();
}

You might also like