Clouds
Home
Tutorial of C language
Computer Language
Introduction to C
Programming in C
Structure of C program
Variable in C
Datatypes in C
Token in C
Operator in C
Precedence of C Operator
Assignment Operator in C
Logical Operator in C
Bitwise Operator in C
Ternary Operator in C
C Input Output
Pointers in C
Storage Classes of C
Control Structure of C
C Decision Making Statement
C Looping Statement
C Jumping Statement
C Function
Function Call By Value
Function Call By Reference
C Input Output Function
C Structure
Array in C
Union in C

Looping Statements

While loop

The most basic loop in C is the while loop.A while statement is like a repeating if statement. Like an If statement, if the test condition is true: the statments get executed. The difference is that after the statements have been executed, the test condition is checked again. If it is still true the statements get executed again.This cycle repeats until the test condition evaluates to false.

Syntax:-

				While (condition)
				{
					
					
				}
				

Example:-

				#include<stdio.h>
				#include<conio.h>
				Void main ()
				{
					int a=1;
				 	clrscr();
				 	While (a<=5)
				 	{
				  		printf("Hello World\n");
				  		a++;
				 	}
					getch();
				}
				

Question. Write a program to print the NIMS University 5 times.

				#include<stdio.h>
				#include<conio.h>
				Void main ()
				{
				 	int a=5;
				 	clrscr();
				 	While (a>=1)
				 	{
				  		printf("Hello World\n");
				  		a--;
				 	}
					getch();
				}
				

Question:- Write a program to Print the 1 to 5 using while loop.

				#include<stdio.h>
				#include<conio.h>
				void main ()
				{
				 	int a=1;
				 	clrscr();
				 	While (a<=5)
				 	{
				  		printf("%d\n",a);
				  		a++;
				 	}
					getch();
				}
				

Question:- Write a program to Print the 5 to 1 using while loop.

				#include<stdio.h>
				#include<conio.h>
				void main ()
				{
				 	int a=5;
				 	clrscr();
				 	While (a>=1)
				 	{
				  		printf("%d\n",a);
				  		a--;
				 	}
					getch();
				}
				

For loop

for loop is similar to while loop, it's just written differently. for statements are often used to proccess lists such a range of numbers:

Syntax:-

				for(initialisation; condition;  increment/decrement)
				{
					
				}
				
Note: Initialisation ,Condition and Increment/decrement are optional.

Example:-

Question:- Write a program to print the Hello World 10 times.
				#include<stdio.h>
				#include<conio.h>
				void main()
				{
					int i;
					clrscr();
					for(i=1;i<10;i++)
					{
						printf("\nHello World");
					}
				}
				

Question: -Write a program to print the 1 to 10 using for loop.

				#include<stdio.h>
				#include<conio.h>
				void main()
				{
					int i;
					clrscr();
					for(i=1;i<10;i++)
					{
						printf("%d\n",i);
					}
					getch();
				}
				

Question:- Write a program to print the 10 to 1 using for loop.

				#include<stdio.h>
				#include<conio.h>
				void main()
				{
					int i;
					clrscr();
					for(i=10;i>=1;i--)
					{
						printf("\n",i);
					}
					getch();
				}
				

Question:- Write a program to print the alternate number between 1 to 10.

				#include<stdio.h>
				#include<conio.h>
				void main()
				{
					int i;
					clrscr();
					for (i=1;i<=10;i+=2)
					{
						printf("\n",i);
					}
					getch()
				}
				

Do-while loop

Do- while is just like a while loop except that the test condition is checked at the end of the loop rather than the start. This has the effect that the content of the loop are always executed at least once.

Syntax:-

				do
				{
					
					
				}
				While(condition);
				

Question:- Write a program to Print NIMS University Jaipur 10 times.

				Void main()
				{
				 	int a=1;
					clrscr();
					do
					{
				 		printf("Hello World\n");
				 		a++;
					}
					while(a<=10);
					getch();
				}
				

Question:- Write a program to Print the 1 to 10 using do-while loop.

				Void main()
				{
				 	Int a=1;
				 	do
				 	{
				  		printf("%d\n",a);
				  		a++;
					}
					while(a<=10);
					getch();
				}
				

Question:- Write a program to Print the 10 to 1 using do-while loop

				Void main()
				{
				 	int a=10;
				 	Clrscr();
				 	do
				 	{
				 		printf("\n",a);
				 		a--;
					}
					while(a>=1);
					getch();
				}
				

Switch Statement:

The switch statement is much like a nested if .. else statement. Its mostly a matter of preference which you use, switch statement can be slightly more efficient and easier to read.

Example:-

				#include<stdio.h>
				#include<conio.h>
				void main()
				{
				 	int choice;
				 	clrscr();
				 	printf("Enter the choice:");
				 	scanf("%d",&choice);
				 	switch(choice)
				 	{
				  		case 1:
							printf("One");
							break;
				  		case 2:
							printf("Two");
							break;
				  		case 3:
							printf("Three");
							break;
				  		default:
							printf("Invalid");
				  	}
				  	getch();
				 }
				 

Example:-

				#include 
				#include<conio.h>
				void main()
				{
					int  Grade = 'A';
				    switch( Grade )
				    {
				        case 'A' : printf( "Excellent\n" );
				        case 'B' : printf( "Good\n" );
				        case 'C' : printf( "OK\n" );
				        case 'D' : printf( "Mmmmm....\n" );
				        case 'F' : printf( "You must do better than this\n" );
				        default  : printf( "What is your grade anyway?\n" );
				     }
				}
				
This will produce following result:
Excellent
Good
OK
Mmmmm....
You must do better than this
what is your grade anyway?

Example:-

				#include<stdio.h>
				#include<conio.h>
				void main()
				{
				 	char choice;
				 	clrscr();
				 	printf("Enter the choice:");
				 	scanf("%c",&choice);
				 	switch(choice)
				 	{
				  		case 'r':
							printf("Red");
							break;
				  		case 'b':
							printf("Blue");
							break;
				  		case 'g':
							printf("Green");
							break;
				  		default:
							printf("Invalid");
				  	}
				 getch();
				 }
				

Example:-

				#include<stdio.h>
				#include<conio.h>
				void main()
				{
				 	int choice,a=5,b=2,sum,sub,mul;
				 	clrscr();
				 	printf("Enter the choice:");
				 	scanf("%d",&choice);
				 	switch(choice)
				 	{
				  		case 1:
							sum=a+b;
							printf("sum of a & b=%d",sum);
							break;
				  		case 2:
							sub=a-b;
							printf("subtract of a & b=%d",sub);
							break;
				  		case 3:
							mul=a*b;
							printf("Multiplay of a & b=%d",mul);
							break;
				  		default:
							printf("Invalid");
				  	}
				 getch();
				 }
				

C Jumping Statement >>
C Function >>
Function Call By Value >>
Function Call By Reference >>
C Input Output Function >>


Home Clouds