Help with loop | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Help with loop

Please explain someone what is int i here You are given code that takes the number of students who enter the university as input. Let's greet them! Task Complete the program to output "Welcome" for each student. import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); for (int i = 0; i < n; i ++) { System.out.println("Welcome"); } } }

21st Jul 2022, 12:36 AM
Maria Shtokalo
Maria Shtokalo - avatar
4 Answers
+ 3
Summary: Int i in that programm's simply a value to count the Times you did a Action and give you a numeric value to make a while question. Also "int n" is the numeric value of how many students we have and "int i" is a value to Count and compare. Let's explain it with an example. We have 9 students also n = 9, int i start with 0 and every time the Loop gets tho one time the value i get increased by 1, so: 1. Loop: i=0 2. Loop: i=1 3. Loop: i=2 4. Loop: i=3 And to Escape the loop we have the condition that i is smaller than n. So when we got tho the loop 9 times 'ur counter value have the value of 8, which's the Last value under 9, so the Loop stops. edit: Sry when it's a bit unreadable and roughly made answer, we have midnight here. 😅
21st Jul 2022, 1:09 AM
Felix Alcor
Felix Alcor - avatar
+ 1
Thank you!!!
21st Jul 2022, 2:05 AM
Maria Shtokalo
Maria Shtokalo - avatar
+ 1
i < n means that i is strictly less than n, so i runs from its initial value through (n-1). And the other answer explains it well with example. If you can find time, I would highly recommend going over high school maths glossary, that would really help with translating your program logic into code.
22nd Jul 2022, 7:44 AM
scientist
scientist - avatar
0
Felix Alcor Great answer! Explanations supported with example small input test cases, I think everyone else should follow this same template of giving solutions here to Q&A posts.
22nd Jul 2022, 7:37 AM
scientist
scientist - avatar