473,385 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Can you solve what the output will be?

The task is to figure out what the output will be to the java code below. Apparantly the answer is:
12
28

Why 28? I thought it was 26. tal1 is 7 and tal2 is (12+7). Then the last output would be 26. Why am I wrong?

Expand|Select|Wrap|Line Numbers
  1. int tal1=7, tal2=12; 
  2.  if (tal1++ > 7) 
  3.     System.out.println(tal1); 
  4.  else 
  5.     System.out.println(tal2); 
  6.  tal2+=tal1; 
  7.  System.out.println(tal1 + tal2);
  8.  
Jan 11 '12 #1

✓ answered by Mariostg

Expand|Select|Wrap|Line Numbers
  1. int tal1=7, tal2=12;  
  2.  if (tal1++ > 7)  //This is false, so else executes.  Yet. after
  3.                   //comparison, tal1 is incremented.  tal1=8
  4.     System.out.println(tal1);  
  5.  else  
  6.     System.out.println(tal2);  
  7.  tal2+=tal1;      //tal2=tal2+tal1 = 12+8=20
  8.  System.out.println(tal1 + tal2); //8+20 = 28
  9.  

2 1371
Mariostg
332 100+
Expand|Select|Wrap|Line Numbers
  1. int tal1=7, tal2=12;  
  2.  if (tal1++ > 7)  //This is false, so else executes.  Yet. after
  3.                   //comparison, tal1 is incremented.  tal1=8
  4.     System.out.println(tal1);  
  5.  else  
  6.     System.out.println(tal2);  
  7.  tal2+=tal1;      //tal2=tal2+tal1 = 12+8=20
  8.  System.out.println(tal1 + tal2); //8+20 = 28
  9.  
Jan 11 '12 #2
Ok, now I understand. Thank you :)
Jan 11 '12 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: ES Kim | last post by:
Hi, I am using VC++ .NET 7.0. I'd like to debug my console program, using redirection of input/output for convenience. Help menu tells me it's possible to specify a file name as a command-line...
4
by: Carlo Marchesoni | last post by:
I really don't achieve to read a simple 'input.txt' with the following content: Jürg (Hex: 4a fc 72 67) to an identical 'output.txt' I do the following (and tried with tons of different...
4
by: Kevin Mansel via .NET 247 | last post by:
Ok, basically this is my problem. I'm building a console app tocall a dos program. So i'm using the Shell command to call theprogram, now depending on what happens, I want to read theoutput that...
2
by: yogesh | last post by:
char TCGI::x2c(char *what) { register char digit; digit = (char) ((what >= 'A' ? ((what & 0xdf) - 'A')+10 : (what - '0'))); digit *= (char) 16; digit += (char) ((what >= 'A' ? ((what & 0xdf) -...
5
by: John Speth | last post by:
Hi Group- I want to use the C preprocessor to generate expanded text as a text processor for software test script generation. The preprocessor output will never be compiled. I need to insert...
3
by: TC | last post by:
I'm trying to debug a console application, but I can't see the console output. I've seen many references which say that console output is supposed to appear on the Output window when the...
1
by: dwaterpolo | last post by:
Hi Everyone, I am trying to read two text files swY40p10t3ctw45.col.txt and solution.txt and compare them, the first text file has a bunch of values listed like: y y y y y y y
2
by: Gilles Ganault | last post by:
Hello I need to write PHP scripts on an AstLinux distribution. Even though I added the "-q" switch on the shebbang line, PHP still outputs some HTML: ========== # cat test.php ...
6
by: drhilbert | last post by:
Dear all, in these days, we have found a problem we can't solve even after long long googling, so we are here asking your precious help. In our program instead of using cout to print messages...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.