Skip to the content
onlineexamguide
  • Home
  • Courses
  • Engg. Interview
    • Placement Papers
    • Electrical Engineering
    • Mechanical Engineering
    • Automobile Engineering
    • Civil Engineering
    • Computer Science Engineering
    • Chemical Engineering
    • CS & IT Engg.
      • C programming Tests
      • C++ programming Tests
      • Java Programming Tests
  • Online Exam
    • NTA UGC NET Exam
    • SSC Examination quiz
    • TET Examination Quiz
    • Banking Exam
    • Aptitude Tests
    • Computer Knowledge Tests
    • Logical Reasoning Tests
    • English Language Tests
    • Staff Nurse Exams
    • General Knowledge Tests
    • Networking Tests
  • Ghatna Chakra
  • Register
    • Instructor Registration
    • Student Registration
    • About us
    • Privacy Policy
  • Cart
  • User Login
  • Home
  • Courses
  • Engg. Interview
    • Placement Papers
    • Electrical Engineering
    • Mechanical Engineering
    • Automobile Engineering
    • Civil Engineering
    • Computer Science Engineering
    • Chemical Engineering
    • CS & IT Engg.
      • C programming Tests
      • C++ programming Tests
      • Java Programming Tests
  • Online Exam
    • NTA UGC NET Exam
    • SSC Examination quiz
    • TET Examination Quiz
    • Banking Exam
    • Aptitude Tests
    • Computer Knowledge Tests
    • Logical Reasoning Tests
    • English Language Tests
    • Staff Nurse Exams
    • General Knowledge Tests
    • Networking Tests
  • Ghatna Chakra
  • Register
    • Instructor Registration
    • Student Registration
    • About us
    • Privacy Policy
  • Cart
  • User Login

Loops in c language While For Do While MCQ

Loops in C

Learn Loops in c language While For Do While MCQ Questions and Answers on Basics to attend job placement exams, interview questions, college viva and Lab Tests

Loops help to repeatedly execute a set of instructions as long as loop condition is satisfied. Condition can be a combination of relational operators and logical operators.

Loops in c language

C language comes with three loops.

  1. while loop
  2. for loop
  3. do while loop

C language provides two more keywords BREAK and CONTINUE to provide more control over loops.

break; Statement

break; statement with a semicolon at the end breaks the current loop where it is present. It can not affect outer loop in a nested loop program.

continue; Statement

continue; statement with a semicolon at the end skips executing next statements in the current loop and execution goes to the beginning of loop with next loop counter.

Increment Operator

++ is called Increment operator. It adds +1 to the variable.

Decrement Operator

— is called Decrement operator. It subtracts -1 from the variable.

1. WHILE Loop

‘while’ loop executes a group of statements under the loop until a loop condition is true. Loop counter is maintained to exit the loop after crossing a certain number in most of the cases. If the loop condition fails for the first time itself, no statements are executed. Program control goes to the first statement after the while loop block.

Syntax:

while ( condition )
 statements;

Loops in c language examples

Example 1:

int main()
{
  int a=1;
  while(a <= 10)
  {
    printf("%d,", a);
    a++;  //increment or decrement
  }
  return 0;
}
//output: 1,2,3,4,5,6,7,8,9,10

In the above example, we printed all numbers from 1 to 10. If you forget to increment a using a++ statement, loop will never end and the program crashes.

Loops in c language

Example 2: While with break statement

int main()
{
 int a=1;
 while(true) //true makes while loop infinite loop
 {
    printf("%d,", a);
    a++;
    if(a>10)
      break; //breaks the while loop
 }
 return 0;
}
//output: 1,2,3,4,5,6,7,8,9,

Here we are depending on break statement to come out of loop instead of LOOP CONDITION.

Loops examples

Example 3: While with continue statement

int main()
{
 int a=0;
 while(a<=10) //true makes while loop infinite loop
 {
    a++;
    if(a%2 == 0)
      printf("%d", a);
    else
      continue; //it skips printf("*")
    print("*");
 }
 return 0;
}
//output: 2*4*6*8*10 

Loops in c language

Example 4: Nesting of While loop

You can nest a while inside another while loop. You can use break and continue with inner and outer loops if required.

int main()
{
  int a=1, b=1;
  //outer loop
  while(a <= 3)
  {
    b=a;
    //nested while loop
    //inner loop
    while(b <= 3)
    {
      printf("%d,", b);
      b++;
    }
    printf("\n");
    a++;
  }
  return 9;
}
//output
//1,2,3,
//2,3,
//3,

