C++ while loop with 2 conditions, VS. for loop with 2 conditions? [closed] - c++

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
If I wanted to iterate through a string, and stop once I found either the letter "o" or the end of the string, would it make any difference to use a while loop to check the 2 conditions, or a for loop with 2 conditions?
const int STRING_SIZE = 16;
char str[STRING_SIZE] = "a bunch of words";
for loop
for (int i = 0; i < STRING_SIZE && str[i] != 'o'; i++){
//do something
}
while loop
int i = 0;
while (i < STRING_SIZE && str[i] != 'o'){
//do something
i++
}
Is there a performance difference between the two? Is one better practice than the other?

There is no difference in performance between the two loops except that:
for() Checks condition then if true its body is executed. So for is simile to while loop.
do-while loop works a slightly different: It executes then checks so at least an execution is ensured.

Related

Check an Array String character value without extra variable [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
suppose I've this code:
string str[] = {"devil", "chandra"};
// check if str[0] has properly devil, character by character or not without extra variable
Now I want to check str[ 0 ]'s all character which is 'd','e','v','i','l' one by one without extra variable.
with extra variable code will be :
string n1 = "devil";
for(int i=0; i<1; i++){
string s1 = str[i]
for(int j=0; j<s1.size(); j++){
if(s1[i] == n[i]){
cout << s1[i] << " ";
}
}
Basically, I want O(n) loop where I can access all indexes string and among them all characters.
Like s[ i ] is "devil" and s[[i]] = 'd' something like this, Know it's not valid, but is there any way to do that??
Even I don't know is it a valid question or not!
I'm not sure why you would need an extra variable. If you need a conditional that checks that the first value in the array of strings is "devil", it shouldn't be anymore complicated than:
if (str[0] == "devil")
{
* Do things *
}
C++ can check a standard string all at once. You don't need to check each individual character if that's what you're thinking.
Keep in mind, this isn't going to account for situations where the string is not exactly the same. For instance, if str[0] has "Devil" instead of "devil", then the conditional will evaluate to false.

How to loop over defined name pattern [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have around 500 signals with the naming pattern
"Dem_AllEventsStatusByte._101"
"Dem_AllEventsStatusByte._102"
"Dem_AllEventsStatusByte._103"
...
"Dem_AllEventsStatusByte._490"
I want to loop over all of them.
I tried the following code but when i=10 then my signal name is like " Dem_AllEventsStatusByte._1010" which is false it should be 110 at the end.
for (i=1; i<=3;i++)
{
SPrint(signal, "Dem_AllEventsStatusByte._10%d", i);
How to loop over 490 signals of same name pattern? please help! Thank you
You probably want something this:
for (i = 1; i <= 490 - 100; i++)
{
SPrint(signal, "Dem_AllEventsStatusByte._%d", i + 100);
or maybe this:
for (i = 101; i <= 490; i++)
{
SPrint(signal, "Dem_AllEventsStatusByte._%d", i);

Can I have a do while statement in C++ that checks for both a character and a int value before looping? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I very new at this and have an assignment in which I would like for a loop to exit if the user inputs(trans) 'e' but also end if a calculation balance(bal) is less than a constant I have set. Basically as my question states one is a character and the other an integer, will that work? I'm not trying to get people to do my homework for me, so I'm not posting all of my code or assignment, hope it makes sense.
This is the line of code I have
do {
ask user input(&trans)
e or calculation
{
while (trans != 'e'| bal < -OVR);
Just use regular unconditional loop and multiple exit conditions:
while( true ) {
char trans;
std::cin >> trans;
if( !std::cin or trans == 'e' )
break;
calculation;
if( bal > -0VR )
break;
}
So first of all you would not do unnecessary calculations, but what is more important you would make your code more readable and easier to understand - you make loop exit decision where it should be instead of pushing it into the end.

if and else if statements in C++ [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
What is the difference between this:
s = 0;
if (x > 0) s++;
if (y > 0) s++;
and this:
s = 0;
if (x > 0) s++;
else if (y > 0) s++;
Any help would be greatly appreciated.
When you write else if instead of if, program will not check the else if statement if x > 0, but when you write two if statements program will check both conditions, no matter if x > 0 or not.
In the first case the both conditions are checked because there are two different if statements.
In the second case the second condition is checked only if the first condition is evaluated to false.
Say x is 10 and y is 10. At the end of the first set of statements, s will be equal to 2. At the end of the second set of statements, s will be equal to 1.
The second example
s = 0;
if (x > 0) s++;
else if (y > 0) s++;`
will check for the y value only if x > 0 is false. The first example will execute the check regardless of x's value.

logic or syntax error? C++ [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
int rVals[];
string rNum;
for (i=0; i < rNum.length(); ++i) {
if((rVals[i] < rVals[i+1]) && (rNum[i] =='C' || rNum[i]=='X' || rNum[i]=='I')){
continue; //checks to see if preceeding value is < the next value
} else {
valid = false;
cout << "you can't subtract by M, D, L, or V\n" << endl;
break;
}
}
rVals[] is a dynamic array and is set correctly. No matter what the input is the if statement seems to evaluate to false. what is wrong with the if statement?
Take a look at this: rVals[i] < rVals[i+1]. If rVals length is 10 for instance and i is 9 rVals[i+1] will "point" to the 11th element of the array (since the indexing of an array is starting from 0 and between 0 and 9 you heave 10 elements - the size of our array).