Professional Documents
Culture Documents
Lab 4 PF 1
Lab 4 PF 1
Lab 4 PF 1
Question 1 - (3 Marks)
#include <iostream>
int a = 30; // A variable in the global scope. Valid everywhere in the .cpp file it is defined in.
int main()
int x = 10;
int y;
if (true)
cout << "I am inside the innermost scope. " << endl;
int b;
y = 20;
int c = 15;
int z;
z = 4;
return 0;
Question 2 - ( 6 Marks)
#include <iostream>
int main()
int check;
if (true)
int x = 5;
int y;
else
{
int x = 6;
y = 5;
cout << x;
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.
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:
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;
}
cout<< count;
}
return 0;
}
--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