EmSAT Achieve Exam  >  EmSAT Achieve Notes  >  C++ for EmSAT Achieve  >  While Loops

While Loops | C++ for EmSAT Achieve PDF Download

In programming, loops are used to execute a set of statements repeatedly until a particular condition is met. One such loop in C++ is the while loop. In this article, we will explore the concept of while loops in C++, including its syntax, working, and examples.

Syntax of C++ While Loop

The syntax for a while loop in C++ is as follows:

while (condition) {

  // code to be executed

}

  • The keyword 'while' indicates the start of the loop.
  • The condition is a Boolean expression that is evaluated at the start of each iteration. If it evaluates to true, the code inside the loop is executed.
  • The code inside the loop is enclosed in curly braces {}.

How While Loop Works in C++

The while loop works as follows:

  • The condition is evaluated first.
  • If the condition is true, the code inside the loop is executed.
  • After executing the code, the condition is checked again.
  • This process continues until the condition becomes false.

Example of C++ While Loop

Let's see an example of a while loop that prints numbers from 1 to 5.

#include <iostream>

using namespace std;

int main() {

  int i = 1;

  while (i <= 5) {

    cout << i << " ";

    i++;

  }

  return 0;

}

Output

1 2 3 4 5

  • The variable 'i' is initialized to 1.
  • The condition checks if 'i' is less than or equal to 5.
  • Inside the loop, we print the value of 'i' and increment it by 1.
  • This process continues until 'i' becomes 6, which violates the condition and stops the loop.

Using While Loop to Check User Input

The while loop can also be used to check user input until a valid value is entered. Let's see an example:

#include <iostream>

using namespace std;

int main() {

  int num;

  do {

    cout << "Enter a positive integer: ";

    cin >> num;

  } while (num <= 0);

  cout << "You entered: " << num;

  return 0;

}

  • The loop checks if the input value is less than or equal to zero.
  • If the input is not valid, the loop continues to prompt the user to enter a positive integer.
  • Once a valid input is entered, the loop stops and displays the input value.

Conclusion

While loops in C++ are an essential tool for executing a set of statements repeatedly until a particular condition is met. We have learned the syntax and working of while loops and explored examples that demonstrate their use. With practice, you can master the use of while loops in your programming projects.

The document While Loops | C++ for EmSAT Achieve is a part of the EmSAT Achieve Course C++ for EmSAT Achieve.
All you need of EmSAT Achieve at this link: EmSAT Achieve
70 videos|45 docs|15 tests

Top Courses for EmSAT Achieve

70 videos|45 docs|15 tests
Download as PDF
Explore Courses for EmSAT Achieve exam

Top Courses for EmSAT Achieve

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Viva Questions

,

While Loops | C++ for EmSAT Achieve

,

video lectures

,

past year papers

,

Free

,

Important questions

,

While Loops | C++ for EmSAT Achieve

,

study material

,

Semester Notes

,

Sample Paper

,

While Loops | C++ for EmSAT Achieve

,

Previous Year Questions with Solutions

,

practice quizzes

,

Summary

,

pdf

,

mock tests for examination

,

ppt

,

Objective type Questions

,

Exam

,

Extra Questions

,

MCQs

,

shortcuts and tricks

;