0% found this document useful (0 votes)
31 views46 pages

Programming Basics: Variables & Data Types

The document provides an overview of programming basics, focusing on variables, data types, and control structures in programming. It explains fundamental data types like int, char, float, and their sizes, as well as how to use if statements, switch cases, loops, and functions. Additionally, it includes examples of code snippets demonstrating these concepts in practice.

Uploaded by

harshsangra86
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)
31 views46 pages

Programming Basics: Variables & Data Types

The document provides an overview of programming basics, focusing on variables, data types, and control structures in programming. It explains fundamental data types like int, char, float, and their sizes, as well as how to use if statements, switch cases, loops, and functions. Additionally, it includes examples of code snippets demonstrating these concepts in practice.

Uploaded by

harshsangra86
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

Programming Basic

• Variable and data types:


• A variable is an identifier which is used to store some value. Constants can never
change at the time of execution. Variables can change during the execution of a
program and update the value stored inside it.
• A single variable can be used at multiple locations in a program. A variable name
must be meaningful. It should represent the purpose of the variable.
• Int my_variable = 48;
• Data types:
• There are five primary fundamental data types,
1. int for integer data
2. char for character data
3. float for floating point numbers
4. double for double precision floating point numbers
5. void
• Array, functions, pointers, structures are derived data types.
Data type Size in bytes Range
Char or signed char 1 -128 to 127
Unsigned char 1 0 to 255
int or signed int 2 -32768 to 32767
Unsigned int 2 0 to 65535
Short int or Unsigned short 2 0 to 255
int
Signed short int 2 -128 to 127
Long int or Signed long int 4 -2147483648 to
2147483647
Unsigned long int 4 0 to 4294967295
float 4 3.4E-38 to 3.4E+38
double 8 1.7E-308 to 1.7E+308
Long double 10 3.4E-4932 to 1.1E+4932
• Procedure:
• Double click on Arduino Software .
• Write a program for variable.
• First variable type is int myData; // data is variable and these type int.
Insert a Tab for reading value
Goto Scientific calculator 2is the byte value it means 16 bit bcz 1 byte = 8 bit.
2^16=65536. 65536-1/2 bcz there are two types of variable signed and unsigned int.
65535/2= 32767.5…..> copy these value and pest at program window in instead of 100 put 32767.
• Char data type.
• Its name represent and its store ASCII character.
• Byte data type
• If I change the program and write
• Instead of serial print to change Serial. Write….. To get ASCII character
A and its size.
• Id I go to program window and change int myData=65 value A and
size should be changed 2. from 1 byte to 2 byte bcz int is a 2byte size
of data type of integer.
• Float data type:
• A float is used to store a decimal number. If I store 3.14 is a constant pi value.
• If I replace float agin int…. Int myData=3.14
• It store only3 bcz int does not store value after decimal point.
• Long data type:
• Int does not store long data its limit is exceed bcz size of int is 2byte.
• Long has 4 byte data type range.
• 4 bytes means how much i.e. 2^32= 4,29,49,67,296-
1=4,29,49,67,295/2= 2,14,74,83,647.5
• String: the name string represent it store number of charactor.
• If I want to store a “MECHATRONICS AND Internet of Things”
• You can not use sizeof operator on string variable.
• it has special function i.e. length
If Statements:
• int x;

• void setup() {
• [Link](9600);
• }

• void loop() {

• if (x == 10) {
• [Link]("The Value of variable x is 10");
• while (1); // it blocks the code execution at that line of program( infinite loop)
• }
• x++; // u also use x+1;
• }
Use else statement
Put delay after x++ 500ms
int x;

void setup() {
[Link](9600);
}

void loop() {
x++;
if (x == 1) {
[Link]("Text 1");

}else if(x==10){
[Link]("Text 10");
while (1); // block the code execution
}else{
[Link]("x: ");
[Link](x);
}

delay(500);
}
Switch Case Statement
• Where to use Switch Case Statement:
• Whenever we want to test to two or more condition on one variable we must to use switch case statement.
int x;

void setup() {
[Link](9600);
}

void loop() {
switch(x){
case 10:
[Link]("We have to reach to Number 10");
break;
case 20:
[Link]("We have to reach to Number 20");
break;
case 30:
[Link]("We have to reach to Number 30");
while(1);
break;
default:
[Link]("x= :");
[Link](x);
}
For Loop
• int x;

• void setup() {
• [Link](9600);
• }

• void loop() {

• for(x=0; x<10; x++){
• [Link]("x= ");
• [Link](x);
• [Link]();

• }

• while(1);
• }
Increment & Decrement :
int x;

void setup() {
[Link](9600);
}

void loop() {
[Link]("First Loop");
for (x = 0; x < 10; x++) {
[Link]("x= ");
[Link](x);
[Link]();
}

[Link]("First Loop");
for (x = 10; x > 0; x--) {
[Link]("x= ");
[Link](x);
[Link]();
}
while (1);
}
While Loop:
1. While loop
2. Do while loop
int x;

void setup() {
[Link](9600);
}

void loop() {

while (x < 10) {


x++;
[Link]("x= ");
[Link](x);
[Link]();
}

while(1);
}
Do While loop:

int x;

void setup() {
[Link](9600);
}

void loop() {

do {
x++;
[Link]("x= ");
[Link](x);
[Link]();
}while(x<10);

while (1);
}
Function:
void setup() {
[Link](9600);
}

void loop() {

welcomeMessage();

while(1);

void welcomeMessage(){
[Link]("Welcome to the world of functions");
}
Call a Function 3 times

void setup() {
[Link](9600);
}

void loop() {

welcomeMessage();
welcomeMessage();
welcomeMessage();

while (1);

void welcomeMessage() {
[Link]("Welcome to the world of functions");

}
Calculate maximum value between two integer:

void setup() {
[Link](9600);
}

void loop() {

[Link](maxValue(2, 3));

while (1);
}

int maxValue(int value1, int value2) {


if (value1 > value2) {
return value1;
} else {
return value2;
}
}
Compare two Max Value

void setup() {
[Link](9600);
}

void loop() {

[Link](maxValue(2, 3));
[Link](maxValue(4, 8));

while (1);
}

int maxValue(int value1, int value2) {


if (value1 > value2) {
return value1;
} else {
return value2;
}
}

You might also like