Thursday, July 9, 2009

I want some concepts in looping & conditional operator in C Programming?

these are the types of conditional operator





1. if-else statement


2. switch case


3. ternary operator (? :)





conditional operators are used to check if the condition is true, IF it is true then it will execute some statement, ELSE if the statement is false then it will execute the other statement





for loops..


there is..





1. while loop


2. do-while loop


3. for loop





loops are control structures which allows a statement to be executed a several times..

I want some concepts in looping %26amp; conditional operator in C Programming?
Hi.


what do you mean? You mean you want explaining how they work? If that's the case then read on, if not then please give more details :-)





loop


You have 2 types of loops in C. for loop and while loop.


In for loop you define start point, end point and steps between these two and finally what should happen between these points.


for example you want to prnit 1,2,3,4 and 5 on to the screen.


int counter = 0;


for ( counter = 0;counter %26lt; 6; counter = counter + 1)


{


printf("%i",counter);


}


it sets the counter to 0, and the maxmimum value is less than six, so 5, and it adds one to counter every time the condition is true (i.e counter %26lt; 6)





In while loop you give one condition, and as long as it is true, whatever inside the loop, happens.


this is an infinity loop:


int x = 0;


while (x=0)


{


printf("Printing...");


}


you can change the condition to whatever you want. for example you put this in your main, and then ask the user to enter a number, and then store the number in x. if i say 0, then the condition is true, so it keeps printing, if i say 1, then condition is not true then the while loop stops.





as for conditional operations, it's if.


if(condition1)


{


do something


}


else if(condition2)


{


do something else


}


else


{


dont do anything since condition1 and 2 are not true


}


example:


if(x == 0)


{


printf("You entered 0);


}


else if(x == 1)


{


printf("You entered 1);


}


else


{


printf("You entered a number other than 0 or 1);


}


remember, in the condition you always need two equal sign!


also you can have multi condition if statements


if(condition1 %26amp;%26amp; condition2)


{


do something


}


this means if condition1 and 2 are true.


if(condition1 || condition2)


{


do something


}


this means if condition1 OR 2 is true.


hope i was a bit og help. if you have question about what I just said, email me :-)

carnation

No comments:

Post a Comment