Conditional structures/C: Difference between revisions

Content added Content deleted
m (often)
m (→‎if-then-else: consistent indent; removed {{works with}})
Line 2: Line 2:


===if-then-else===
===if-then-else===
{{works with|gcc|4.1.2}}


<lang c>int main (int argc, char ** argv) {
<lang c>int main (int argc, char ** argv) {
int input = 2;
int input = 2;


if ( 3 == input ) {
if (3 == input) {
/* Do something */
/* Do something */
}
}




if ( 3 == input ) {
if (3 == input) {
/* Do something */
/* Do something */
} else {
} else {
Line 21: Line 20:
One line predicates do not require curly braces (although their usage is often preferred for readability):
One line predicates do not require curly braces (although their usage is often preferred for readability):


<lang c>if(cond)
<lang c>if (cond)
expr;
expr;

if(cond)
if (cond)
expr;
expr;
else
else
expr;</lang>
expr;</lang>


And these may be mixed:
And these may be mixed:


<lang c>if(cond)
<lang c>if (cond)
expr;
expr;
else
else
{
{
Line 37: Line 37:
}</lang>
}</lang>


<lang c>if(cond)
<lang c>if (cond)
{
{
/* multiple expressions */
/* multiple expressions */