473,395 Members | 1,502 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,395 software developers and data experts.

Can any one expain how the following program works?

itiger
12
Expand|Select|Wrap|Line Numbers
  1. class SwitchTest
  2. {
  3.     public static void main(String args[])
  4.     {
  5.         int x = 5;
  6.         switch(x)
  7.         {
  8.             case 1:
  9.                 System.out.println("In 1st case");
  10.             return ;
  11.             case 3:
  12.                 System.out.println("In 3rd case");
  13.             return ;
  14.             case 5:
  15.                 System.out.println("In 5th case");
  16.             return ;
  17.             case 7:
  18.                 System.out.println("In 7th case");
  19.             return ;
  20.             default:
  21.                 System.out.println("In default case");
  22.         }
  23.         System.out.println("Out of switch");
  24.     }
  25. }
  26.  
Output:
In 5th case

What does return keyword returning here. Main() is not returning anything (void), So how the program is running without any error even though we are retuning something in switch.
Jan 15 '11 #1
1 1128
Dheeraj Joshi
1,123 Expert 1GB
You are returning in the switch statement. That is perfectly alright. Return statement returns the control to the caller. In this case with out any value.

In this program case 5 is matching value. So it prints and returns the control.

If you take x as 8 then the output will be
Expand|Select|Wrap|Line Numbers
  1. In default case
  2. Out of switch
  3.  
Why you are expecting any errors in this code?

Regards
Dheeraj Joshi
Jan 15 '11 #2

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

Similar topics

3
by: Danny | last post by:
VB6 windows XP home I run a small app that has no forms in it, only a few BAS modules. It's function is to open a text file and load a few pieces of data into memory and then periodically...
7
by: devesh.agrawal | last post by:
Hi , this program does a segfault in g++3.2 onwards , works with g++2.95 , Any ideas are welcome , basically there is this instruction when compiled with -S option , call *%eax , where the one...
6
by: PengYu.UT | last post by:
Hi, I run into error with the following program. Would you please help me? Best wishes, Peng struct tag1{}; struct tag2{};
4
by: Denise Mills | last post by:
Hi, On my previous Windows 98 machine, I wrote a simple C program that takes two text files as input, performs some calculations, then produces a text file that contains the results. It worked...
10
by: free2cric | last post by:
Hi, FOllowing is a program which is written by someone else. Its output is a wonderful poem. I dont understand how it works really. can anyone tell. thanks cric #include <stdio.h> main(int...
2
by: mikelinyoho | last post by:
Does the following program architecture make sense? #include <stdio.h> #include "test1.c" #include "sample.cpp" int main(void){ output();
1
by: Morrison | last post by:
My HelloWorld program works on localhost but not on server. I have 2 hello world applications, one in VS 2003 and other in VS 2005. Both programs is working on my computer but if I uppload it on...
10
by: sunny | last post by:
Does this following program implement the factory design.if not what are things that i have to change in order to make this following program to be designed to factory design pattern. ...
16
by: Ravi | last post by:
main() { float a = 5.375; char *p; int i; p = (char*)&a; for(i=0;i<=3;i++) printf("%02x",(unsigned char)p); } the above code gives the binary representation of a.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.