2. FOR Loop

A FOR loop works like a WHILE loop except that FOR loop includes both INITIALIZATION section and INCREMENT section along with CONDITION section.

Syntax:

for(INITIALIZATION ; CONDITION ; INCREMENT OR DECREMENT)
  statements;

Loops in c language

Example 1:

int main()
{
  int a;
  for(a=1; a<=5; a++)
   printf("%d,", a);
  return 9;
}
//output: 1,2,3,4,5

The variable ‘a’ is initialized with 1 in the for loop. a++ increments the value by 1.

Order of execution of FOR loop

  1. Initialization part is executed only for the fist time.
  2. For loop condition is checked. If the condition is satisfied, loop statements are executed.
  3. After execution of loop statements, program control goes to INCREMENT or DECREMENT part. This completes First Iteration.
  4. Second Iteration starts with CONDITION check. If the condition is satisfied, loop statements are executed for the second time followed by INCREMENT / DECREMENT.

Note: You can skip all three sections of FOR loop like for( ; ; ){ }. You should add code to break the loop with break; statement. Other wise, it becomes an infinite loop.

Nesting of FOR loop:

You can nest one for loop inside another for loop. You can also nest a while inside for loop, for loop inside do-while loop and so on. Here is an example.

int main()
{
  int a, b;
  //outer for loop
  for(a=1; a<=3; a++)
  {
    //inner for loop
    for(b=a; b<=3; b++)
    {
      printf("%d,", b);
    }
    printf("\n");
  }
  return 9;
}
//output:
// 1,2,3
// 2,3
// 3

You can use break and continue statements inside for loop for better coding and performance.

3. DO WHILE loop

A do-while loop is different from FOR loop and WHILE loop because a do-while loop executes loop statements at least once even if the loop condition fails for the very first time.

Syntax:

do
{
  STATEMENTS;
}while( CONDITION );
//SEMICOLON IS REQUIRED.

C Programming Loops While For Do While

Example 1:

int main()
{
  int a=1;
  do
  {
    printf("%d,", a);    
    a++;
  }while( a <=10 );
}
//output:
//1,2,3,4,5,6,7,8,9,10

Order of execution of DO WHILE loop

  1. Statements of a DO-WHILE loop is executed at least once. First iteration is over.
  2. CONDITION is checked. If the condition is satisfied, second iteration starts. If the condition fails, control goes to the next statement after DO-WHILE.

Nesting of DO WHILE loop

You can nest a do-while loop inside another do-while loop or any other loop like FOR or WHILE. To better control a do-while loop, you can use break and continue statements.

Example:

int main()
{
  int a=1, b=1;  
  do
  { 
    b=a;
    do
    {
      printf("%d,",b);
      b++;
    }while(b <=3);
    print("\n");
    a++;
  }while(a<=3);

  return 9;
}
//output
//1,2,3,
//2,3,
//3,

Loops in c language

[WpProQuiz 28]

Write a comment Cancel reply

You must be logged in to post a comment.

Recent Posts

  • Atal Bihari Vajpayee | श्री अटल बिहारी वाजपेयी जीवन परिचय
  • सम्राट अशोक का जीवन परिचय (Emperor Ashoka)
  • Prithviraj Chauhan
  • बाल गंगाधर तिलक
  • स्वामी दयानंद सरस्वती
  • UPPSC Mines Inspector Recruitment 2022 Notification Out
  • AIIMS Delhi JR Vacancy 2022 [194 Post] Notification and Apply Online
  • भीमराव अम्बेडकर
  • डॉक्टर राजेंद्र प्रसाद का जीवन परिचय
  • श्रीनिवास रामानुजन का जीवन परिचय
  • Amnesty International day
  • World Economic Forum
  • UPSSSC VDO Syllabus and Exam Pattern 2022
  • RBI Officer Grade B Recruitment 2022
  • UKMSSB Assistant Professor Recruitment 2022 Apply Now 339 Post

About us

Free online test to practice for Competitive exams , Online Exam, Entrance and Interview. Learn and Practice online test for Free and Prepare for your exam online with us

Select Language

Follow us

Search

Learn and Earn

Register as Instructor - Create and sell online courses and coaching services with the best online platform onlineexamguide.com . Build a course, build a brand, earn money

Copyright © 2022 onlineexamguide.com - All Rights Reserved.
error: Content is protected !!

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.