+ 1
will the else statement remains same for all if statements or they will have different else statements for different if statemen
3 Antworten
+ 1
The answer is: different else statement for different if statement
0
It will be helpful if you can post a sample code explaining your problem.
This is how conditional statements work in c++ (and most of the other programming languages). Each conditional check has one of two values, "true" or "false".
So for each if statement you can define what to do if it's evaluated to true, and if it's evaluated to false.
e.g.
if(i==0)
{
i++
}
else
{
i--;
}
But if you want you can omit the else part. Then it will proceed to the next executable line.
Every "else" clause has single matching "if" clause. You cannot share an "else" clause with multiple "if" clauses.
0
Yes ,I hope you can share multiple "if" with a single "else". here is an example:
if (score >= 90){
grade = 'A';
}
if (score >= 80){
grade = 'B';
}
if (score >= 70){
grade = 'C';
}
if (score >= 60){
grade = 'D';
}
else {
grade = ‘F’;
}