Skip to main content

FOR LOOP EXAMPLE IN C LANGUAGE

Simple example on how "for loop" works.
Syntax:


for( initialisation ; condition ; incrementation or decrementation) {  }
  1. Initialisation  - Initialise any value
  2. Condition - Basically its till when the loop should continue. (Ex: number < 10)
  3. incrementation or decrementation : To increment or decrement any value.
Example to print 10 even numbers.

#include<stdio.h>
int main()
{
int num;
for(num=0;num<10;num++) //Repeat this block 
{
         if(num%2 == 0)
         {
            printf(" %d ",num);
         }
}
return 0;
}

Output:
0 2 4 6 8

Description:
In this example is same as while loop.We first repeat some block of statements till the value of "num" variable reaches to 10.Then we check if the current number is even and print it.

for(num=0;num<10;num++) { }- this statement  first assigns zero value to num.

Step 1:Then condition is checked.Pointer then enters the for loop block if condition is true / satisfied.

Step 2:It then executes everything inside the block as long as condition is satisfied.

Step 3:Then after the block is executed once value of num gets incremented.

Step 4: Then if condition is true Step 1 is repeated. If condition is false then pointer comes out from the for loop.

if(num%2==0) - This statement means that when number is divided by 2 then remainder is zero. This is simply to check if value in "num" variable is even or not.

num++ : This means num = num +1;
Description in marathi /เคฎเคฐाเค ीเคค เคตเคฐ्เคฃเคจ:

เคนा while loop เคšा เค‰เคฆเคนเคฐเคฃ เค†เคนे. เคน्เคฏा เค‰เคงเคฐ्เคจाเคค เฅง เคคे เฅงเฅฆ เค…ंเค•ाเคจ เคฎเคง्เคฏे even numbers (2,4,6,8,10......) เค•ोเคฃ เค•ोเคฃเคคे เค†เคนे เคคे เคจिเคตเคกเคฒे เค†เคนे.

for(num=0;num<10;num++) - เคน्เคฏाเคค เฅฉ เค˜เคŸเค• เค†เคนेเคค.
เฅง - เคชाเคนिเคฒांเคฆा "num" เคฒा เคถुเคจ्เคฏ เค•िंเคฎเคค เค เคฐเคตเคฒी เค†เคนे.
เฅจ - เคฎเค— เค…เคŸ เค…เคถी เค†เคนे เค•ि "num" เคšी เค•िंเคฎเคค เฅงเฅฆ เคชेเค•्เคทा เคฒเคนाเคจ เค…เคธเคฒी เคชाเคนिเคœे.
เฅฉ - เคœเคฐ เค…เคŸ เคฌเคฐोเคฌเคฐ เค…เคธेเคฒ เคคเคฐ เคค्เคฏा เคซॉเคฐ เคฒूเคช เคฎเคง्เคฏे เคœे เคฒिเคนिเคฒेเคฒा เค…เคธेเคฒ เคคे เคฐเคจ เคนोเคฃाเคฐ.
เฅช - เคซॉเคฐ เคฒूเคช เคเค•เคฆा เคฐเคจ เคเคฒ्เคฏाเคตเคฐ "num" เคšी เค•िंเคฎเคค เฅง เคจे เคตाเคขเคตเคฒी เคœाเคคे.
เฅซ - เคชเคฐเคค เคคी เค…เคŸ เคคเคชाเคธเคฒी เคœाเคคे. เคœเคฐ เค…เคŸ เคฌเคฐोเคฌเคฐ เค…เคธेเคฒ เคคเคฐ เคคिเคธเคฐी เค“เคณ(เคน्เคฏा เคชเคฐिเคš्เค›ेเคฆ) เคชुเคจ्เคนा เคชाเคณเคฒी เคœाเคคे.เคชเคฃ เคœเคฐ เค…เค• เคšुเค•ीเคšी เค…เคธेเคฒ เคคเคฐ เคชोเค‡ंเคคเคฐ เคฒूเคชเคš्เคฏा เคฌाเคนेเคฐ เคฏेเคคो.

