if and else if statements in C++ [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 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.

Related

check if there is anything next to the item in board (2D array) [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
Hey I'm making simple console game in C++. We have pawns on board (I made it using 2D dynamic array). I want to check if there is space to move our pawn. My simplest and best idea would be if I could check every square next to the pawn. The problem, however, arises when the pawn is in the corner of the board.
(x-1,y), (x, y+1) , (x+1, y) , (x, y-1) [this means every field around our pawn]. My board is 2D dynamic array. So if I check field e.g. array[-1][0] (this means field x: -1, y: 0) of course it goes out of memory range. And my question is, Is it good if I go outside this range anyway but I will make a condition in case of this error?
You can just check whether index is in bounds:
if (x >= 0 && x < SIZE_X && y >= 0 && y < SIZE_Y)
return array[x][y];
else
return nullptr;
Alternatively use try/catch:
try
{
return array[x][y];
}
catch (out_of_range e)
{
return nullptr;
}

justify this excution please c++ [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 3 years ago.
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.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
int x = 5, y = 10;
if(x > 0)
y = y + 100;
else;
y = y + 200;
cout << "y = " << y << endl;
The output: 310
I know that whenever there is a semicolon after else it is treated as there is nothing. I also know that if there were no curl braces then we only execute the first statement after if.
Then why the answer here appears as the second statement was executed?
Also, if the else was not there will the second be executed also and why?
After an if and after an else there needs to be exactly one statement. The statement after if will be executed if the condition is true, the statement after else will be executed if the condition is false.
Now the important thing to understand is the following:
A single semicolon ; is a statement. It's an empty statement and putting it behind if or behind else fullfils the requirements of a statement.
A block between curly brackets {} is a statement. It groups multiple statements together, but it is one statement and thus also fulfills the requirements.
Any other statement that comes after the first statement after if or else has nothing to do with the if or else and will be executed like any other code would.
So to break down your example:
if(x > 0)
y = y + 100;
else;
y = y + 200;
is equivalent to:
if(x > 0)
y = y + 100; // first statement after if
else
; // first statement after else
y = y + 200; // separate statement uneffected by if or else
Removing the else gives us this:
if(x > 0)
y = y + 100; // first statement after if
y = y + 200; // separate statement uneffected by if
and of course the statement y = y + 200; will be executed, because it has nothing to do with the if or the else.
In this case, both statements will be executed.
if(x>0)
is true so the first part will work
y = 110
after this, then we reach else and that is the end of the line the next line does not have anything to do with the if-else block, it will always execute. hence
y = 110 + 200
, so y will have a final value of 310.
Hope this helps, cheers.

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.

if condition for a number multiple [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 8 years ago.
Improve this question
I got an if condition as a multiples of 5, i need to check if condition until a value <= 10000. My if statement looks like this
// in main function
if(value >=0 && value <16){
function(number,value);
}
else if(value >=5 && value <10){
value-=16;
function(number,value);
}
....
// function
int function(int n, int value){
return (n<<value)|(n>>(16-value))
}
Is there a better way to do this if statement. I am new to programming world and a bit curious to know how to do this.
Thanks in advance
You could use function pointers.
typedef void (*func)();
func fpointers[] = {func1, func2, func3}
int check = value / 5;
fpointers [check] ();
Put the amounts you're subtracting from value into an array, indexed by the multiple of 5 for each range:
int subtract[] = [2, 5, ...];
if (value > 0 && value < 5*(sizeof subtract/sizeof(*subtract))) {
value -= subtract[value/5];
functioncall(value);
}
If you need to evaluate the condition for multiples of 5 , I suggest you to use swich case
int check = value/5;
switch(check)
{
case 0: // 0 <= value < 5
// do things
break;
case 1 : // 5 <= value < 10
// do things ...
break;
.............
default:
break;
}
I got an if condition as a multiples of 5
To check if a number is a multiple of 5, use the modulus operator:
if (number % 5 == 0) ...
i need to check if condition until a value <= 10000
This sounds like you need a loop.

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.