175k views
5 votes
Assume we are testing a variable is_sunny in a while loop. is_sunny = “n”.

Write a while clause that will continue executing a loop until is_sunny is not equal to ‘n’.

User Jcolebrand
by
6.0k points

1 Answer

5 votes

Answer:

while(is_sunny=="n")

Step-by-step explanation:

The loop keeps repeating itself until a certain condition is met.

Here while loop keeps executing until value of is_sunny is not equal to 'n'

When the value of is_sunny is not equal to n then the loop stops.

So lets explain this logic with a chunk of code:

#include <iostream> //to use input output functions

using namespace std; //to identify objects like cin cout

int main() { //start of main function

string is_sunny = "n"; //value of is_sunny is set to n

cout<<"Enter value of is_sunny: "; // prompts user to enter value of is_sunny

cin>>is_sunny; // reads value of is_sunny from user

while(is_sunny=="n") // keeps iterating until value of is_sunny is not equal to n

{ cout<<"keep executing until is_sunny is not equal to n"<<endl;//display this message with each iteration

cout<<"Enter value of is_sunny: "; //keeps prompting to enter is_sunny

cin>>is_sunny; } } //keeps reading value of is_sunny from user

Now if the user keeps entering "n" as value of is_sunny then the loop keeps repeating. When the user enters any string other than n then the loop breaks. The output of the program is attached.

Assume we are testing a variable is_sunny in a while loop. is_sunny = “n”. Write a-example-1
User Gabtub
by
6.6k points