i+2, i++, or printf(...) terminated by a ; becomes a statement in C.{ and } are used to group statements inside a compound statement or a block.if-else the else part is optional.if checks the numeric value of an expression after evaluating it.We might do -
if(x) //non-zero value implicitly means true
//instead of
if(x != 0)
else-if.else but it is optional.switch(expression)
{
case const-expr: statements
case const-expr: statements
default: statements
}
Each case is labeled by integer-valued constants or constant expressions.
All cases must be different.
default case is optional.
IMPORTANT NOTE - Switch is fall through i.e. if we do not break the flow after a case, then it goes to the next case until it is stopped explicitly.
Several cases can be attached to a single action using this fall through property.
#include<stdio.h>
int main()
{
char sec;
scanf("%s", &sec);
switch (sec)
{
case 'a':
case 'A':
case 'b':
case 'B':
case 'c':
case 'C': printf("Your class has 10 students.");
break;
case 'd':
case 'D':
case 'e':
case 'E': printf("Your class has 11 students.");
break;
default: printf("Section not found.");
break;
}
return 0;
}
for and while loops are basically the same thing.for loop are expressions and either of them can be omitted, but the semicolons must remain.for loop is infinite, even if we do not update.for loop.
for (int i = 0, j = 1; i < counti, j < countj; i++, j++)
{
/* code */
}
do block is optional but the while part can be mistaken for start of a while loop.break causes an exit from the innermost enclosing loop or switch.continue causes the next iteration of the loop to begin.continue has no meaning in switch.continue inside a switch inside a loop will cause the loop to jump to next iteration.goto can jump to a label anywhere inside the same function. A label follows same naming conventions as that of variables.