Welcome

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:
		switch(expression)
		{
			case const-expr1: statements;
			case const-expr2: statements;
				.
				.
				.
			default: statements;
		}
	

For loop

for loop synatx:

		for(expr1; condition;expr2)
		{
			statements....
		}
	

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++) 
			{
				printf("%d",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:

		expr1
		while(condition)
		{
			statements....
			expr2;
		}
	

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

Infinity loops

		while (1){
			statements...
		}
	
		while (x=2){
			statements...
		}
	

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) 
			break;

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

        }
	
	
output:
Please enter Number:1
yes
Please enter Number:2
yes
Please enter Number:3
no
Please enter Number:4
yes
Please enter Number:32
yes
Please enter Number:-1

	

Do While loop

Do while loop synatx:

		
		do
		{
			statements....
			
		}while(condition);
	

ADS