num++ - เคน्เคฏा เค•ोเคก เคฎुเคณे num เคšी เค•िंเคฎเคค เฅง เคจे  เคตाเคขเคคे. (num = num+เฅง). เคนा เคจाเคนी เคฒिเคนिเคฒा เคคเคฐ "num" เคšी เค•िंเคฎเคค เคคเคธीเคš เคฐाเคนीเคฒ เค†เคฃि while loop เคšाเคฒเคคเคš เคฐाเคนเคฃाเคฐ.เคฎ्เคนเคฃूเคจ เคนि เค•ाเคณเคœी เค˜ेเคคเคฒी เคชाเคนिเคœे.

if(num%2==0) - เคน्เคฏा เค•ोเคกเคšा เค…เคฐ्เคฅ เค†เคนे เค•ि "num" เคฎเคง्เคฏे เค…เคธเคฒेเคฒ्เคฏा เค…ंเค•ाเคฒा เคœेเคต्เคนा เฅจ เคจि เคญाเค—ीเคคเคฒे เคคเคฐ เค‰เคฐ्เคตเคฐिเคค เคญाเค—(remainder) เคถुเคจ्เคฏ เคฎिเคณेเคฒ. เคœเคฐ เคถुเคจ्เคฏ เคฎिเคณाเคฒा เคคเคฐ เคคो "num" even number เค…เคธा เคฎ्เคนंเคŸเคฒा เคœाเคˆเคฒ.

Comments

Popular posts from this blog

TOUCH PLATE BASED DOOR BELL

Title:  Touch plate based door bell  Circuit:  Components: IC 555 Resistors: 1 M, 100k, 330 ohms Transistor: BC547  PN2222A Capacitor: 10n 1 Copper plate : as touch plate. A 6v battery An LED / Ic UM66 Description: This is the simple circuit for touch plate based security system. In this project what basically done is, circuit detects stray voltages produced by mains voltage and electrostatic built  up in the room.If sufficient static voltage is detected by the plate then chip will charge up. Transistor BC 547 or PN2222A is used basically to increase the sensitivity.In place of led just connect IC um 66(melody IC). Applications: In homes, of course. This can be specially used in places like hospitals, when patients need to call doctor by himself.

Interfacing Load Cell with Raspberry Pi 3 (via HX711) ⚖️

Interfacing Load Cell with Raspberry Pi 3 (via HX711) ⚖️ Interfacing Load Cell with Raspberry Pi 3 (via HX711) ⚖️ A load cell is a transducer that converts force (weight) into an electrical signal. The HX711 is a precision 24-bit analog-to-digital converter (ADC) designed for weigh scales. Today we’ll connect a load cell to Raspberry Pi 3 using the HX711 module. ๐Ÿงช ๐Ÿ”ง Components Required Component Quantity Raspberry Pi 3 1 Load Cell 1 HX711 Module 1 Jumper Wires 6 Breadboard (optional) 1 ๐Ÿ”Œ Pin Connections HX711 Pin Raspberry Pi Pin Pin Number VCC 5V Pin 2 GND Ground Pin 6 DT GPIO 5 Pin 29 SCK GPIO 6 Pin 31 Figure: Load Cell connected to Raspberry Pi 3 via HX711 ๐Ÿ’ป Python Code from hx711 import HX711 import RPi.GPIO as GPIO import time hx = HX711(dout_pin=5, pd_sck_pin=6) hx.set_reading_format("MSB", "MSB") hx.set_reference_unit(1) hx.reset()...

Interfacing Sound Sensor with Raspberry Pi 3

๐Ÿ”น Overview The KY-037 is a high-sensitivity sound detection sensor that can detect noise levels in the environment. It provides both analog and digital outputs. In this tutorial, we’ll interface the digital output of KY-037 with Raspberry Pi 3 Model B+ (without using an ADC like MCP3008) and detect sound events.