FOR & IF

When using FOR, isn't it START; END; INCREMENT? If so, how can END be less than or equal to something? That isn't a proper end is it? Can't it stop anywhere between 0 and length in this loop?:

for (ctr = 0; ctr <= length; ctr++)

This is for a Caesar cipher I'm making. I'm using IFs inside of the loop. Is this the correct syntax?:

1
2
3
4
5
6
7
8
    for (ctr = 0; ctr <= length; ctr++)
    {
        if (in[ctr]=='a')
        {
            out[ctr] = 'z';
        }
        // Rest of IFs...
    }


If this isn't correct, please tell me what to fix.

Original FOR & IF idea from Daniweb.
Last edited on
When using FOR, isn't it START; END; INCEMENT?


No, it is not.

It is
for(statement to execute once at the start; end when this condition is false; statement to execute at end of each loop)

If so, how can END be less than or equal to something?

It isn't. It ends when the condition is false.
Last edited on
The loop will continue as long as ctr <= length is true. If you had used a while loop instead it would have looked something like this:
1
2
3
4
5
6
7
8
9
10
ctr = 0;
while (ctr <= length)
{
	if (in[ctr]=='a')
	{
		out[ctr] = 'z';
	}
	...
	ctr++;
}


Using a for loop is more readable when you get used to it.
for most strings you want

1
2
3
4
for(int idx = 0; idx < string_length; idx++)
{
  // this will go from string[0] to string[length - 1];
}


If idx <= string_length was used instead it would increment 1 past the last valid string index
So should I use WHILE or FOR loops?
You can use either. It's up to you.
So should I use WHILE or FOR loops?

They're the same thing really, use whatever looks better to you.
The general "rule" is to use a for loop when the loop has a definite end and use a while loop when it doesn't.

1
2
3
4
5
6
7
8
char *name = "John Smith";
cout << "Hello ";

for (int i = 0; name[i] != '\0'; i++)
{
  cout << name[i];
}
cout << ".  Nice to meet you\n";

1
2
3
4
5
6
int i = -1;
while (i < 0)
{
  cout << "Enter a positive integer: ";
  cin >> i;
}

Last edited on
What does the "[ctr]" do after "in" within the IF? Does it multiply in by ctr? I don't know why it's there.
Last edited on
What does the "[ctr]" do after "in" within the IF?


Arrays. http://www.cplusplus.com/doc/tutorial/arrays/

Thanks. Oh and, with calling Funtions, is there a way I can call a function multiple times without having to c&p the call over and over again? I want to have it call "randchar" x times, when x equals a random number. I'm using this for a project where it generates a random number called "x" and calls "randchar" x times. Is this possible?
Is this possible?


Yes.

Call it in a loop; loop x times. You need to go back a few steps and start learning from the beginning.
Okay, checking out a C++ book from the library soon. Now then, I'm using a FOR loop now for the calling of functions. Exactly how do I use if without arrays to read the next char of the input variable like "a", then export a seperate character like "z" to the output variable? I want to do it like this:

1
2
3
4
if (input[ctr] == 'a')
{
    output[ctr] = 'z'
}


Then I want to combine all of the seperate output variables using STRING like this:

1
2
3
4
5
for (ctr = ctr; ctr > 0; ctr--)
{
    string x = output[ctr];
    string result = result + x;
}


The problem being is that I don't know how to name a variable after a variable. I want it to be like this:

output4 = x

Where "x" equals the outputted CipherChar, and output is the variable name, and "4" equals "ctr". But I want both variables to combine and equal x.
Last edited on
How does the array apply to the variable? (I lost the original page and can't seem to locate it.) Does it make the variable value become the array? Idk what's going on with the IFs yet. And exactly how do I read a variable, char by char, with IF? I have c set as the current char to read, but I don't know how to skip to the next char of the input variable:

1
2
3
4
5
string o;
if (c = 'a')
{
    o = o + Z
}


Do I need to replace o = o + 'z'; with string o = o + 'z'; to add 'Z' to o, the output? Should I maybe split the input into a char array then have BitCrypt read the array char by char?
Last edited on
Topic archived. No new replies allowed.