In the previous post we learned about the two important functions that are a must in the arduino coding.
In this tutorial, let's see the types of conditional statements and loops.
CONDITIONAL STATEMENTS -
- if -
This statement is used when there are two conditions true/false. If condition is true then execute the function or come out of the condition. !!
SYNTAX -
if (condition)
{
// statements to be executed ;
}
EXAMPLE -
let a variable (a) if greater than a value then it is to be incremented by 1
if ( a > 10)
{
a = a + 1;
}
- if ... else -
This statement is used for greater control than the basic if statement. Working of this code is as follows - if condition is true then do something , else do something different.
SYNTAX -
if (condition)
{
// statement to be executed;
}
else
{
// do something different;
}
EXAMPLE -
let variable (a) if greater than 10 , increment by 1, or, if less, then decrement by 1.
if ( a > 10)
{
a = a + 1;
}
else
{
a = a - 1;
}
- switch case -
This statement is used when there are many things to do and any one has to be selected based on the condition applied.
SYNTAX -
switch (variable)
{
case 1: //function one;
break; // to break the switch case function, if case 1 is satisfied.
case 2: // function two;
break; // to break the switch case function, if case 2 is satisfied.
case 3: // function three;
break; // to break the switch case function, if case 3 is satisfied.
default: // if nothing matches then execute this
}
EXAMPLE -
let a month number be given as input and the month is to be displayed if the month number correctly matches.
switch (month)
{
case 1: serial.println("january");
break; // if month variable is 1 then display january.
case 2: serial.println("february");
break; // if month variable is 2 then display february.
case 3: serial.println("march");
break; // if month variable is 3 then display march..
case 4: serial.println("april");
break;
case 5: serial.println("may");
break;
case 6: serial.println("june");
break;
case 7: serial.println("july");
break;
case 8: serial.println("august");
break;
case 9: serial.println("september");
break;
case 10: serial.println("october");
break;
case 11: serial.println("november");
break;
case 12: serial.println("december");
break;
default: serial.println("Please enter correct month number.")
}
LOOPS -
- while -
This loop is used when a function is to be executed till the time the condtion is satisfied.
SYNTAX -
while (condition)
{
// function to be executed;
}
EXAMPLE -
let a variable (a) if greater than a value then it is to be incremented by 1
while (a > 10)
{
a = a + 1;
}
- do ... while -
Same as the while loop exept that the condition is tested at the end of the loop.
SYNTAX -
do
{
// function to be executed.;
}while (condition);
EXAMPLE -
let a variable (a) if greater than a value then it is to be incremented by 1
do
{
a = a + 1;
} while (a > 10);
- for -
This is the most mportant loop, which is widely used for writing many functions. The main reason is that it has a huge control over the functions in the loop.
SYNTAX -
for ( initialize ; check condition ; increment)
{
// function to be executed ;
}
EXAMPLE -
Multiplication table of 2 from 2*1 to 2*10
int b = 0;
for(int a = 1; a <= 10; a++)
{
b = 2 * a;
}
result generated by variable 'b' will be - 2,4,6,8,10,12,14,16,18,20
WORKING -
- General working of the loop is -
- First variable is initialized
- Then condition is checked. If true then statements inside loop is executed, else it comes out of the loop.
- After statement execution, variable value is incremented.
- After incrementation, value is checked.
- If true, statements executed. If false, come out of loop.
- In the example -
- Variable 'a' is initialized to 1 ie value of a = 1.
- Value checked if it is less than 10 or not.
- Since 1 < 10 ,true - Statement ' b = 2*a ' is executed.
- Value of 'a' is then incremented by 1 ie value of a = 2.
- Since 2 < 10, true Statement executed.
- This continues till, value of a is 10. ie a = 10
- Since 10 <= 10, true - Statement executed - b = 2*10 = 20.
- Value of 'a' is incremented by 1 ie a = 11.
- Condition 11 <= 10, false.
- for loop is terminated.
Thank u ...
Keep tuned for next tutorial .... !!!
No comments:
Post a Comment