C Programming Tutorial

C Control Flow Statements

Controlling Exection of Statements

if statement, Switch Case

Loops -> For loop, While Loop, Do-While Loop

If Statement

Switch Statement

     Swtich statement is a multi-way conditional statement, generalizing the if-else statement

Constant integral expressions following case labels must all be unique. if there is no break statement execution "falls through" to the next case statement

The switch statement allows for multiple branches from a single expression. It's more efficient and easier to follow than a multileveled if statement. A switch statement evaluates an expression and then branches to the case statement that contains the template matching the expression's result. If no template matches the expression's result, control goes to the default statement. If there is no default statement, control goes to the end of the switch statement

switch statement works on following data types char,short,int and long, floating point types, float double , long double, are not supported in switch statement

switch case syntax:
			case const-expr1: statements;
			case const-expr2: statements;
			default: statements;

For loop

for loop synatx:

		for(expr1; condition;expr2)

expr1 for initializing a variable
condition executes for every iteration,true enters into loop,false exists from loop,
expr2 usually incremental value

		 for(int i=0;  i < 10; i++) 
		//here  expr1 is int i=0;
		//      condition i < 10
		//      expr2   is i++

as long as condition is true loop executes printf statement

While loop

while loop synatx:


while loop condition is +ve and -ve values enter into loop, for 0 exits from loop

Infinity loops

		while (1){
		while (x=2){

find number is power of 2 or not using While loop

        int x=10;
        while (1){
                printf("Please enter Number:");
                scanf("%d",  &x);
                if(x  ==  -1) 

                char *ptr = ( (x&(x-1)) == 0)? "yes": "no" ;
                printf("%s\n",  ptr);

Please enter Number:1
Please enter Number:2
Please enter Number:3
Please enter Number:4
Please enter Number:32
Please enter Number:-1


Do While loop

Do while loop synatx:


Unconditional Jumps

    Jump statements interrupts the sequential execution of the program. so that execution starts at different point in the program. There are four statements that cause unconditional jumps: break,continue,goto,return

  • break
  •       break statement can be used in loop statements or switch statements. which causes the control paases to immediately next statement after the loop or switch statement.

  • continue
  •       The continue statements can be used only within the body of the loop, and causes the program flow to skip over the rest of the iteration of the loop.

  • goto
  •       The goto statement causes the unconditional jump to another statement with in the same function.

  • return
  •       The return statement ends execution of the current function. and jumps back to where the function was called.