Sunday, 28 June 2015

Using Namespace, Characters and Strings, Flow of Control


[C++] Using Namespace, Characters and Strings, Flow of Control



Till now, in all of our programs we have used std::cin or std::cout. In old books and tutorials, you will see simple cin or cout statements, without std::. This will surely work on old compilers like Turbo C++, but will give a Compile time Error in the latest compilers. To solve this problem, we will use a statement using namespace std.

//Program to input a number and display its table

#include<iostream>

using namespace std;

int main()
{
int num;
cout<<"Enter a number: ";
cin>>num;
cout<<num<<"\n";
cout<<num*2<<"\n";
cout<<num*3<<"\n";
cout<<num*4<<"\n";
cout<<num*5<<"\n";
cout<<num*6<<"\n";
cout<<num*7<<"\n";
cout<<num*8<<"\n";
cout<<num*9<<"\n";
cout<<num*10<<"\n";

return 0;

}

In the above program, we have simply used cin and cout. The statement:
using namespace std;

Makes it possible. We will use this statement in the coming programs as well. We won't go into details of what namespaces are, but for now, it is used to make our program look like the ones explained in older books and to reduce some characters that we need to type in. :)

One more thing we need to understand is the difference between characters and strings. A CHARACTER is a single alphabet, digit or any other special symbol enclosed in single quotes. For example, 'A', 'n' etc are characters, but 'Nikhil' is not a character, since character means ONLY ONE character. BACKSLASH CHARACTERS are special characters preceeded by a '\' sybmol and are used for special purposes, such as to go to a new line, as a backspace, to insert blank space (tab) etc. Examples are: '\n' , '\b' , '\t'. In most compilers,a character occupies ONE BYTE of memory

A STRING is a group of one or more characters enclosed in double quotes. Examples are: "Nikhil", "GUITAR","N" etc. The number of bytes a string occupies is the number of characters in it plus one. Therefore, "Nikhil" will occupy (6+1)=7 bytes in memory. This is because a String is represented in the memory as:

N i k h i l \0

That is, each string is accompanied by a NULL character, that tells the compiler that the string ends here. Null Character is also a type of backslash character.

The next thing we will talk about is FLOW OF CONTROL Till now, all the programs we have made till now use SEQUENCE CONSTRUCT. This means that one statement is executed after the other, in a top to down manner. That is, the statement that is written at top gets executed first, then the next and so on. C++ provides two more Constructs, namely SELECTION and ITERATION.

SELECTION Construct means that the program executes based on some condition. The computer makes a decision based on a given condition, and then some statements are executed, depending on whether the condition is True or False. For example, suppose I want my program to turn off the light during day time, say 7.00 am and turn it on at night, say 7.00 pm. The program will check whether the current time is 7.00 pm or not. If it is, it will turn on the light. If the time is 7.00 am, it will turn it off. At other times, it may do nothing. ;)

ITERATION Construct means that some statements are repeatedly executed a finite number of times. This is useful when we want the program to do a repeated task, again based on some condition. In the above example, if we want to display he tables of all the numbers from 1 to 10, it will be combursome to write 100 lines of code. To make it compact, we can use Iteration.

No comments:

Post a Comment