Question

In: Computer Science

Question 1: What is For Loop? Explain with 2 Examples. You can write code in any...

Question 1: What is For Loop? Explain with 2 Examples. You can write code in any programming language.

Solutions

Expert Solution

For-Loop is basically programming control flow statement for executing a set of instructions inside it for a specific number of times or until unless a condition is violated .

It has two parts:

  • a header for specifying an iteration
  • body part (generelly enclosed within{ ,} )

Inside the header it has three parts which are applied on a loop variable say i (the variable that runs the loop) :

  • initailization(eg : i= 0)-It tells with what initial value loop starts from
  • condition ( i<5) it tells till what iterations loop must run
  • increment or decrement or any other operation =It tells how to change the control variable
  • The syntax pseudo code for all programming language is:
  • for( i:initialize;i :(condition);i:(operation))

{

Body of loop with set of instructions

}

Example 1: Let us take an example to print Hello 10 times:

Approach : You can write code Hello 10 times seperately but it would be very repeated code and would not appear code for a programme .

So for your rescue comes the foor loop:

for(int i=0;i<10;i++){

System.out.println("Hello)   

}

In above you can see you dont need the loop variable i inside the body but it is used to control the loop.If you replace < with <= symbol the loop will print hello 11 times.

Example 1: Let us take an example to print sum of numbers from 1 to 10:

Approach : You can write code as :

ans 1+2+3_ __ 10; but it will be a bad progrmming practice

So for your rescue comes

the foor loop:

for(int i=1;i<=10;i++){

ans=ans+10;

}

In above you can see you need the loop variable i inside the body If you replace < = with < symbol the loop will compute sum from 1 to 9.

Please find the code attached below in JAVA.

If you have any doubts feel free to ask

public class forLoop{

     public static void main(String []args){
         //Code for printing Hello 10 times
         for(int i=1;i<=10;i++)
         {
             //Print statement in Java
            System.out.println("Hello"); 
             
         }
         int ans=0;
         //Code for sum of numbers from 1 to 10
         for(int i=1;i<=10;i++)
         {
             ans=ans+i;
         }
         System.out.println("The some of numbers from 1 to 10 is "+ans);
         
     }
}


Related Solutions

explain what a while loop is and how you can use it. Give examples based on...
explain what a while loop is and how you can use it. Give examples based on websites on how you can use a while loop to your advantage. This assignment should be a couple paragraphs long (5-8 sentences per paragraph).
Code in python Write a while loop code where it always starts form 2. Then it...
Code in python Write a while loop code where it always starts form 2. Then it randomly chooses a number from 1-4. If the number 4 is hit then it will write “TP” if the number 1 is hit then it will write”SL”. It will rerun the program every time the numbers 1 and 5 are hit. The code should also output every single number that is randomly chosen. 2 of the same numbers can't be chosen back to back...
java code Question 4: Iterating with loops You can process the list using a For loop....
java code Question 4: Iterating with loops You can process the list using a For loop. The variable in the For loop becomes the index value for each of the items in the loop. Everything you do to an element inside the loop gets done for the entire list. Examine the following loops. Then use what you know to write the following loops. int[] numbers = new int[100]; for(int i = 0; i < numbers.length; i++){ numbers[i] = i; }...
Can you rewrite this MATLAB code using a for loop instead of a while loop? %formatting...
Can you rewrite this MATLAB code using a for loop instead of a while loop? %formatting clc, clear, format compact; %define variables k=1; b=-2; x=-1; y=-2; %while loop initialization for k <= 3 disp([num2str(k), ' ',num2str(b),' ',num2str(x),' ',num2str(y),]); y = x^2 -3; if y< b b = y; end x = x+1; k = k+1; end
Where is the infinite loop in the code? Input for the code 1 2.5 2 40200000...
Where is the infinite loop in the code? Input for the code 1 2.5 2 40200000 1 0 2 80400000 2 ffffffff 3 #include <stdio.h> #include <math.h> #include <stdlib.h> int count = 31; void toBinary(int num, int n){ for(int i = 1; i < n; i++){ if((int)(num/pow(2,(n-i))) > 0){ num = num - pow(2,(n-i)); printf("1"); count--; }else{ printf("0"); count--; } } } char checkSign (int sign) { char new_sign; if (sign == 0) { new_sign = '+'; return new_sign; }...
Programming. Write only code for the following. Do not write any comments. You can submit your...
Programming. Write only code for the following. Do not write any comments. You can submit your answer as .java file(s). #1. Design a Java JProduct class for a product which implements both cloneable and comparable interfaces The class should have the following private member variables: m_id: an integer that holds the product ID m_name: a string that holds the product name m_wholesaleprice: a double that holds the wholesale price m_retailers: a String array that holds all retailers who sell the...
Write a python code which prints triangle of stars using a loop ( for loop )...
Write a python code which prints triangle of stars using a loop ( for loop ) Remember what 5 * "*" does The number of lines of output should be determined by the user. For example, if the user enters 3, your output should be: * ** *** If the user enters 6, the output should be: * ** *** **** ***** ****** You do NOT need to check for valid input in this program. You may assume the user...
can you answer this question correct: Explain fully, with examples, what dollar cost averaging is. What...
can you answer this question correct: Explain fully, with examples, what dollar cost averaging is. What will happen (1) if the price of an investment trends down overtime; (2) trends up; (3) trends down then up; and (4) in real life? Use excel to model and graph the resu
Question 3: What is Function? Explain with examples. Provide at least 2 program examples.
Question 3: What is Function? Explain with examples. Provide at least 2 program examples.
every code should be in java Program 1: Write a while loop that sums the integers...
every code should be in java Program 1: Write a while loop that sums the integers from 1 to 10, excluding 3 and 6. Print the sum. [Hint: Use logical operators] Program 2: Write a for loop that attempts to display the numbers from 1 to 10, but terminates when the control variable reaches the value 6. Program 3: Write a do...while loop that prints the integers from 10 to 0, inclusive.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT