Question

What is a beginner friendly way to write this for loop? for(char i=tokens[1].charAt(0); i != tokens[2].charAt(0);...

What is a beginner friendly way to write this for loop?

for(char i=tokens[1].charAt(0); i != tokens[2].charAt(0); i = (char)((tokens[1].charAt(0) > tokens[2].charAt(0))? i-1 : i+1)) {

...

}

Homework Answers

Answer #1

Here, to get the first letter of a string, you must use the charAt(0). So, it is to be there in modified code also. What you can reduce is the increment/ decrement operation performed and you can move it at the end of the for loop code you have written. This leaves no change, you can write in the both ways. If you want to make for loop looking small, you can even take the initialization of i outside the loop. This is working similar as while loop if you are taking both 1 and 3 from for(1,2,3). So, to minimize this code and make it understandable, I moved the increment/decrement after the for loop implementation.

Code:


char i;

for(i = tokens[1].charAt(0); i!=tokens[2].charAt(0); )
{
   //your execution code of the for loop

   if(tokens[1].compareTo(tokens[2]) > 0)
   {
       i = (char)(i-1);
   }
  
   else
   {
       i = (char)(i + 1);
   }
}

You can define the variable i just before the for loop, its initialization and condition checking is the same. I removed the third part from the loop and wrote it inside.

Here, the code was comparing both the strings tokens[1] and tokens[2]'s first character. If the tokens[1] is having greater character ( a<b<c..... such) then you need to decrement i or else increment it by 1 and convert it into char again. This was done using conditional operators, I changed it to simple if else condition.

Moreover, you can directly find the difference between the first two characters of the string when their first character are not same using compareTo() method of String class. So, instead of writing

i = (char)((tokens[1].charAt(0) > tokens[2].charAt(0))? i-1 : i+1)

you can write the if-else to make it simplified.

Do comment if there is any query. Thank you. :)

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
q : explain the code for a beginner in c what each line do Question 2....
q : explain the code for a beginner in c what each line do Question 2. The following code defines an array size that sums elements of the defined array through the loop. Analyze the following code, and demonstrate the type of error if found? What we can do to make this code function correctly ? #include <stdio.h> #define A 10 int main(int argc, char** argv) { int Total = 0; int numbers[A]; for (int i=0; i < A; i++)...
Write any 2 C programs of a structure having return type int, char, float. Explain each...
Write any 2 C programs of a structure having return type int, char, float. Explain each step for both the programs. The code should be simple as I am a beginner. Also, I need an urgent answer.
Urn 1 contains 2 blue tokens and 8 red tokens; urn 2 contains 12 blue tokens...
Urn 1 contains 2 blue tokens and 8 red tokens; urn 2 contains 12 blue tokens and 3 red tokens. You roll a die to determine which urn to choose: if you roll a 1 or 2 you choose urn 1; if you roll a 3, 4, 5, or 6 you choose urn 2. Once the urn is chosen, you draw out a token at random from that urn. Given that the token is blue, what is the probability that...
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 extern char **environ;    5 void output(char *a[], char...
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 extern char **environ;    5 void output(char *a[], char *b[]) { 6 int c = atoi(a[0]); 7 for (int i = 0; i < c && b[i]; ++i) { 8 printf("%s", b[i]+2); 9 } 10 } 11 12 void main(int argc, char *argv[]) { 13      14 switch (argc) { 15 case 1: 16 for (int i = 0; environ[i]; ++i) {    17 printf("%s\n", environ[i]); 18 } 19 break; 20 default: 21 output(argv +...
bool update(char userInput){ int i = board_height - 1; int j = userInput - 'A'; while(i>=0){...
bool update(char userInput){ int i = board_height - 1; int j = userInput - 'A'; while(i>=0){ if(board[i][j] == ' '){ board[i][j] = next; if(checkRow(i,j,next) || checkColumn(i,j,next) || checkLeftDiagonal(i,j,next) || checkRightDiagonal(i,j,next)){ global_state = next - '0'; } if(next == '1'){ next = '2'; }else{ next = '1'; } break; } i--; } if(i == -1){ return false; } return true; } How can this be turned into int update instead of bool update
Consider the following variable definitions: char a, *b, *c; int d[2], *e; int i, *j; How...
Consider the following variable definitions: char a, *b, *c; int d[2], *e; int i, *j; How many total bytes does this code allocate for variables?  Assume a 32-bit representation for integer and pointer values. a     sizeof(char) b     sizeof(char *) c     sizeof(char *) d    2*sizeof(int) e     sizeof(int *) i     sizeof(int) j     sizeof(int *) Total number of bytes ? What is the output of the following piece of code? (Use the above variable definitions). j = &i; *j = 50;                      /* i = 50 */ e = &d[0]; *e...
1. The conditon of a "while" loop and a "for" loop are executed at the ____________...
1. The conditon of a "while" loop and a "for" loop are executed at the ____________ of these loops. 2. What will the following code fragment print?    for (i = 0; i < 4; i++)        cout << i + 1; cout << i; 3. What is the result in answer below? int int1=16; int int2=5; int answer=0; answer = int1 / int2 + (5 * 2);
Write a while loop that prints userNum divided by 2 (integer division) until reaching 1. Follow...
Write a while loop that prints userNum divided by 2 (integer division) until reaching 1. Follow each number by a space. Example output for userNum = 40: 20 10 5 2 1 ..... my code: #include <iostream> using namespace std; int main() { int userNum; userNum = 40; /* Your solution goes here */ while (userNum != 1){ userNum = userNum/2; cout << userNum << " ";   } cout << endl; return 0; } ........ but as a result i...
I am a beginner when it comes to java codeing. Is there anyway this code can...
I am a beginner when it comes to java codeing. Is there anyway this code can be simplified for someone who isn't as advanced in coding? public class Stock { //fields private String name; private String symbol; private double price; //3 args constructor public Stock(String name, String symbol, double price) { this.name = name; this.symbol = symbol; setPrice(price); } //all getters and setters /** * * @return stock name */ public String getName() { return name; } /** * set...
Write a for loop that prints: 1 2 ... userNum Ex: userNum = 4 prints: 1...
Write a for loop that prints: 1 2 ... userNum Ex: userNum = 4 prints: 1 2 3 4 import java.util.Scanner; public class ForLoops { public static void main (String [] args) { int userNum; int i; Scanner input = new Scanner(System.in); userNum = input.nextInt(); for (/* Your code goes here */) { System.out.print(i + " "); } } } Need to fill out the "for" solved in JAVA