0% found this document useful (0 votes)
139 views4 pages

CS125 - Lab 01 - Programing Basics

This lab covers programming basics including naming conventions, using an IDE, debugging, variables, operators, and variable scope. Students will write a simple "Hello World" program in C++ using Visual Studio, dissect the code line by line, use constants and variables with arithmetic operators, and explore variable scope through examples. The objective is to learn fundamental programming concepts and practices.

Uploaded by

asadhppy
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
139 views4 pages

CS125 - Lab 01 - Programing Basics

This lab covers programming basics including naming conventions, using an IDE, debugging, variables, operators, and variable scope. Students will write a simple "Hello World" program in C++ using Visual Studio, dissect the code line by line, use constants and variables with arithmetic operators, and explore variable scope through examples. The objective is to learn fundamental programming concepts and practices.

Uploaded by

asadhppy
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

CS125: Programming Fundamentals

Lab No. 1 Programming Basics


Objective
1. 2. 3. 4. Programming practices, naming conventions Integrated Development Environment (IDE), Its components, Debugging, Constants , Variables, Operators Scope basics

Programming Practices
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. Keep scopes small. Dont use the same name in both a scope and an enclosing scope. Declare one name (only) per declaration. Keep common and local names short, and keep uncommon and nonlocal names longer. Avoid similar-looking names. Maintain a consistent naming style. Choose names carefully to reflect meaning rather than implementation. Remember that every declaration must specify a type (there is no implicit int) Avoid unnecessary assumptions about the numeric value of characters. Avoid unnecessary assumptions about the size of integers. Avoid unnecessary assumptions about the range of floating-point types. Prefer a plain int over a short int or short or a long int or a long. Prefer a double over a float or a long double. Prefer plain char over signed char and unsigned char. Avoid making unnecessary assumptions about the sizes of objects. Avoid unsigned arithmetic. View signed to unsigned and unsigned to signed conversions with suspicion. View floating-point to integer conversions with suspicion. View conversions to a smaller type, such as int to char, with suspicion. Develop habit to declare and initialize the variable at the same time.

IDE

Lab No. 1: Programming Basics

CS125: Programming Fundamentals

Microsoft Visual Studio 2012 Express Edition for Desktop


http://www.microsoft.com/visualstudio/eng/downloads

Exercise
Write the simplest C++ program using Visual C++ IDE.

Hello World
#include <iostream> int main() { std::cout << "Hello World!" <<endl; }

Yet another way


#include <iostream> using namespace std; int main() { cout << "Hello World!" <<endl; }

Exercise
1. Line by line dissection of the above programs 2. Usage of header file and namespaces

Constant, Variables, Arithmetic Operators


int main() { int a = 3; int b = 5;

Lab No. 1: Programming Basics

CS125: Programming Fundamentals


int c = a + b; int d = a / b; } int main() { double double double double }

a b c d

= = = =

3; 5; a + b; a / b;

Exercise
Check the output of variables c and d in each of the above program by debugging.

Variable Scope
1. Below is a sample program. Use it to answer the following question: What happens if we declare the same name twice within a block, giving it two different meanings?
#include <iostream > using namespace std; int main () { int arg1; arg1 = -1; int x, y, z; char my_double = '5'; char arg1 = 'A'; cout << arg1 <<"\n"; return 0; }

2. Below is a sample program. Use it to answer the following question: Suppose an identifier has two different declarations, one in an outer block and one in a nested inner block. If the name is accessed within the inner block, which declaration is used?
#include <iostream > using namespace std; int main () { int arg1; arg1 = -1; { char arg1 = 'A';

Lab No. 1: Programming Basics

CS125: Programming Fundamentals


cout << arg1 << endl; } return 0; }

3. Below is a sample program. Use it to answer the following question: Suppose an identifier has two different declarations, one in an outer block and one in a nested inner block. If the name is accessed within the outer block, but after the inner block, which declaration is used?
#include <iostream > using namespace std; int main () { int arg1; arg1 = -1; { char arg1 = 'A'; } cout << arg1 << endl; return 0; }

4. Below is a sample program that will not compile. Why not? By moving which line can we get the code to compile?
using namespace std; int main () {

cout << "Hello World!" <<endl; return 0;


} #include <iostream >

Checked By: Lab No. 1: Programming Basics

Date:

You might also like