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;

}

This doc is part of
70 videos|45 docs|15 tests
Join course for free

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.
Download the notes
While Loops
Download as PDF
Download as PDF

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.
Take a Practice Test
Test yourself on topics from Software Development exam
Practice Now
Practice Now

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
Are you preparing for Software Development Exam? Then you should check out the best video lectures, notes, free mock test series, crash course and much more provided by EduRev. You also get your detailed analysis and report cards along with 24x7 doubt solving for you to excel in Software Development exam. So join EduRev now and revolutionise the way you learn!
Sign up for Free Download App for Free
70 videos|45 docs|15 tests

Up next

70 videos|45 docs|15 tests
Download as PDF

Up next

Explore Courses for Software Development exam
Related Searches

ppt

,

While Loops | Basics of C++ - Software Development

,

Exam

,

Sample Paper

,

Semester Notes

,

study material

,

Viva Questions

,

mock tests for examination

,

pdf

,

While Loops | Basics of C++ - Software Development

,

While Loops | Basics of C++ - Software Development

,

Objective type Questions

,

MCQs

,

Important questions

,

video lectures

,

Extra Questions

,

shortcuts and tricks

,

Previous Year Questions with Solutions

,

practice quizzes

,

Free

,

Summary

,

past year papers

;