Lab 4 PF 1

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Programming Fundamentals Lab 4: Jump statements and Statement Scope

Date Assigned: 06/ 06/ 2020

Submission Date: Thursday 11/ 06/ 2020, Submit on Google Classroom

Name: sawara rehman

Regn no: 329-FBAS-bsit 4/f18

Question 1 - (3 Marks)

#include <iostream>

using namespace std;

int a = 30; // A variable in the global scope. Valid everywhere in the .cpp file it is defined in.

int main()

Name the variables that will be created at start of this scope:


x,y,z
{

int x = 10;

int y;

for (int i = 0; i < 10; i++)

Name the variables that will be created at start of this scope:


i

cout << "entering scope of for" << endl;

if (true)

Name the variables that will be created at start of this scope:


b

cout << "I am inside the innermost scope. " << endl;

int b;
y = 20;

int c = 15;

cout << y << endl;

int z;

z = 4;

return 0;

Question 2 - ( 6 Marks)

#include <iostream>

using namespace std;

int main()

int check;

if (true)

int x = 5;

int y;

else

{
int x = 6;

y = 5;

cout << x;

cout << check;

check = 10;

return 0;

1 - Screenshot the errors in the code after running the code in your IDE
2 - Which three statements are causing error (highlight them) and explain why for each? Take help
from the screenshot above.

 In else statement y is not initialized.


 Cout<<”x”; it must be written in if and else statement.

3 - Also, What kind of error are they each (choose from undeclared variable error in part of code that
will definitely run, undeclared variable error in part of code that will never run, uninitialized variable
error)

Uninitialized variable.
Question 3: (9 Marks)
Separate out loop condition and loop body from the code snippets below:

Loop condition Loop body Answer the following


questions keeping in mind
only the given piece of
code:
1 for ( int i = 0 ; i < 10 ; I<10 {cout<<I;
++i) Int y=2; How many times is i
{ cout<<y+I; created and destroyed?
cout << i; _create 1 time and
int y = 2; destroy 1 time.
cout << y + i; How many times is y
} created and destroyed?
Y is created 10 times and
destroy 1 time.
2 int x = 0; X<100 { How many times is x
Cout<<x; created and destroyed?
while ( x <100) Int y=3; X is created 1 time and
{ Cout<<y+x; destroy one time .
cout<<x; +++x; How many times is y
int y = 3; } created and destroyed?
cout << y+x; Y is created 100 times
and destroy.
++x;
}
3 int y = 0; Y<100 {
cout << y; How many times is y
do created and destroyed?
{ int z = 5; Y is created 1 times and
cout << y; cout << y+z; destroy 1 time.
How many times is z
int z = 5; y+=2 created and destroyed?
cout << y+z; } Z is created 100 times
and destroy 1 time.
y + = 2;

} while ( y < 100)

Question 4: (4 Marks)
Write the expected output of the programs.
Code snippet Expected output
1 #include <iostream> 135791113151719
using namespace std;
int main()
{
for (int count = 0 ; count < 20; ++count)
{
if ( count % 2 == 0)
{ continue; }
cout<< count;
}
return 0;
}

2 #include <iostream> 13579


using namespace std;
int main()
{
for (int count = 1 ; count <= 20; count +=2)
{
if ( count % 11 == 0)
{ break; }

cout<< count;
}
return 0;
}

3 int count = 10; 10 9 8 7


while (count > 0)
{
if (count == 6)
continue;

cout << count << ' ';

--count;
}

4 08224
int count = 0 ; 18224
do 28224
{ 38224
if (count == 10) 48224
break; 58224
cout << count << ' '; 68224
78224
} while ( ++count < 20); 88224
98224

You might also like