Can we write code using if else statements without declaring value to the variable? | Core Java Forum
Harika Posted on 04/07/2023

I want to run a code on if else statement but i want to give values to the variables while i'm running the code will it possible? if yes how can i write the program without giving values to the variables.


Yogesh Chawla Replied on 06/07/2023

Yes it is possible in number of ways. You can use Scanner API for this. See example below:

package com.programming.class18;

import java.util.Scanner;

public class KeepAddingTillMoreThanHundredOutputComes {

	public static void main(String[] args) {
		System.out.println("Enter some numbers to add: ");
		Scanner scanner = new Scanner(;
		int count = 0;
		while (scanner.hasNext()) {
			count = count + scanner.nextInt();
			if (count > 100) {
				System.out.println("Max exceeded!");