Is this Valid Switch statement ?????

S

Sridhar_Gadda

Hello friends,

I am new to C and I have written a code but
#include <stdio.h>
main()
{
init_funtion();
}


void init_funtion();
{
int i;
for (i =1; i < 10; i++)
{
switch (i)
{
case 1 :
{
printf("my vlaue is 1");
break;
}
case 2 :
{
printf(" my value is 2");
break;
}
and so on till case 9
}
}
}


but when I complile the error is "the statement for is of no effect" is
that mean in for loop I cannot define my switch satement......in general
switch statement can be written in for loop ???

please help me ....

I am waiting for your replies

with regards,

Sridhar.
 
J

Joona I Palaste

Sridhar_Gadda said:
Hello friends,
I am new to C and I have written a code but
#include <stdio.h>
main()
{
init_funtion();
}

void init_funtion();
{
int i;
for (i =1; i < 10; i++)
{
switch (i)
{
case 1 :
{
printf("my vlaue is 1");
break;
}
case 2 :
{
printf(" my value is 2");
break;
}
and so on till case 9
}
}
}

but when I complile the error is "the statement for is of no effect" is
that mean in for loop I cannot define my switch satement......in general
switch statement can be written in for loop ???
please help me ....

Take the semicolon away from the end of your for loop line.
 
C

Christopher Benson-Manica

Joona I Palaste said:
Take the semicolon away from the end of your for loop line.

Since there is no semicolon at the end of the line in question:

I assume you're talking about the one at the end of this line :)
 
S

Sridhar_Gadda

ok thanks ...I see that my program is working and you can define Switch
statement inside forloop.

greetings

sridhar,
 
K

Keith Thompson

Sridhar_Gadda said:
I am new to C and I have written a code but
#include <stdio.h>
main()
{
init_funtion();
}


void init_funtion();
{
int i;
for (i =1; i < 10; i++)
{
switch (i)
{
case 1 :
{
printf("my vlaue is 1");
break;
}
case 2 :
{
printf(" my value is 2");
break;
}
and so on till case 9
}
}
}


but when I complile the error is "the statement for is of no effect" is
that mean in for loop I cannot define my switch satement......in general
switch statement can be written in for loop ???

That's not the same as the program you actually compiled, is it? The
error message was probably the result of a minor error in your code,
most likely an extra semicolon on the line with the "for". You've
posted code with *different* minor errors, particularly the extra
semicolon on the "void init_funtion() line. When I compiled the code
you posted (with the "and so on till case 9" commented out), I got a
syntax error, not a complaint about the for loop.

We can't tell you what the problem is unless you post the *exact* code
that you compiled. Don't re-type it, cut-and-paste it. And don't
paraphrase (the "and so on till case 9"), but reduce the code as much
as you can.

(BTW, it's spelled "function", not "funtion". C doesn't care whether
your identifiers are correctly spelled as long as you're consistent,
but your readers will care.)
 
J

Joe Wright

Sridhar_Gadda said:
Hello friends,

I am new to C and I have written a code but
#include <stdio.h>
main()
{
init_funtion();
}


void init_funtion();
{
int i;
for (i =1; i < 10; i++)
{
switch (i)
{
case 1 :
{
printf("my vlaue is 1");
break;
}
case 2 :
{
printf(" my value is 2");
break;
}
and so on till case 9
}
}
}


but when I complile the error is "the statement for is of no effect" is
that mean in for loop I cannot define my switch satement......in general
switch statement can be written in for loop ???

please help me ....

I am waiting for your replies

with regards,

Sridhar.

Try this. Look carefully at how it differs from your code.

#include <stdio.h>

void init_funtion(void)
{
int i;
for (i = 1; i < 4; i++) {
switch (i) {
case 1:
{
printf("my vlaue is 1");
break;
}
case 2:
{
printf("my value is 2");
break;
}
case 3:
{
printf("my value is 3");
break;
}
}
puts("");
}
}

int main(void)
{
init_funtion();
return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top