While Loops | Basics of C++ - Software Development 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 | Basics of C++ - Software Development is a part of the Software Development Course Basics of C++.
All you need of Software Development at this link: Software Development
70 videos|45 docs|15 tests

Top Courses for Software Development

70 videos|45 docs|15 tests
Download as PDF
Explore Courses for Software Development exam

Top Courses for Software Development

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

shortcuts and tricks

,

practice quizzes

,

Summary

,

Important questions

,

While Loops | Basics of C++ - Software Development

,

past year papers

,

Previous Year Questions with Solutions

,

Free

,

While Loops | Basics of C++ - Software Development

,

Semester Notes

,

MCQs

,

Extra Questions

,

pdf

,

While Loops | Basics of C++ - Software Development

,

mock tests for examination

,

Objective type Questions

,

Sample Paper

,

video lectures

,

ppt

,

Exam

,

Viva Questions

,

study material

;