Write a for loop that displays the following set of numbers
Solution
7.FOR LOOP :
int x=0;
for(x = 0; x <= 1000; x += 10)
cout << x;
8.while loop output:
8a)
A while loop in C programming repeatedly executes a target statement as long as a given condition is true.
Syntax
The syntax of a while loop in C programming language is
Here, statement(s) may be a single statement or a block of statements. Thecondition may be any expression, and true is any nonzero value. The loop iterates while the condition is true.
output of program: error : bogus semicolon at while loop
The program contains an infinite loop that keep testing the loop-continuation-condition
The program will run forever but will not make any progress.
8b)
while loop can be addressed as an entry control loop. It is completed in 3 steps.
Syntax :
output of program: x = 1 2 3 4 5 6 7 8 9
9)
#include <Servo.h> //includes the servo library
int motorPin1 = 3;
int motorPin2 = 4;
int motorPin3 = 5;
int motorPin4 = 6;
int servoPin = 9;
const int pingPin = 8;
const int dist = 0;
int leftdist = 0;
int rightdist = 0;
int object = 10; //distance at which the robot should look for another route
Servo myservo;
void setup ()
{
Serial.begin(9600); // initialize the data rate in bits per second
myservo.attach(9);
pinMode(motorPin1,OUTPUT);
pinMode(motorPin2,OUTPUT);
pinMode(motorPin3,OUTPUT);
pinMode(motorPin4,OUTPUT);
myservo.attach(servoPin);
myservo.write(90);
delay(700);
}
void loop()
{
if(dist < object) { //if distance is less than 5
forward(); //then move forward
}
if(dist >= object) { //if distance is greater than or equal to 5
findroute();
}
}
void forward() {
digitalWrite(motorPin1,HIGH );
digitalWrite(motorPin2,LOW);
digitalWrite(motorPin3,HIGH);
digitalWrite(motorPin4,LOW);
return;
}
void findroute() {
halt(); // stop
reverse(); //go backwards
lookleft(); //go to subroutine lookleft
lookright(); //go to subroutine lookright
if ( leftdist < rightdist )
{
turnleft();
}
else
{
turnright ();
}
}
void backward() {
digitalWrite(motorPin1,LOW);
digitalWrite(motorPin2,HIGH);
digitalWrite(motorPin3,LOW);
digitalWrite(motorPin4,HIGH);
delay(500);
halt();
return;
}
void halt () {
digitalWrite(motorPin1,LOW);
digitalWrite(motorPin2,LOW);
digitalWrite(motorPin3,LOW);
digitalWrite(motorPin4,LOW);
delay(500); //wait after stopping
return;
}
void lookleft() {
myservo.write(150);
delay(700); //wait for the servo to get there
leftdist = digitalRead(pingPin);
myservo.write(90);
delay(700); //wait for the servo to get there
return;
}
void lookright () {
myservo.write(30);
delay(700); //wait for the servo to get there
rightdist = digitalRead(pingPin);
myservo.write(90);
delay(700); //wait for the servo to get there
return;
}
void turnleft () {
digitalWrite(motorPin1,HIGH); //use the combination which works for you
digitalWrite(motorPin2,LOW); //right motor rotates forward and left motor backward
digitalWrite(motorPin3,LOW);
digitalWrite(motorPin4,HIGH);
delay(1000); // wait for the robot to make the turn
halt();
return;
}
void turnright () {
digitalWrite(motorPin1,LOW); //use the combination which works for you
digitalWrite(motorPin2,HIGH); //left motor rotates forward and right motor backward
digitalWrite(motorPin3,HIGH);
digitalWrite(motorPin4,LOW);
delay(1000); // wait for the robot to make the turn
halt();
return;
}
long duration, inches, cm; // function from datasheet of ping sensor
pinMode(pingPin,OUTPUT); //
digitalWrite(pingPin,LOW);
delayMicroseconds(2); // delay recomended from datasheet
digitalWrite(pingPin,HIGH);
delayMicroseconds(5); // delay recomended from datasheet
digitalWrite(pingPin,LOW);
pinMode(pingPin,INPUT);
duration =pulseIn(pingPin,HIGH);
inches = microsecondsToInches(duration); // converting time into distance
cm = microsecondsToCentimeters(duration);
Serial.print(inches);
Serial.print(\"in, \");
Serial.print(cm);
Serial.print(\"cm\");
Serial.println();
delay(100);
long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}



