Loops

Available

A lecture snippet with a mentor as part of the Codegym University course. Sign up for the full course.


"Hi."

"Hi, Ellie!"

"It's time to learn about loops. Loops are as simple as if/else statements, but even more interesting. You can use a loop to execute any command or a block of commands multiple times. In general, a loop looks like this:"

Loop (example 1)
while(boolean condition)
    command;
Loop (example 2)
while(boolean condition)
    block of commands in curly brackets

"It's all very simple. A command or block is executed again and again as long as the loop condition is true. First, the condition is checked. If the condition is true, the loop body (block of commands) is executed. The condition is checked again. If the condition is true, the loop body is executed again. This repeats until the condition ceases to be true."

"What if it is always true or always false?"

"If it is always true, then the program will never stop running: it will repeat the loop indefinitely. If it's always false, then the loop body will never be executed."

Here are some examples:

Java code Description
int i = 3;
while (i >= 0)
{
    System.out.println(i);
    i--;    //Decrease by 1
}
3
2
1
0
int i = 0;
while (i < 3)
{
    System.out.println(i);
    i++;   //Increase by 1
}
0
1
2
boolean isExit = false;
while (!isExit)
{
    String s = buffer.readLine();
    isExit = s.equals("exit");
}
The program will print strings from the keyboard until the string 'exit' is input.
while (true)
    System.out.println("C");
The program will repeatedly display the letter C on screen.
while (true)
{
    String s = buffer.readLine();
    if (s.equals("exit"))
        break;
}
The program will read strings from the keyboard until the string 'exit' is input.

"After conditional statements, this doesn't seem complicated. I already want to try it."

Comments (25)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Anonymous #11534938
Level 5 , Darmstadt, Germany
22 July, 20:36
poor explained👎
Benjamin Joseph Lentine
Level 13 , Songwon, South Korea
4 July, 00:55
This was probably the most informative lesson yet. That video was extremely helpful. With this knowledge, I created a word guessing "game" the program has a word and you have to type in a word to see if it matches. It's a terrible program, but it works. Now I just have to find out how to add a pool of words for the program to pick and have it randomly pick them. Then maybe add hints based on the user's input. I have a project now, I guess.
tomas weldetinsae
Level 8 , Stockholm, Sweden
5 June 2021, 11:25
The third example is not making sense for me at all. Someone wiling to help??
Francine Blanc
Level 25 , London, United Kingdom
10 June 2021, 14:11
Hi Tomas In the third example, we are setting isExit to be false initially, then in the while loop, we check that it is true that isExit continues to be false - i.e. we are just confirming that isExit evaluates to false. Within the loop, the user can continue to enter text in as input but once the input string is "exit" i.e. s.equals("exit") is true, this will mean isExit evaluates as being true. The code within the while loop will therefore not execute because it is now no longer true that isExit is false. It is a bit confusing to get your head around at first. I hope this explanation helped a bit.
Agent Smith
Level 38
8 August 2020, 17:38
There is also a second version of while loop called do-while. It executes the while body before checking the condition. You can read about it here - while and do-while
Dinesh
Level 7 , Delhi, India
29 October 2020, 20:23
Thanks
Brandon Leirer
Level 7 , Keller, United States
28 May 2020, 21:47
boolean isExit = false; while (!isExit) { String s = buffer.readLine(); isExit = s.equals("exit"); Can someone explain " while (!isExit) " ? if isExit is set to false, wouldn't this code be saying," while isExit is not false run this loop."?
Deepak
Level 7
29 May 2020, 10:08
Yes it will be set to (!isExit) true and while loop will run until it the user inputs "exit" from the keyboard
Erik Kristofer Anderson
Level 15 , Chicago, United States
14 April 2020, 21:19
I'm confused by the following Java code and description: """
boolean isExit = false;
while (!isExit)
{
    String s = buffer.readLine();
    isExit = s.equals("exit");
}
The program will print strings from the keyboard until the string 'exit' is input. """ I don't see a print statement. Is this an error?
Rod Johnston Software Developer
9 May 2020, 22:51
Hi Erik, I think it's an error. It should say "The program will read strings from the keyboard untl the string 'exit' is input.
Yuliya Sheludyakova
Level 19 , Dusseldorf, Germany
4 October 2019, 19:44
There is a mistake in the code entry exercise: boolean must start with a small letter since it's a keyword.
Kent Hervey Software Engineer/Consult Expert
15 October 2019, 02:59
Or, it will still work as an object instead of a primitive
Yuliya Sheludyakova
Level 19 , Dusseldorf, Germany
15 October 2019, 12:29
Oh, you're right, I didn't think about it back then. But anyway I guess their intention was to use a primitive type boolean instead of a Wrapper class Boolean, since it's like this in the example above + it might be confusing for total beginners, who are not yet familiar with the Wrapper classes topic (it comes only in Level 10).
Renat Mukhametshin
Level 16 , Pervouralsk, Russain Federation
27 March 2019, 13:37
ok!!!
Hashirama
Level 26 , Port-Harcourt, Nigeria
5 January 2019, 11:39
IF you are confused with example 3 just signal me!!!
Philip Obiorah
Level 16 , Port - Harcourt, Nigeria
14 January 2019, 21:20
... So while is NOT FALSE (true) it keeps reading from the keyboard, unitl isExist is set to true , and then it runs as while NOT TRUE (false). and stops.
Hashirama
Level 26 , Port-Harcourt, Nigeria
15 January 2019, 09:02
Exactly. In addition: the code in the while loop can only execute when the while condition is true, but when the condition is false, the block of code is skipped.
// Java Poser
Level 18 , Cincinnati, United States
10 February 2019, 14:36
So the 3rd and 5th example are the exact same thing? if so, which is most efficient?
Hashirama
Level 26 , Port-Harcourt, Nigeria
10 February 2019, 16:40
I don't think any beat the other. But to me, I prefer the 5th since it will save me space for variable declaration.
Fa Yu
Level 16 , Tashkent, Uzbekistan
29 May 2019, 06:54
if someone confused better to try it on intellij as i did and check how it works with different options
Laurence Chadwell
Level 7 , San Antonio, United States
29 August 2019, 00:38
The program keeps running until you type exit.
jamylam
Level 26 , Nairobi, Kenya
11 November 2019, 07:07
When you type exist, does that make condition to be true? thus exist the loop?
Maryem Vickers
Level 7 , HT..., United Kingdom
28 July 2020, 19:05
XD Well I am because there is an...
ERROR
... XD
Michael Martin
Level 19 , Arlington, United States
2 November 2018, 19:24
For examples 3 and 5, I think the Description should say "The program will enter strings...." or "The program will input strings...." instead of "The program will print string...."
Hashirama
Level 26 , Port-Harcourt, Nigeria
5 January 2019, 11:38
I totally agree with you.