A PHP Error was encountered

Severity: 8192

Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Filename: libraries/Filtered_db.php

Line Number: 23

Error: \'else\' without a previous \'if\' in C
Q:

Error: \'else\' without a previous \'if\' in C

0

Error: 'else' without a previous 'if' in C

This error: 'else' without a previous 'if' is occurred when you use else statement after terminating if statement i.e. if statement is terminated by semicolon.

if...else statements have their own block and thus these statement do not terminate.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Consider the given code:

#include <stdio.h>

int main()
{
	int a = 10;
	if(a==10);
	{
		printf("True\n");
	}
	else
	{
		printf("False\n");
	}

	return 0;
}

Output

prog.c: In function 'main':
prog.c:8:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
     if(a==10);
     ^~
prog.c:9:5: note: ...this statement, but the latter is misleadingly indented as if it is guarded by the 'if'
     {
     ^
prog.c:12:5: error: 'else' without a previous 'if'
     else
     ^~~~
 

How to fix?

See the statement, if(a==10);

Here, if statement is terminated by semicolon (;). Thus, Error: 'else' without a previous 'if' in C is occurred.

To fix the error remove the semicolon (;) after the if statement.

Correct code:

#include <stdio.h>

int main()
{
	int a = 10;
	if(a==10)
	{
		printf("True\n");
	}
	else
	{
		printf("False\n");
	}

	return 0;
}

Output

True

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now