0% found this document useful (0 votes)
55 views2 pages

Programming Concepts Lab 4 Solutions

This document contains the details of Lab #4 for a Programming Concepts course, including sample code snippets and questions. The code snippets demonstrate the use of switch statements, if/else statements, and for loops. The questions ask the student to determine the output of the code snippets and to convert an if/else statement to a switch statement.

Uploaded by

ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views2 pages

Programming Concepts Lab 4 Solutions

This document contains the details of Lab #4 for a Programming Concepts course, including sample code snippets and questions. The code snippets demonstrate the use of switch statements, if/else statements, and for loops. The questions ask the student to determine the output of the code snippets and to convert an if/else statement to a switch statement.

Uploaded by

ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Al-ZaytoonahUniversity of Jordan ‫جـامعـة الـزيتـونــــة األردنيــة‬

Course Name: Programming Concepts

Instructor: Nesreen Hamad

Lab #4

Name of student Number of student

Q1. What is the output of the following code segments


char op='?';
switch(op)
{case '*' : ! = exclamation mark
System.out.println(op + " = multiply" ); End of output question
break; *
case '?':
op='!';
System.out.println(op + " = exclamation
mark");
default:
op='*';
System.out.println("End of output question");
}
System.out.println(op);

int x=10;
char ch='*'; * 103
if(x<=10) 12
if(ch=='+')
System.out.println(ch + " " + x);
else if(ch=='?')
System.out.println((x+3));
else
System.out.println(ch + " " + x + 3);
else
System.out.println((x+4));
x=x+2;
System.out.println(x);

int count;
for (count=1;count<=5;count++) Infinite loop
{count = 1;
System.out.println( count );
}
int count; 10
for (count=10;count>=-2;count-=4) 6
System.out.println(count); 2
System.out.println(count); -2
-6
int count; 13

QF01/2611– page 1/2


Al-ZaytoonahUniversity of Jordan ‫جـامعـة الـزيتـونــــة األردنيــة‬

for (count=1;count<=10;count+=4)
if(count==3)
count+=4;
else
count+=2;
System.out.println(count);
double i,sum=0;
for(i=1;i<=3;i++) 14.0
sum=sum+pow(2,i);
System.out.println(sum);

:Q2. Convert the following to switch

Scanner input=new Scanner(System.in);


char operator;
System.out.println("Enter the operator");
operator=input.next().charAt(0);
if(operator== '>')
System.out.println("greater than operator");
else if(operator=='<')
System.out.println("less than operator");
else
System.out.println("not an allowed operator");

Scanner input=new Scanner(System.in);


char operator;
System.out.println("Enter the operator");
operator=input.next().charAt(0);
switch(operator)
{ case '>':
System.out.println("greater than operator");
break;
case '<':
System.out.println("less than operator");
break;
default:
System.out.println("not an allowed operator");
}

QF01/2611– page 2/2

You might also like