Best practice for simple if else [closed] - if-statement

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed yesterday.
Improve this question
Suppose I have an if else logic that sets a variable. Pseudocode below:
if validStudent;
if marks < 10;
grade = 'D';
else;
grade = 'A';
end if;
end if;
The field grade is never set if the outer if block is false i.e, not valid student.
But I want to set it as 'N' for invalid student.
There are two ways to do this. Which would be considered a best practice and why ?
if validStudent;
if marks < 10;
grade = 'D';
else;
grade = 'A';
end if;
else;
grade = 'N';
end if;
OR
grade = 'N';
if validStudent;
if marks < 10;
grade = 'D';
else;
grade = 'A';
end if;
end if;
I personally would prefer setting the grade to N always and then changing it if a valid student. Reason just to avoid an additional else block that would make the nested ifs more confusing.
Please keep in mind, this is a simplified form of a complex if condition in a huge program with a lot of nested if branches and complex conditions.

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.

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.

C++ while loop with 2 conditions, VS. for loop with 2 conditions? [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 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.

How do a compare a char to a number? C++ [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 months ago.
Improve this question
in my current program I have the user input a number that gets stored in a char list. After that I go through the list and try to assign that number to an int variable. That is where I run into an error. This is an excerpt of my code.
if (list1[j]=='1');
z=1;
if (list1[j]=='2');
z=2;
if (list1[j]=='3');
z=3;
if (list1[j]=='4');
z=4;
The issue is that z always becomes 4 even if list[j]=3. I know I am making a mistake my comparisons but I've been unable to locate it. I would appreciate your help.
This is how I define the list:
char list1[32];
And this is how I fill it up:
for(int i=0;i<(2*c);i+=2)
{
cin>>list1[i]>>list1[i+1];
}
You can replace your if statements with a single math line:
z = list1[j] - '0';
This works for most encodings.
Note: the above statement only works with single digit characters.
Edit 1: switch vs. if
If you insist on comparing, I believe a switch would be more readable:
switch (list[j])
{
case '1': z = 1; break;
case '2': z = 2; break;
case '3': z = 3; break;
// ...
}
The single statement above is still less code, less chances for defects.
All you need to do is drop the semicolons from your if statements. If you include the semicolon after each if statement the code-block in the if statement won't execute. It should look like this:
if (list1[j]=='1')
z=1;
if (list1[j]=='2')
z=2;
if (list1[j]=='3')
z=3;
if (list1[j]=='4')
z=4;

for loops instead of if else statement [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
program:
what else can I use in this program instead of using multiple if else statement
my desire number is until a thousand.
do {
System.out.print("Enter number: ");
a=Integer.parseInt(br.readLine());
if(a==1)
System.out.println(o[0]);
if(a==2)
System.out.println(o[1]);
if(a==3)
System.out.println(o[2]);
if(a==4)
System.out.println(o[3]);
if(a==5)
System.out.println(o[4]);
if(a==6)
System.out.println(o[5]);
}
while(a!=0);
if (a==0){
System.out.println("You entered number zero");
}
do {
System.out.print("Enter number: ");
a = Integer.parseInt(br.readLine());
if (a != 0 && a < o.length) {
System.out.println(o[a-1]);
}
} while(a != 0);
if (a == 0) {
System.out.println("You entered number zero");
}
Start by analyzing the pattern: given if(a==X) System.out.println(o[Y]);, what is the relation of X (and therefor a) to Y?
Now eliminate all the if-else statements to take this into account - the program should be left with a single System.out.println(o[..]); line (where the expression .. transforms a by the pattern identified above).
Then, what is the domain of X (and a by extension)? That is, for what values of X should this println operation occur?
Add an if statement around the above println; this will prevent output when the user enters 0 (or 7), for instance.