Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,043 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,551 people online right now. Registration is fast and FREE... Join Now!




control structures

 
Reply to this topicStart new topic

control structures

programer18
5 Aug, 2008 - 12:48 AM
Post #1

New D.I.C Head
*

Joined: 5 Aug, 2008
Posts: 2

what is the syntax of if statement, while statement, do-while statement, for statement, and the switch case constructure.
User is offlineProfile CardPM
+Quote Post

AmitTheInfinity
RE: Control Structures
5 Aug, 2008 - 01:02 AM
Post #2

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,025



Thanked: 35 times
Dream Kudos: 125
My Contributions
QUOTE(programer18 @ 5 Aug, 2008 - 02:18 PM) *

what is the syntax of if statement, while statement, do-while statement, for statement, and the switch case constructure.


cpp


if(<condition>)
{
// code
}
else //may be or may not be required
{
// code
}

if(<condition>)
{
// code
}
else if
{
// code
}
else
{
// code
}

switch(<variable to switch on>)
{
case <one of possible values of variable> : //code
case <one of possible values of variable> : //code
case <one of possible values of variable> : //code
default : //code
}


while(<condition>)
{
// code
}

do
{
// code
}while(<condition>);

for(<initial assignment>;<condition>;<increment/decrement>)
{
// code
}



examples
cpp

int i=1;
if(i==1)
{
printf("Condition is true");
}
else
{
printf("Condition is false");
}

if(i==1)
{
printf("Condition1 is true");
}
if(i==2)
{
printf("Condition2 is true");
}
else
{
printf("Condition is false");
}

switch(i)
{
case 1 : printf("Condition1 is true"); break;
case 2 : printf("Condition2 is true"); break;
case 3 : printf("Condition3 is true"); break;
default: printf("Condition is false"); break;
}

while(i<10)
{
printf("Inside while for i=%d",i);
i = i+1;
}

do
{
printf("Inside do while for i=%d",i);
i = i+1;
}while(i<20);

for(i=0;i<10;i++)
{
printf("Inside for i=%d",i);
}



You can get all syntactical information about C here.

This post has been edited by AmitTheInfinity: 5 Aug, 2008 - 01:04 AM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 04:45PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month