Register | Sing In

New posts

Stacks in C programming Linked Lists in C programming File Input/Output in C programming Check prime numbers in C Programming C Programming Enumeration C Programming Union

Archives

2013-10 2013-09 2013-08

While Loop in C programming

August 25, 2013 Posted by: GPA Views: 578 0 comments

In this lesson you will learn how to use of while loop statement in C.

While Loop

 

If you want to perform the repetitious work then you use the Loop control statement. Loop repeats a statement or a process many times according to the specified conditions. 

How does it work? While loop checks the condition first, if the condition is true then all processes written in body the while loop are executed otherwise ignored all.

The Syntax for the while loop:

 

while(condition)
{
   do something
}

In this example you will learn how to use the while loop statement. The program will show the menu until user enter 4.

 

Example:

   

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]){
   int ch=0;//variable for choice
   while(ch!=4){
              printf("Main menu\n");
              printf("1- menu item 1\n");
              printf("2- menu item 2\n");
              printf("3- menu item 3\n");
              printf("4- exit\n");
              printf("Select one menu item: ");
              scanf("%d",&ch);//read user choice
             //switch control statement
               switch(ch){
                 case 1:
                  printf("You have selected menu item 1\n");
                  break;
                    case 2:
                      printf("You have selected menu item 2\n");
                      break;
                    case 3:
                      printf("You have selected menu item 3\n");
                       break;
                    case 4:
                     break;
                  default:
                 
printf("You have not selected menu item\n");
                 break;
            }//end switch  
}//end while
  return 0;
}

Output

While Loop in C example

Download this example

0 Comments...

Leave a Reply

Please, Sing In to leave a Reply for this material.



Online Chat

LiveZilla Live Help

Popular posts

Function strtok in C programming Check prime numbers in C Programming C Programming Union Linked Lists in C programming File Input/Output in C programming C Programming Enumeration

RSS-subscribe

New materials

Subscribe